aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes AsyncContext timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "AsyncContext timeout" Watch "AsyncContext timeout" New topic
Author

AsyncContext timeout

Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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?
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

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.


Piyush
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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

Joined: Jun 10, 2011
Posts: 207

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AsyncContext timeout