This is unfortunate. Since the session is a local thread variable, it does not pass between new threads (which happens in your example). What is misleading and surprising is that when the code resumes after the wait method, there is a session variable (but this is a different instance).
I would say that this is a mistake - I expect the session context to be maintained around the wait call.
However, I understand why this is difficult. When you use wait, you are actually writing code in at least three threads. Front end, job call / asynchronous call and subsequent part. Watch her, it's awesome.
However, I agree that the session state for the request must be maintained, so I suggest you indicate the problem: https://play.lighthouseapp.com/projects/57987-play-framework/tickets/new
The following is a workaround that copies the session card, passing it through an asynchronous call. You could write a simple Job wrapper that always does this.
public static void test() { Logger.debug("before: Session.current() " + Session.current()); Session.current().put("key", new Date().toString()); Job<Session> async = new Job<Session>() { Session sessionPassed = Session.current(); @Override public Session doJobWithResult() throws Exception { Logger.debug("during job: Session.current() " + Session.current()); Logger.debug("during job: sessionPassed " + sessionPassed); Thread.sleep(1000L);
EDIT:
Alternatively, you can save the session map using Cache.set () - this is possibly cleaner than passing it.
As an aside, I rarely use a session to store user data. Each cookie (which is a session in the game) slows down your HTTP requests (read about how cookies work). I prefer to create a server-side map using a cache (e.g. Cache.set (session.getId (), userDataMap)). Obviously, each use case may differ, but I prefer this method to maintain the state of the user.
source share