The HttpSession
object itself can be used in multiple threads (but not thread-safe, and therefore must be synchronized). However, Spring does extra magic, for example. when you have session
-scoped beans. Namely, it uses ThreadLocal
from below to bind the current session to the thread.
I don't know what your exact scenario is, but apparently Spring is trying to extract the HttpSession
from this ThreadLocal
while you are in another thread that is obviously not working.
The solution is simple - extract the session attributes you need in @Async
and pass them directly. This, by the way, is a much better design - avoid skipping the HttpSession
object because it makes testing more difficult and your code is much less likely to be reused in the future.
source share