Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
    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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Async Servlet

Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Report post to moderator
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,

Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
Posts: 67451
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do no post the same question more than once.
It's just a flesh wound! Or a tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic