线程安全spring mvc controller java.io.IOException: Stream closed异常

程序的流程是客户端发送一个Http post请求请求的内容是一个xml文件,服务器通过spring mvc 的controller接收
然后response 返回xml
@Controller
public class QueryController {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {

                    //业务处理.....
   }

}
类似上面的调用,间隔时间比较短的情况下会出现下面的异常,分析原因是Spring MVC的controller不是线程安全的,每次请求都是同一个servlet,当第一次请求的reponse或request关闭后,接下来的请求就会出现stream closed的情况。

尝试了几种方式:
1.用ThreadLocal管理request response 问题依然存在
2.尝试把处理写成无状态,线程安全的视乎很难
3.SingleThreadModel ,synchronized 管理好像也不行,感觉无法控制response和request对象。

现在的想法是把这个servlet变成线程安全,尝试了下视乎配置的不对,希望大家指点。

还有就是像这样的业务比较好的解决方法是什么,也请大家分享一下。

java.io.IOException: Stream closed
at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:406)
at org.apache.catalina.connector.CoyoteReader.read(CoyoteReader.java:105)
at org.apache.xmlbeans.impl.piccolo.xml.XMLReaderReader.fillCharBuffer(XMLReaderReader.java:247)
at org.apache.xmlbeans.impl.piccolo.xml.XMLReaderReader.reset(XMLReaderReader.java:84)
at org.apache.xmlbeans.impl.piccolo.xml.DocumentEntity.open(DocumentEntity.java:74)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.reset(PiccoloLexer.java:982)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:709)
at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3454)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1329)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1316)
at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:360)
at org.poc.pos.obervationQuery.QueryObservationRequestDocument$Factory.parse(Unknown Source)
at com.yuxin.referral.services.ObservationQueryServiceImpl.ObservationQueryRequest(ObservationQueryServiceImpl.java:79)
at com.yuxin.referral.services.ObservationQueryServiceImpl$$FastClassByCGLIB$$27758ac0.invoke()
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
at com.yuxin.referral.services.ObservationQueryServiceImpl$$EnhancerByCGLIB$$e00082b7.ObservationQueryRequest()
at com.yuxin.referral.web.ObservationQueryController.handleRequest(ObservationQueryController.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at com.springsource.metrics.collection.web.HttpRequestMetricCollectionValve.invoke(HttpRequestMetricCollectionValve.java:44)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:379)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)

1个回答

servlet 怎么可能线程安全呢。
不然怎么并发, response close 的应该有问题,你应该在close后还做了相关的operation。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问