I am trying to use the asynchronous context timeout function. But the behavior is very choppy. Sometimes a timeout occurs, and many times it does not. I embed my code here.
@WebServlet(name = "TestServlet", urlPatterns = {"/test"},asyncSupported = true) public class TestServlet extends HttpServlet{ private static final long serialVersionUID = 1L; private static PriorityBlockingQueue<Runnable> pq = new PriorityBlockingQueue<Runnable>(1000); private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,1,10, TimeUnit.SECONDS,pq); public void service(final ServletRequest servletRequest, final ServletResponse response) throws ServletException, IOException { TestListener listener = new TestListener(); final AsyncContext asyncContext = servletRequest.startAsync(); asyncContext.addListener(listener); asyncContext.setTimeout(100); Handler handler = new Handler(asyncContext); threadPoolExecutor.execute(handler); } }
The following is a list of listeners and a handler code.
public class TestListener implements AsyncListener { public void onComplete(AsyncEvent event) throws IOException { System.out.println("Event completed"); } public void onError(AsyncEvent event) throws IOException { event.getAsyncContext().complete(); } public void onStartAsync(AsyncEvent event) throws IOException {
And the output is intermittent. Including here -
[ ops@root combinedlogs]$ time curl "http://localhost:9001/mockresponse/test" real 0m0.506s user 0m0.001s sys 0m0.003s [ ops@root combinedlogs]$ time curl "http://localhost:9001/mockresponse/test" real 0m0.159s user 0m0.001s sys 0m0.003s
Catalina logs -
slept for 500 Completing async context timeout is 100 Event completed Timeout Event completed slept for 500 Exception in completing async context
I do not understand why this is happening. Please help! Thank you for your time.
PS: tomcat version - 7.0.37
source share