This document discusses a technically challenging task of maintaining awareness of the current logged in user across different components of a J2EE web service application implemented using EJBs. The solution involved extracting user information at the session boundary and saving it in a ThreadLocal user session object, which could then be accessed statically without polluting interfaces. An additional complexity was passing the user information to asynchronous worker threads, since ThreadLocal is not inherited by child threads - this required using InheritableThreadLocal and manually passing the data.
This document discusses a technically challenging task of maintaining awareness of the current logged in user across different components of a J2EE web service application implemented using EJBs. The solution involved extracting user information at the session boundary and saving it in a ThreadLocal user session object, which could then be accessed statically without polluting interfaces. An additional complexity was passing the user information to asynchronous worker threads, since ThreadLocal is not inherited by child threads - this required using InheritableThreadLocal and manually passing the data.
This document discusses a technically challenging task of maintaining awareness of the current logged in user across different components of a J2EE web service application implemented using EJBs. The solution involved extracting user information at the session boundary and saving it in a ThreadLocal user session object, which could then be accessed statically without polluting interfaces. An additional complexity was passing the user information to asynchronous worker threads, since ThreadLocal is not inherited by child threads - this required using InheritableThreadLocal and manually passing the data.
What are some technically challenging, interesting or cool tasks
that you did?
Keeping an EJB application be aware of the current user details One had a requirement where the most of the EJB application need to be aware of the current logged in user and some information about the user. This was a J2EE Webservice implemented using HTTP router module and EJB implementation. Well there is nothing like an HTTP session in EJB. You can use a Stateful session bean, but since this it is fronted by web-tier there is no way to keep the session across from the user to the Stateful session bean. Besides our requirement is to make the current user available to the entire application without polluting all interfaces with additional user info. What I ended up doing is to extract the user information at the session boundary layer and save it in a user session object based on ThreadLocal and expose is through a static method. EJB applications are multi user. A user session object saved in a request thread will be available everywhere in the current request scope and all classes can get it statically without polluting this information in the interfaces through out the call stack. Another problem was that we were doing some concurrent asynchronous calls to some external webservices using asynchronous worker threads. Asynchronous beans essentially use a pool of container threads that the application can use to accomplish some parallel processing since direct use of Threading is prohibited in J2EE container. State from ThreadLocal are not transferred to the child threads, so we need to use InheritableThreadLocal. But InheritableThreadLocal is not thread-safe and even this case the Worker thread does not clear out the state after going back to the pool. It happens that we need to manually pass the user information to these worker threads from the parent thread while initializing the worker threads.