0
votes

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.

2
"I would prefer to not use 3rd party component". You are using com.sun.* which is not guaranteed to be present in the JVM in the future. The JVM only promises java.* and javax.* 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 use com.sun.net.httpserver.HttpServer is 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
Does javax contain own "standard" servlet container/http server? - Vova l

2 Answers

1
votes

In jetty-9 many of the signatures changed for how the server was wired up. In leu of having all the getters and setters it was switched to more constructor based so you should find the ability to pass in the thread pool there. As for the prestarting of the threads, feel free to open up a bug for that feature at bugs.eclipse.org under RT/Jetty and we'll take a look.

Also, we have a jetty-http-spi implementation in git for jetty-9 but we have not published any artifacts for it. We have them for 7 and 8, we are just attempting to gauge if its worth keeping around for jetty-9 based on requests for it to appear. The classes in there extend the com.sun http server you mention above and allow you to use jetty in place of the one that is bundled inside, so this is another potential option, though you still have the thread pool thread starting issue which I don't see as a particularly difficult issue to sort out at first blush.

0
votes

1) I found the way to tune thread pool in Jetty:

Server server = new Server(80);
QueuedThreadPool p = (QueuedThreadPool) server.getThreadPool();
p.setMaxThreads(1012);

2) Also it's possible to control TCP session both in Jetty and in com.sun.http Jetty: just add header Connection: close to response, and Jetty will add this header and send FIN packet

response.setHeader("Connection", "close");
response.getWriter().println(str);

But doesn't work with Tansfer-Encoding: chunked, I guess it's bug.

com.sun.http: also use

t.getRequestHeaders().add("Connection", "Close");

But it works quite different - it doesn't add header to response, but immediately close TCP session, so use it after sending data:

OutputStream os = t.getResponseBody();
os.write(str.getBytes());
t.getRequestHeaders().add("Connection", "Close");