On JBoss AS 7.02 I tried the Servlet 3 asynchronous support. As a test I created an infinite dispatch loop:
The Servlet:
@WebServlet(urlPatterns = "/asynctest", asyncSupported = true) public class AsyncServlet extends HttpServlet { private static final long serialVersionUID = 1L; @EJB private AsyncBean asyncBean; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(); asyncBean.doAsyncStuff(asyncContext); System.out.println("After handing work to asyncbean"); } }
The worker:
@Stateless public class AsyncBean { @Asynchronous public void doAsyncStuff(AsyncContext asyncContext) { try { Thread.sleep(SECONDS.toMillis(1)); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Async work done, dispatching to Servlet container"); asyncContext.dispatch(); } }
The classes were put in a .war (without any other files) and deployed to JBoss AS 7.02.
The result is that the first call to request.startAsync(); indeed starts asynchronous processing (the client keeps waiting for the response), but after the worker dispatches the work back to the Servlet, the second call to request.startAsync(); seems to be ignored and after the doGet method ends the response is closed.
I'm not 100% sure, but it seems this is not entirely correct. The JavaDoc for AsyncContext#dispatch() says the following:
Control over the request and response is delegated to the dispatch target, and the response will be closed when the dispatch target has completed execution, unless ServletRequest#startAsync() or ServletRequest#startAsync(ServletRequest, ServletResponse) are called.
(emphasis by me)
I tested it on GlassFish and there a loop does occur with a continous dispatching between the worker and servlet happening.
FYI, I now also tested this on AS 7.1CR1b and there it does work as expected.