Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaEE 6 AsyncContext behavior after timeout

What is the exact behavior of AsyncContext after it times out? Does it still run and does it return response using getResponse()?

like image 838
xwinus Avatar asked Mar 16 '13 09:03

xwinus


1 Answers

Java Servlet 3.0 Maintenance Release on page 34 reads:

A call to this method - ServletRequest.startAsync(ServletRequest req, ServletResponse res) - ensures that the response isn't committed when the application exits out of the service method. It is committed when AsyncContext.complete is called on the returned AsyncContext or the AsyncContext times out and there are no listeners associated to handle the time out. The timer for async time outs will not start until the request and it’s associated response have returned from the container. The AsyncContext could be used to write to the response from the async thread. It can also be used to just notify that the response is not closed and committed.

From the javadoc of the javax.servlet.AsyncContext interface:

In the event that an asynchronous operation has timed out, the container must run through these steps:

  1. Invoke, at their onTimeout method, all AsyncListener instances registered with the ServletRequest on which the asynchronous operation was initiated.
  2. If none of the listeners called complete() or any of the dispatch() methods, perform an error dispatch with a status code equal to HttpServletResponse.SC_INTERNAL_SERVER_ERROR.
  3. If no matching error page was found, or the error page did not call complete() or any of the dispatch() methods, call complete()."

The last line above is of most importance - there'll be a call to AsyncContext.complete(). The javadoc says:

Completes the asynchronous operation that was started on the request that was used to initialze this AsyncContext, closing the response that was used to initialize this AsyncContext.

With AsyncContext closed, calls to most of its methods will throw IllegalStateException.

like image 199
Jacek Laskowski Avatar answered Nov 16 '22 13:11

Jacek Laskowski