在使用Servlet3.0的异步特性时,免不了会遇到下面这个异常
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代码如下,异步的逻辑在Executor中。
AsyncContext ctx = req.startAsync();
new Thread(new Executor(ctx)).start();
class Executor implements Runnable {
private AsyncContext ctx = null;
public Executor(AsyncContext ctx){
this.ctx = ctx;
}
public void run(){
try {
//等待三十秒钟,以模拟业务方法的执行
System.out.println("进入线程处理时间:"+new Date());
Thread.sleep(30000);
PrintWriter out = ctx.getResponse().getWriter();//这一步一般会抛出异常
System.out.println("结束线程处理时间");
out.println("业务处理完毕的时间:" + new Date() + ".");
out.flush();
ctx.complete();
} catch (Exception e) {
e.printStackTrace();
}
}
此时,由于应用服务器(Tomcat)的异步超时时间默认为10秒,
此代码位于Request.java类中。会获取通道配置的异步超时时间
asyncContext.setTimeout(getConnector().getAsyncTimeout());
而异步线程中会睡眠30秒,当30秒之后,会执行
ctx.getResponse().getWriter()
这个ctx.getResponse()方法会判断此时的request是否为空,代码如下:
@Override
public ServletResponse getResponse() {
check();
return servletResponse;
}
private void check() {
if (request == null) {
// AsyncContext has been recycled and should not be being used
throw new IllegalStateException(sm.getString(
"asyncContextImpl.requestEnded"));//此处即为抛出的异常
}
}
所以,为了避免该问题,方法有二:
一,在异步Servlet的代码中执行的逻辑时间要小于配置的异步超时时间,
二,在应用服务器中将该时间增大
分享到:
相关推荐
纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料 出这个错误一般就是下面2个.....
NULL 博文链接:https://hw1287789687.iteye.com/blog/1925191
异常:Caused by: java.lang.IllegalStateException: Method has too many Body parameters Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract ...
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but...
1. java.lang.IllegalStateException: No wrapped connection. 2.java.lang.IllegalStateException: Adapter is detached. 原因: 1.单线程一次执行一个请求可以正常执行,如果使用多线程,同时执行多个请求时就会...
NULL 博文链接:https://anminer.iteye.com/blog/845067
解决java.lang.IllegalStateException: unread block data的架包
spring 总的概述。。。入门好教程~~~~~~~~~~~~~
Type 异常报告 消息 Failed to convert ... nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Date': no matching editors or co
weblogic12c部署webservice错误问题WSSERVLET11: 无法解析运行时描述符: java.lang.IllegalStateException: MASM0001:
IllegalStateException: The specified child already has a parent.我的博客中有文章讲解
各种数据库在JDBC中的连接方法.及在struts与hibernate中的配置..
Cause: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.:java.lang.IncompatibleClassChangeError: class ...
因工作的缘故,有机会接触了Java本地方法调用(JNI)。本文中记下了初试JNI的过程和一点点体会。 使用Java的JNI调用C/C++的动态联结库有个固定的步骤,下面将以一个最简单的HelloWorld例子程序来说明调用过程。这个...
今天公司springboot项目准备部署到...java.lang.IllegalStateException: 启动子级时出错 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720) at org.apache.catalina.core.Con
里面包含了java通过xfire访问webservice用到的jar包,已经打包好了
主要介绍了java 实现websocket的两种方式实例详解,一种使用tomcat的websocket实现,一种使用spring的websocket,本文通过代码给大家介绍的非常详细,需要的朋友可以参考下
这个存储库提供了一种在处理片段传输和后台任务时避免“java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState”的方法。 您可以在的非常权威的阅读有关该问题和可能的解决方案...
SteganoImage 1.0 绿色版