File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Async Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Async Servlet

Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Hello dear rachers,

I will ask again since last time I did not get any answers. It has to do with async support of Servlet 3.0.
Now, from what I understand async support has two main purposes : release the http container Thread as soon as possible and support the Comet-like programming.

I want to discuss the first one really. So, the idea is this, every time we send in a request, the container allocates a Thread for it to be processed. If there is a Web Service (for example) that this particular Thread needs to access and this Web Service takes a lot of time to respond, then we "start" the async support: meaning that we start another Thread (not a http container Thread), but a usual Thread and we pass the actual work to this second Thread, while the Http Container thread goes back to the pool. Now this is where I do not get it. We are supposed to resolve the Thread starvation issue and, but we are creating a new Thread... Also, look at the sample below:

AsyncContext context = request.startAsync(request, response);
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
executor.execute(new AsyncRunnable(context));

where AsyncRunnable is nothing more then a slow resource (the Web Service simulated by a Thread that sleeps for 10 seconds). If I am the client and I issue a request , I still have to wait for those 10 seconds to pass before I get my response. I do not really care that the Http Thread was released, then another Thread did my Job, and then the same Http Thread issued my response back.

Basically to me, Async Servlet is just a way to release the Http Container Thread - it is a server side enchantment, if the server is not busy (not even close to Thread starvation), then the user experience will not be any different, right? At the same time this technique can be achieved with synchronous Servlet and a simple Thread - I so not then get it what is the use of async Servlets asnyway?
In case I said something stupid, please correct me.

Thank you,
Eugene.


Anapurna Mehra
Greenhorn

Joined: Aug 23, 2010
Posts: 2
The usage of Async servlet comes into use if you note that it is the "Context" that gets preserved.

That means all the information that you had received in the request and response objects including any additional wrapping/processing that you had got in your response objects - remains preserved until you have finish with the time consuming task. And all of this is done without using the server's resource(Thread).

From my understanding, the actual thread that launched the http request is completed once we register our context with our own user custom thread. This custom thread then initiates the needed task. Once that is done, we have the possibility of either closing the flow with a call to complete or using overloaded dispatch methods which allow us to communicate to the server to send a response back to user.

So yes - from the user's perspective it would still take time - however the async flow is to allow the user to be informed when the processing has completed.And not to keep the user waiting for the processing.

- Ana.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

Please do no post the same question more than once.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Async Servlet