Search code examples
javavaadinthreadpoolvaadin7vaadin-session

How to provide current vaadin session to the old threads of thread pool?


I am facing a problem in understanding following quote of VaadinSession.getCurrent()

Gets the currently used session. The current session is automatically defined when processing requests to the server and in threads started at a point when the current session is defined (see InheritableThreadLocal). In other cases, (e.g. from background threads started in some other way), the current session is not automatically defined.

Specifically following point is difficult to understand for me,

... in threads started at a point when the current session is defined (see InheritableThreadLocal).

What does that mean ?

My understanding is if thread is created before the new Session has been defined or in the old session then it will not refer to the current newly created Session.

Currently I have Thread Pool and some threads are referring to the old session which is now closed then I will face the problem in using session in those threads.

I'm using spring with vaadin, specifically @Async method calls.

@Async
public static methodX() {
    //I have used session inside it
}

Problem is say, thread1 has been used to execute methodX, now I have used session session1 and after the user logout this session1 will be closed.

Now, when new user logs in to the system which has session2 and executes this method with thread1 again then this method has still used session1 instead of session2 and that creates problem when methods tries to fetch data from the closed session1.

My Questions :

  • Why Vaadin can not provide or notify old threads (i.e. threads which belongs to the old(closed) session) about the newly defined Session ?
  • What is the best way to provide the session to these threads ?

Solution

  • Problem is for background threads the VaadinSession(not to be confuse with HttpSession) is not available. After searching for this in book of vaadin I came to the conclusion with following solution.

    To get current session we have VaadinSession.getCurrent() and UI.getCurrent().getSession() both of them returns the session.

    Problem is old threads which are created during some other session are unable to access the current session as already stated in quote. This situation only raised with the @Async calls.

    @Async
    public void method() {
       //get id from session
    }
    

    Once application gets loaded the method can be used by multiple vaadin requests and each time thread of ThreadPool executes the task.

    Thread - 1 executed method - used session 1
    session 1 closed
    session 2 defined
    Thread - 1 again used to execute method
    Thread - 1 executed method - used session 1 instead of new defined session 2
    

    Now, in my case for each vaadin request we have new UI instance. But the UI the old thread was considering was obsolete for which session is closed.

    I have externally passed the UI instance to this asynchronous method, like this.

    @Async
    public void method(UI ui) {
       UI.setCurrent(ui);
       UI.getCurrent().getSession();//Now I have latest session
    }
    

    It was also stated in the documentation of UI.setCurrent(UI ui) method,

    The application developer can also use this method to define the current UI outside the normal request handling, e.g. when initiating custom background threads.