server.start();
return ctx;
}
public static final String CONTEXT_ATTRIBUTE = "ATLAS_MAIN";
private static final String LOCAL_WAR_DIR = "./src/main/webapp";
<<<<<<< HEAD
private static SelectChannelConnector CONNECTOR;
public static void main(String[] args) throws Exception {
if(IS_PROCESSING) {
System.out.println(">>> Launching processing configuration");
}
createWebApp(warBase() + "/WEB-INF/web.xml", createApiServer());
createWebApp(warBase() + "/WEB-INF/web-monitoring.xml", createMonitoringServer());
new BackgroundTask(Duration.standardMinutes(1), new Runnable() {
@Override
public void run() {
if (CONNECTOR != null) {
CONNECTOR.statsReset();
}
}
}).start();
}
private static void createWebApp(String descriptor, final Server server) throws Exception {
WebAppContext ctx = new WebAppContext(warBase(), "/");
ctx.setDescriptor(descriptor);
server.setHandler(ctx);
server.start();
}
private static String warBase() {
if (new File(LOCAL_WAR_DIR).exists()) {
return LOCAL_WAR_DIR;
}
ProtectionDomain domain = AtlasMain.class.getProtectionDomain();
return domain.getCodeSource().getLocation().toString();
}
private static Server createApiServer() throws Exception {
int port = defaultPort();
String customPort = System.getProperty("server.port");
if (customPort != null) {
port = Integer.parseInt(customPort);
}
int requestThreads;
String requestThreadsString = System.getProperty("request.threads");
if (requestThreadsString == null) {
requestThreads = 100;
} else {
requestThreads = Integer.parseInt(requestThreadsString);
}
Server server = createServer(port, requestThreads, 200, "api-request-thread");
CONNECTOR = (SelectChannelConnector) server.getConnectors()[0];
return server;
}
private static Server createMonitoringServer() throws Exception {
int port = 8081;
String customPort = System.getProperty("monitoring.port");
if (customPort != null) {
port = Integer.parseInt(customPort);
}
return createServer(port, 10, 20, "monitoring-request-thread");
}
private static Server createServer(int port, int maxThreads, int acceptQueueSize, String threadNamePrefix) {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setStatsOn(true);
connector.setPort(port);
connector.setAcceptQueueSize(acceptQueueSize);
QueuedThreadPool pool = new QueuedThreadPool(maxThreads);
pool.setName(threadNamePrefix);
connector.setThreadPool(pool);
// one acceptor per CPU (ish)
connector.setAcceptors(4);
connector.setRequestBufferSize(1024);
connector.setResponseHeaderSize(1024);
server.setConnectors(new Connector[] { connector });
return server;
}
private static int defaultPort() {
return IS_PROCESSING ? 8282 : 8080;
}
public static int numberOfConnectionsInLastMinute() {
return CONNECTOR.getConnectionsOpen();
=======
private static final ScheduledExecutorService CONNECTOR_RESET_THREAD_SERVICE = new ScheduledThreadPoolExecutor(1);
private static final boolean IS_PROCESSING = Boolean.parseBoolean(System.getProperty("processing.config"));
private SelectChannelConnector apiConnector;
public static void main(String[] args) throws Exception {
if (IS_PROCESSING) {
System.out.println(">>> Launching processing configuration");
}
new AtlasMain().start();
}
public void start() throws Exception {
WebAppContext apiContext = createWebApp(warBase() + "/WEB-INF/web.xml", createApiServer());
WebAppContext monitoringContext = createWebApp(warBase() + "/WEB-INF/web-monitoring.xml",
createMonitoringServer());
monitoringContext.setAttribute(CONTEXT_ATTRIBUTE, this);
apiContext.setAttribute(CONTEXT_ATTRIBUTE, this);
CONNECTOR_RESET_THREAD_SERVICE.scheduleAtFixedRate(this, 1, 1, TimeUnit.MINUTES);
}
@Override
public void run() {
if (apiConnector != null) {
apiConnector.statsReset();
}
}
private WebAppContext createWebApp(String descriptor, final Server server) throws Exception {
WebAppContext ctx = new WebAppContext(warBase(), "/");
ctx.setDescriptor(descriptor);
server.setHandler(ctx);
private String warBase() {
if (new File(LOCAL_WAR_DIR).exists()) {
return LOCAL_WAR_DIR;
}
ProtectionDomain domain = AtlasMain.class.getProtectionDomain();
return domain.getCodeSource().getLocation().toString();
}
private Server createApiServer() throws Exception {
int requestThreads;
String requestThreadsString = System.getProperty("request.threads");
if (requestThreadsString == null) {
requestThreads = 100;
} else {
requestThreads = Integer.parseInt(requestThreadsString);
}
Server server = createServer("server.port", defaultPort(), requestThreads, 200, "api-request-thread");
apiConnector = (SelectChannelConnector) server.getConnectors()[0];
return server;
}
private Server createMonitoringServer() throws Exception {
return createServer("monitoring.port", 8081, 10, 20, "monitoring-request-thread");
}
private Server createServer(String portProperty, int defaultPort, int maxThreads,
int acceptQueueSize, String threadNamePrefix) {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setStatsOn(true);
int port = defaultPort;
String customPort = System.getProperty(portProperty);
if (customPort != null) {
port = Integer.parseInt(customPort);
}
connector.setPort(port);
connector.setAcceptors(Runtime.getRuntime().availableProcessors());
connector.setAcceptQueueSize(acceptQueueSize);
QueuedThreadPool pool = new QueuedThreadPool(maxThreads);
pool.setName(threadNamePrefix);
connector.setThreadPool(pool);
connector.setRequestBufferSize(1024);
connector.setResponseHeaderSize(1024);
server.setConnectors(new Connector[] { connector });
return server;
}
private int defaultPort() {
return IS_PROCESSING ? 8282 : 8080;
}
public int getNumberOfConnectionsMax() {
return apiConnector.getConnectionsOpenMax();
}
public static int getMaxNumberOfOpenConnectionsInLastMinute(Object atlasMain)
throws IllegalArgumentException,
SecurityException, IllegalAccessException, InvocationTargetException {
Class extends Object> clazz = atlasMain.getClass();
if (clazz.getCanonicalName() != AtlasMain.class.getCanonicalName()) {
throw new IllegalArgumentException("Parameter must be instance of "
+ AtlasMain.class.getCanonicalName());
}
try {
return (Integer) clazz.getDeclaredMethod("getNumberOfConnectionsMax").invoke(atlasMain);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(
"There appears to be a mismatch between AtlasMain objects",
e);
}
>>>>>>> bf1631a6af260e1c17ac6004fa2bc5e3b18bf4e3
}
} |