This project demonstrates asynchronous Servlet 3 filters.
Run the application in a Tomcat container or directly with Jetty
> ./gradlew runJetty
and open http://localhost:8080
- First the filter is called with dispatch type REQUEST
- An AsyncContext is started
- A asynchronous request (to http://checkip.amazonaws.com) is done and the doFilter method exits
- When the callback on the asynchronous call returns the dispatch() is called on the AsyncContext
- The request is dispatched again through the doFilter method, this time with dispatch type ASYNC
- This time we call chain.doFilter() and the request enters the servlets doGet() method
- A new AsyncContext is started and a asynchronous dispatch to a JSP page is done on an other thread
- The servlets doGet() method exits and control is returned to the filter that registers an AsyncListener
- The onStartAsync() is called on the AsyncListener
- The filters doFilter() method exits
- The response from the jsp is returned
Printout is something like this in Tomcat 8: (In Jetty 9 the last row is missing...)
WaitFilter::doFilter: >>> Start
Filter::doFilter: [REQUEST] Start >>> (Thread[http-nio-8080-exec-6,5,main])
Filter::doFilter: [REQUEST] End <<< (Thread[http-nio-8080-exec-6,5,main])
Filter::doFilter: <<< End
Filter::doFilter: [REQUEST] AsyncResponse <<< (Thread[New I/O worker #1,5,RMI Runtime]), attr=81.170.155.156
Filter::doFilter: >>> Start
Filter::doFilter: [ASYNC] before doFilter >>> (Thread[http-nio-8080-exec-7,5,main])
Servlet::doGet: Start >>> (Thread[http-nio-8080-exec-7,5,main])
AsyncListener::onStartAsync(Thread[http-nio-8080-exec-7,5,main])
Servlet::doGet: End <<< (Thread[http-nio-8080-exec-7,5,main])
Filter::doFilter: [ASYNC] after doFilter <<< (Thread[http-nio-8080-exec-7,5,main])
Servlet::In Async(Thread[pool-1-thread-1,5,RMI Runtime])
Filter::doFilter: <<< End
Hello in JSP (Thread[http-nio-8080-exec-8,5,main]) attr=81.170.155.156
AsyncListener::onComplete(Thread[http-nio-8080-exec-8,5,main])