Question

I just switched to using Servlet Async in Spring mvc, for example I have this code:

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

Normally I have a Filter that will fill and empty a ThreadLocal with the http request and response that is used further in the code to retrieve the logged in member, etc...

However, how can I do this in Async mode? I can't find any Spring mvc tooling for this to make it easy to acess the Request and Response.

Note: of course I can pass in the Http request and response, but further in the code, which is not web code, it will access an abstract wrapper that will use the ThreadLocal to read properties like the logged in member.

Was it helpful?

Solution

I made a Implementation of CallableProcessingInterceptor and added it in the spring config xml like this. The interceptor is called just before and after running the callback.

<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>

It will then fill and empty my ThreadLocal that is also filled through a Http Filter in case of sync execution.

Spring does something similar through a fixed interceptor to fill it's RequestContextHolder class.

It's also used in Spring for Hibernate usage: open/close hibernate session usage instead of using the OpenEntityManagerInViewFilter in sync mode. A post on SO about that: LINKE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top