value = console.readLine().trim();
if (value.length() == 0)
value = String.valueOf(maxThreads);
maxThreads = Integer.parseInt(value);
BayeuxServerImpl bayeuxServer = new BayeuxServerImpl();
bayeuxServer.addExtension(new AcknowledgedMessagesExtension());
MonitoringQueuedThreadPool jettyThreadPool = new MonitoringQueuedThreadPool(maxThreads);
MonitoringThreadPoolExecutor websocketThreadPool = new MonitoringThreadPoolExecutor(maxThreads, jettyThreadPool.getIdleTimeout(), TimeUnit.MILLISECONDS, new ThreadPoolExecutor.AbortPolicy());
String availableTransports = "jsrws,jettyws,http,asynchttp";
String transports = "jsrws,http";
System.err.printf("transports (%s) [%s]: ", availableTransports, transports);
value = console.readLine().trim();
if (value.length() == 0)
value = transports;
for (String token : value.split(","))
{
switch (token.trim())
{
case "jsrws":
bayeuxServer.addTransport(new LoadWebSocketTransport(bayeuxServer, websocketThreadPool));
break;
case "jettyws":
bayeuxServer.addTransport(new LoadJettyWebSocketTransport(bayeuxServer, websocketThreadPool));
break;
case "http":
bayeuxServer.addTransport(new JSONTransport(bayeuxServer));
break;
case "asynchttp":
bayeuxServer.addTransport(new AsyncJSONTransport(bayeuxServer));
break;
default:
throw new IllegalArgumentException("Invalid transport: " + token);
}
}
boolean stats = true;
System.err.printf("record statistics [%b]: ", stats);
value = console.readLine().trim();
if (value.length() == 0)
value = String.valueOf(stats);
stats = Boolean.parseBoolean(value);
boolean reqs = true;
System.err.printf("record latencies [%b]: ", reqs);
value = console.readLine().trim();
if (value.length() == 0)
value = String.valueOf(reqs);
reqs = Boolean.parseBoolean(value);
boolean qos = false;
System.err.printf("detect long requests [%b]: ", qos);
value = console.readLine().trim();
if (value.length() == 0)
value = String.valueOf(qos);
qos = Boolean.parseBoolean(value);
Server server = new Server(jettyThreadPool);
// Setup JMX
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbeanContainer);
SslContextFactory sslContextFactory = null;
if (ssl)
{
Path keyStoreFile = Paths.get("src/main/resources/keystore.jks");
if (Files.exists(keyStoreFile))
throw new FileNotFoundException(keyStoreFile.toString());
sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keyStoreFile.toString());
sslContextFactory.setKeyStorePassword("storepwd");
sslContextFactory.setKeyManagerPassword("keypwd");
}
ConnectionFactory[] factories = AbstractConnectionFactory.getFactories(sslContextFactory, new HttpConnectionFactory());
ServerConnector connector = new ServerConnector(server, null, null, null, 1, selectors, factories);
// Make sure the OS is configured properly for load testing;
// see http://cometd.org/documentation/howtos/loadtesting
connector.setAcceptQueueSize(2048);
// Make sure the server timeout on a TCP connection is large
connector.setIdleTimeout(50 * Config.MAX_NETWORK_DELAY);
connector.setPort(port);
server.addConnector(connector);
HandlerWrapper handler = server;
RequestLatencyHandler requestLatencyHandler = null;
if (reqs)
{
requestLatencyHandler = new RequestLatencyHandler();
handler.setHandler(requestLatencyHandler);
handler = requestLatencyHandler;
}
if (qos)
{
RequestQoSHandler requestQoSHandler = new RequestQoSHandler();
handler.setHandler(requestQoSHandler);
handler = requestQoSHandler;
}
StatisticsHandler statisticsHandler = null;
if (stats)
{
statisticsHandler = new StatisticsHandler();
handler.setHandler(statisticsHandler);
handler = statisticsHandler;
}
// Add more handlers if needed
ServletContextHandler context = new ServletContextHandler(handler, Config.CONTEXT_PATH, ServletContextHandler.SESSIONS);
context.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer);
context.setInitParameter(ServletContextHandler.MANAGED_ATTRIBUTES, BayeuxServer.ATTRIBUTE);
WebSocketServerContainerInitializer.configureContext(context);
// Setup default servlet to serve static files
context.addServlet(DefaultServlet.class, "/");
// Setup comet servlet
String cometdURLMapping = Config.SERVLET_PATH + "/*";
CometDServlet cometServlet = new CometDServlet();
ServletHolder cometdServletHolder = new ServletHolder(cometServlet);
context.addServlet(cometdServletHolder, cometdURLMapping);
// Make sure the expiration timeout is large to avoid clients to timeout
// This value must be several times larger than the client value
// (e.g. 60 s on server vs 5 s on client) so that it's guaranteed that
// it will be the client to dispose idle connections.
bayeuxServer.setOption(AbstractServerTransport.MAX_INTERVAL_OPTION, String.valueOf(10 * Config.MAX_NETWORK_DELAY));
// Explicitly set the timeout value
bayeuxServer.setOption(AbstractServerTransport.TIMEOUT_OPTION, String.valueOf(Config.META_CONNECT_TIMEOUT));
// Use the faster JSON parser/generator
bayeuxServer.setOption(AbstractServerTransport.JSON_CONTEXT_OPTION, Jackson1JSONContextServer.class.getName());
bayeuxServer.setOption("ws.cometdURLMapping", cometdURLMapping);
bayeuxServer.setOption(ServletContext.class.getName(), context.getServletContext());
server.start();
new StatisticsService(bayeuxServer, jettyThreadPool, websocketThreadPool, statisticsHandler, requestLatencyHandler);
}