• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AsyncContext timeout

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have hard time understanding how exactly timeouts work for AsyncContext.

void setTimeout(long timeout)
The timeout applies to this AsyncContext once the container-initiated dispatch during which one of the ServletRequest#startAsync methods was called has returned to the container.


When exactly the container starts to countdown the set timeout?
- once ServletRequest#startAsync is called
- once AsyncContext#dispatch or AsyncContext#complete is called

From the explanation in the API and Servlet 3.0 specification I think that it should start when we complete the async processing, but this doesn't make any sense. I have made some tests and the timeout is working even if I don't call complete or dispatch.
Can someone give me some guidance on the matter?
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also had hard time understanding the timeout. Spent almost 2 hours this morning on this, experimented a lot, and finally I think I have understood what it means.

Async Timeout will start from the moment when the service method of your Async Servlet has returned to the container.

See following code for async servlet:


In the servlet code above I have set the timeout to 1 sec. and
code for AsyncProcess is :

To complete the AsyncProcess it will take atleast 4 seconds, so timeout occurs.

Now I modified my AsyncServlet code to this:


Here still the timeout is 1 sec and AsyncProcess is started just after the setting of timeout, so here also timeout should occur. But it does NOT. Why? because the service method has not yet returned to the container.

So I concluded from this that:
service() method is the container-initiated dispatch that the specification is talking about:

The timeout applies to this AsyncContext once the container-initiated dispatch during which one of the ServletRequest#startAsync methods was called has returned to the container.


read it like this :
The timeout applies to this AsyncContext once the service() method during which one of the ServletRequest#startAsync methods was called has returned to the container.

 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piyush,
Thanks a lot! Your finding makes sense. I think you managed to decode the specification. I will spend some more time testing timeouts(for example I will try some subsequent setTimeout() calls after the countdown has already started).

I have one more question about "container-initiated dispatch". I have always felt that I don't understand that term. I thought that it is somehow related to RequestDispatcher#forward or #include methods or even to AsyncContext#dispatch or complete, but from your explanation it seems that my assumptions are wrong.
"Container-initiated dispatch" is actually the container's call on servlet's service() method. Am I right?

Thanks again for the help!
 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I think the "Container-initiated dispatch" refers to either the service() method or doFilter() method, because from filters also we can start async request processing. This conclusion is only based on experimentation and can be wrong.

The above conclusion works well with setTimeOut() and complete() methods (I have not tried it with dispatch()), but there is a confusion. Specification mentions that setTimeout() and both addListener() methods throws IllegalStateException when:

Throws:
IllegalStateException - if this method is called after the container-initiated dispatch, during which one of the ServletRequest#startAsync methods was called, has returned to the container



I have tried a lot but never got the IllegalStateException thrown. I don't know whether it is a bug in Tomcat implementation or my understanding of "Container-initiated dispatch" is wrong.

If you find anything while you try more with code please share.

Thanks.
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic