cancel
Showing results for 
Search instead for 
Did you mean: 

ThreadLocal variables not local to current thread

bwestrich
Champ in-the-making
Champ in-the-making
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?

TIA,

Brian
1 REPLY 1

bwestrich
Champ in-the-making
Champ in-the-making
On further analysis, I realized this is NOT an issue with Activiti's handling of ThreadLocal variables, and as far as I can tell Activiti is working just fine with them. Instead, the problem occurred because of a coding error on my part: I had declared my Activiti listener beans as Spring singletons, but had not written the listeners in a threadsafe manner. Once I corrected the listeners, the problem was resolved. Sorry for the false alarm.