【问题标题】:retrieve servletContext in Servlet Async mode in Spring mvc在 Spring mvc 中以 Servlet 异步模式检索 servletContext
【发布时间】:2014-05-13 13:35:19
【问题描述】:

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

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

通常我有一个过滤器,它将使用 http 请求和响应填充和清空 ThreadLocal,该请求和响应在代码中进一步用于检索登录的成员等...

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

注意:当然我可以传入 Http 请求和响应,但在代码中,它不是 web 代码,它将访问一个抽象包装器,该包装器将使用 ThreadLocal 来读取登录成员等属性。

【问题讨论】:

    标签: java spring spring-mvc servlets


    【解决方案1】:

    我做了一个 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 过滤器填充。

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

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

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 2011-08-10
      • 2011-03-21
      • 2013-05-26
      • 2018-12-27
      • 2018-03-18
      • 2017-02-11
      • 2013-10-24
      • 1970-01-01
      相关资源
      最近更新 更多