Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Servlet 3.0 Asynchronous Operations

 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?...
 
Author
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--> 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
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ram,

Yes.
 
I once met a man from Nantucket. He had a tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic