我有一个Web应用程序,我在其中使用ScheduledThreadPoolExecutor来安排稍后执行的某些逻辑(可能在原始Web请求结束后)。是否可以在子线程内访问/使用原始请求的HttpServletRequest(通过ScheduledThreadPoolExecutor上的“schedule”调用创建)?
我尝试在其子函数在子线程中运行的类中自动装配HttpServletRequest,但是出现了以下错误(我理解其原因)
java.lang.IllegalStateException: No thread-bound request found: Are
you referring to request attributes outside of an actual web request,
or processing a request outside of the originally receiving thread? If
you are actually operating within a web request and still receive this
message, your code is probably running outside of
DispatcherServlet/DispatcherPortlet: In this case, use
RequestContextListener or RequestContextFilter to expose the current
request.
有没有办法? 可以使用RequestContextFilter吗?
答案 0 :(得分:0)
似乎你要完成的是让HTTP连接保持打开,而请求的处理是在一个单独的线程中异步处理的?
在这种情况下,你可能想看一下Spring MVC中的DefferedResult支持 - http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/context/request/async/DeferredResult.html - 这里也解释了 - http://java.dzone.com/articles/long-polling-spring-32s