The request associated with the AsyncContext has already completed processing.处理
程序员文章站
2022-07-02 09:39:02
...
前段时间在用servlet3.0时出现了一个问题,就是在servlet在异步处理数据时,由于时间超时,索然方法执行结束,但是后台抛出了异常,异常如下:
java.lang.IllegalStateException: The request associated with the AsyncContext has already completed processing. at org.apache.catalina.core.AsyncContextImpl.check(AsyncContextImpl.java:521) at org.apache.catalina.core.AsyncContextImpl.getResponse(AsyncContextImpl.java:245)
出现这个的原因是因为servlet默认的异步处理时间为10秒,显然由于数据量过大,导致时间超时。
解决办法也很简单只需要在servlet的doPost方法中加入一下一句
asyncContext.setTimeout(900000000);
为异步加载手动的设置超时时间即可,全部代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType(getServletContext().getInitParameter("content")); //进入异步模式,调用业务处理线程进行业务处理 //Servlet不会被阻塞,而是直接往下执行 //业务处理完成后的回应由AsyncContext管理 AsyncContext asyncContext = request.startAsync(); asyncContext.setTimeout(900000000); ProductHandleThread productHandleThread = new ProductHandleThread(asyncContext,request.getSession()); asyncContext.start(productHandleThread); }