1 Reply Latest reply on Nov 18, 2014 5:07 AM by Martin Kouba

    Update a Session in a different thread

    Geoffrey De Smet Newbie

      I have a long running task, which I submit to a ManagedExecutorService in WildFly 8.1, from a JAXRS REST call during a HTTP request.
      In that long running task, I need to update a session scoped bean from CDI (weld).
      How do I pass the session (or session id) into that Task and then update my session bean when the long running task has been run?

       

      A few days ago, I created a related StackOverflow question.

        • 1. Re: Update a Session in a different thread
          Martin Kouba Master

          Hi Geoffrey,

          you can't because the session context is only active during the service(), doFilter() and listeners notifications. I think you should really use the centralized location to store the results (as John Ament suggested), possibly an application scoped bean. As to the session timeout - you could simply implement a HttpSessionListener.sessionDestroyed(HttpSessionEvent) and remove all the results if necessary. I'm not sure about stopping a running task (this would require some further synchronization), but discarding the results would be simple.

           

          • use JAX-RS @Context HttpServletRequest to inject the current request (this must be supported on all servlet containers)
          • invoke HttpServletRequest.getSession(true) to be sure a new session is created if necessary
          • take the session id (HttpSession.getId()) and:
            • initialize the entry in the centralized store
            • pass this id to the SolverCallable
          • implement HttpSessionListener.sessionDestroyed() and remove the entry from the centralized store if necessary (timeout, logout, etc.)
          • at the end of the computation, check whether there is a relevant entry in the centralized store, if not, discard the result