public static void main(String[] args) throws Exception {
final int port = args.length > 0
? Integer.parseInt(args[0]) : 8080;
final HttpServer httpServer = new HttpServer();
final NetworkListener networkListener = new NetworkListener(
"http-listener", "0.0.0.0", port);
final TCPNIOTransport transport = networkListener.getTransport();
// force to not initialize worker thread pool
transport.setWorkerThreadPoolConfig(null);
transport.setSelectorRunnersCount(Runtime.getRuntime().availableProcessors() * 2);
// set PooledMemoryManager
transport.setMemoryManager(new PooledMemoryManager());
// always keep-alive
networkListener.getKeepAlive().setIdleTimeoutInSeconds(-1);
networkListener.getKeepAlive().setMaxRequestsCount(-1);
// disable transaction timeout
networkListener.setTransactionTimeout(-1);
// remove the features we don't need
networkListener.registerAddOn(new SimplifyAddOn());
// add HTTP pipeline optimization
networkListener.registerAddOn(new HttpPipelineOptAddOn());
// disable file-cache
networkListener.getFileCache().setEnabled(false);
httpServer.addListener(networkListener);
httpServer.getServerConfiguration().addHttpHandler(
new RootHttpHandler(), "/");
// httpServer.getServerConfiguration().addHttpHandler(
// new PlainTextHttpHandler(), "/plaintext");
// httpServer.getServerConfiguration().addHttpHandler(
// new JsonHttpHandler(), "/json");
try {
httpServer.start();
System.err.print("Server started.\n");
synchronized (Server.class) {
Server.class.wait();
}
} finally {
httpServer.shutdown();
}
}