views:

120

answers:

4

Hi All!

What's the diffrent between servlet 3.0 asynchronous feature against:

old servlet impl
doGet(request,response) {
Thread t = new Thread(new Runnable()
     void run(){
        // heavy processing
        response.write(result)
     }
}

In servlet 3.0 if I waste a thread to do heavy processing - I earn one more thread in the container, but I waste it in heavy processing... :(

Somebody can help?

+1  A: 

Hi,

The servlet 3.0 async feature provides to keep the http connection open but to release any unused threads when the request cannot be served immediately but is waiting for some event to occur or for example when you are writing some comet/reverse ajax application.,In the above case you are creating a new thread completely so it should not make any difference for you unless you want to keep the request waiting for some event.

Best Regards, Keshav

keshav.veerapaneni
+1  A: 

This won't work. Once your doGet method ends, the response is complete and sent back to the client. Your thread may or may not still be running, but it can't change the response any longer.

What the new async feature in Servlet 3.0 does, is that it allows you to free the request thread for processing another request. What happens is the following:

RequestThread:  |-- doGet() { startAsync() }  // Thread free to do something else
WorkerThread:                 |-- do heavy processing --|
OtherThread:                                            |-- send response --|

The important thing is that once RequestThread has started asynchronous processing via a call to startAsync(...), it is free to do something else. It can accept new requests, for example. This improves throughput.

Ronald Wildenberg
A: 

There are several API-s supporting COMET (long living HTTP requests, where there is no thread/request problem) programming. So there is no strict need to use servlet 3 API for avoiding thread/request. One is the Grizzly engine which is running in Glassfish 2.11 (example). Second solution is Jetty Continuation. The third is Servlet 3 API..

The basic concept is that the request creates some container managed asynchronous handler in which the request can subscribe to an event identified by an object (for example a clientid string). Then the asynchronous processing thread once can say to the handler, that the event occours, and the request gets a thread to continue. It totally depends on your choosen application server wich API you can use. Which is your choice?

Gábor Lipták
One should note Grizzly and Jetty each accomplish this through their own workarounds, whereas the Servlet 3.0 standard means such non-portable (across different application servers) solutions will no longer be necessary. Naturally, Grizzly and Jetty both (or are going to) implement Servlet 3.0, one would imagine. So, it's not a matter of choosing between the three, but rather choosing which of the two, which implement the third, you like.
A: 

Creating your own threads in a servlet container is asking for trouble. (There might be cases where you have to do it, but if you have some framework that manages the threads for you, then you should use it.)

Mike Baranczak