在Spring mvc中以Servlet异步模式检索servletContext

时间:2014-05-13 13:35:19

标签: java spring spring-mvc servlets

我刚刚切换到在Spring mvc中使用Servlet Async,例如我有这个代码:

@RequestMapping("/report")
public Callable<Void> handleRequest() {
   return new Callable<Void>() {
    public Void call() throws Exception {
        handleRequestIntern();
        return null;
        }
    };
}

通常情况下,我有一个Filter,它将填充并清空一个ThreadLocal,其中包含http请求和响应,该代码将在代码中进一步用于检索登录成员等...

但是,如何在异步模式下执行此操作?我无法找到任何Spring mvc工具,以便轻松访问请求和响应。

注意:当然我可以传递Http请求和响应,但是在代码中,这不是Web代码,它将访问一个抽象包装器,它将使用ThreadLocal来读取登录成员之类的属性。 / p>

1 个答案:

答案 0 :(得分:0)

我做了CallableProcessingInterceptor的实现,并将它添加到spring config xml中。在运行回调之前和之后调用拦截器。

<mvc:annotation-driven>
  <mvc:async-support default-timeout="15000">
    <mvc:callable-interceptors>
    <!-- Required to ensure the http request and responsive are available -->
     <bean class="bla.ServletContextHolderAsyncInterceptor" />
    </mvc:callable-interceptors>
   </mvc:async-support>
</mvc:annotation-driven>

然后填充并清空我的ThreadLocal,在同步执行的情况下也通过Http Filter填充。

Spring通过一个固定的拦截器做类似的事情来填充它的RequestContextHolder类。

它也在Spring中用于Hibernate用法:打开/关闭hibernate会话使用,而不是在同步模式下使用OpenEntityManagerInViewFilter。 有关该问题的帖子:LINKE

相关问题