I have a web application(using spring and vaadin) were i need to create a secondary thread to do some work(I cannot do this another way because of the existing code which i cannot change). In this thread i need access to the session. I found this useful answer:
Accessing scoped proxy beans within Threads of
Everything is ok but after the thread has started when i try to get the session i get null. I still get the session id though...
public RequestAwareRunnable() {
this.requestAttributes = RequestContextHolder.getRequestAttributes();
this.thread = Thread.currentThread();
String sessionID = requestAttributes.getSessionId();
HttpSession session = ((ServletRequestAttributes) requestAttributes).getRequest().getSession(false);//the session is OK
}
public void run() {
try {
String sessionID = requestAttributes.getSessionId();
HttpSession session = ((ServletRequestAttributes) requestAttributes).getRequest().getSession(false); // i get null
RequestContextHolder.setRequestAttributes(requestAttributes);
onRun();
} finally {
if (Thread.currentThread() != thread) {
RequestContextHolder.resetRequestAttributes();
}
thread = null;
}
}
Any ideas?