I'm trying to create simple java HTTP server (well, me too))), however I need control over thread pool and TCP session. I'd like to precreate all needed threads at startup, and also I need to close TCP session from HTTP handler.
First I tried to embed Jetty. Jetty 8 Server class has setThreadPool method, though its ThreadPool is less powerful than standard (for instance Jetty's ThreadPoolExecutor has no prestartAllCoreThreads (and I can't use Java's ThreadPoolExecutor, can I?).
But it seems I can close TCP session from servlet handler by
HttpSession session = request.getSession();
session.invalidate();
But Jetty 9 Server now has no setThreadPool method at all. Did it migrate to some other place?
Second I tried to use com.sun.net.httpserver.HttpServer. It works great with thread pool
ThreadPoolExecutor thp = (ThreadPoolExecutor) Executors.newCachedThreadPool();
thp.setMaximumPoolSize(1000);
thp.setCorePoolSize(1000);
thp.prestartAllCoreThreads();
server.setExecutor(thp);
but has no control over TCP connection close (HttpExchange class doesn't provide any method).
So actually I need somehow combine thread control from Sun HttpServer and session control from Jetty?
Controlling ThreadPool in Jetty 9 will be good too.
Also I would prefer do not use 3rd party components like Jetty, if possible.
Thanks.
com.sun.*which is not guaranteed to be present in the JVM in the future. The JVM only promisesjava.*andjavax.*to be present (Oracle, the new owners and caretakers of Java, have been changing and removing references to Sun for a while now). Your desire to usecom.sun.net.httpserver.HttpServeris highly likely to be met with the need for a 3rd party download of a legacy jar in the future. Possibly with no updates. - Joakim Erdfelt