We are using @Async for multithreading. Untill each multithreading method i can see values for RequestContextHolder.getRequestAttributes().
But when i debug inside the method i'm getting request attributes as NULL.
Any thoughts?
We are using @Async for multithreading. Untill each multithreading method i can see values for RequestContextHolder.getRequestAttributes().
But when i debug inside the method i'm getting request attributes as NULL.
Any thoughts?
To get around this issue we created a ContextAwareRunnable
Object that was pre-populated with the current requestHolder, securityContextHolder, etc, so that all spawned threads would be able to execute as if it were running in the main thread.
By default ThreadLocal
variable is used as holder for request attributes. That means that only single thread which handles entire https request is able to access request attributes. In contrast @Async
methods are processed by threads from a separate thread pool so they can't access the attributes.
However there is one more InheritableThreadLocal
variable which could be used as request attributes holder instead for default one. You can enabled it by setting threadContextInheritable
property to true
in DispatcherServlet
or RequestContextFilter
.
Take a look at implementation of RequestContextHolder
for more details.