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 Servlet 3.0 Asynchronous Operations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet 3.0 Asynchronous Operations" Watch "Servlet 3.0 Asynchronous Operations" New topic
Author

Servlet 3.0 Asynchronous Operations

Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Hi Budi Kurniawan,

As per my observation,

To process request, request processing thread from Thread pool is allotted by Servlet container.

Supposing some n request processing thread is waiting synchronously for the response which takes longer time to be sent from DB (or) some backend datasource.
Since some of the request processing thread involved in waiting for the response, it keeps the other
requests waiting for thread from ThreadPool to be allotted for processing.

Asynchronous Servlets remove this burden by making request processing thread to get back to Thread Pool for the availability of threads in Thread Pool to process the requests and
make the other thread to delegate the action of waiting for response.

To start the Asynchronous Session on the request, startAsync() is invoked and then this Asynchronous event enabled request object is passed to other thread to wait for response.

We can directly pass the request object to the delegating thread without the need of startAsync() method and so
request processing thread executing the servlet method will be returned back to Request processing thread pool.

So, why this startAsync method to make the asynchronous operations since we can perform this kind of asynchronous operations in versions prior to Servlet 3.0?...

SCJP 6 [SCJP - Old is Gold]
Budi Kurniawan
Author
Ranch Hand

Joined: Apr 30, 2002
Posts: 65
Hi Ram,

Your question is: We can directly pass the request object to the delegating thread without the need of startAsync() method and so request processing thread executing the servlet method will be returned back to Request processing thread pool. So, why this startAsync method to make the asynchronous operations since we can perform this kind of asynchronous operations in versions prior to Servlet 3.0?

This is a good question.

It's true that you can pass a task to an Executor or create a new thread to handle a long running operation prior to Servlet 3.0. However, there was no way you could notify the user of the outcome because it would return BEFORE the operation was complete.

Here's from Chapter 14 of my book:

The asynchronous processing feature allows you to be frugal with
container threads. You should use this feature for long-running operations.
What this feature does is release the request processing thread while waiting
for a task to complete so that the thread can be used by another request.
Note that the asynchronous support is only suitable if you have a long
running task AND you want to notify the user of the outcome of the task. If
you only have a long running task but the user does not need to know the
processing result, then you can just submit a Runnable to an Executor and
return right away. For example, if you need to generate a report (which
takes a while) and send the report by email when it's ready, then the servlet
asynchronous processing feature is not the optimum solution. By contrast, if
you need to generate a report and show it to the user when the report is
ready, then asynchronous processing may be for you.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Hi Budi,

Thanks for the reply.

While delegating the long running task to other thread from request processing thread, HttpServletRequest and HttpServletResponse can be passed as Constructor arguments while instantiating the Runnable task and bind it to thread (or) Executor.

So in run() method, Input parameters are fetched from HttpServletRequest and long running operation is proceeded...

Moreover, this thread will not return until the operation is complete since this thread will be involved in the operation execution... (instance, Thread will be involved in waiting for ResultSet from DB, so wont return until ResultSet is available).

After its execution completion, output can be sent in OutputStream by obtaining from HttpServletResponse.

Could you please describe in detail how the outcome could not be notified in non-Asynchronous Servlet approach?.
Budi Kurniawan
Author
Ranch Hand

Joined: Apr 30, 2002
Posts: 65
--> While delegating the long running task to other thread from request processing thread, HttpServletRequest and HttpServletResponse can be passed as Constructor arguments while instantiating the Runnable task and bind it to thread (or) Executor.

Passing the HttpServletRequest and HttpServletResponse objects to another thread won't help. The Servlet.service() method will still return and this will sever the HTTP connection with the client, which means you won't be able to notify the outcome of the long-processing task.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Thanks Budi,

Now got a clear picture of Asynchronous Operations..

So, startAsync method invocation on request set some implicit flag to indicate the JSP/Servlet container not to close the connection in
order to send the outcome.
Budi Kurniawan
Author
Ranch Hand

Joined: Apr 30, 2002
Posts: 65
Hi Ram,

Yes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Servlet 3.0 Asynchronous Operations