AnsweredAssumed Answered

ThreadLocal variables not local to current thread

Question asked by bwestrich on Dec 30, 2011
Latest reply on Jan 2, 2012 by bwestrich
Hello Activiti gurus,

I'm trying to use thread local variables to pass information from my code that initially triggers events on a model to my Activiti ExecutionListener classes. I chose to use thread locals instead of process instance context variables because the ExecutionListener class in question is attached to the first task of the model, so at the time the listener fires Activiti hasn't yet persisted the process instance so it's too early to store this information as a process instance context variable.  I noticed a few other forum messages suggesting that others are also using or recommending use of ThreadLocal variables with Activiti.

This approach seems to work fine under light loads. But when more than one thread is making requests against the engine at the same time, the values of the ThreadLocal variables of one thread are affecting the values for other threads.  At first glance it seems that the first thread's values are preserved, while any subsequent threads (as long as there are multiple threads, that is) all assume the thread local variable values of the first thread.  

I could probably avoid using ThreadLocals if I reworked my models to add an initial state to them that would not have a listener on it. But I'd prefer not complicate my models this way if possible.

Has anyone else come across this issue when using ThreadLocal variables with Activiti? Is there some configuration parameter or other option I can change in Activiti that would allow me to work with ThreadLocal variables in multi-threaded situations?