AnsweredAssumed Answered

Thread Safeness of Java Service Task

Question asked by timo.rohrberg on May 9, 2016
Latest reply on May 18, 2016 by jbarrez
Hello everybody,

we're using the Activiti engine for a while now in our enterprise application. Some of our Java Service Tasks are completely identical from the perspective of the source code. Their particular behavior just depends on the expressions injected via the <extensionElements> mechanism. We created a sample project available under the following link to simulate the setup we have:
https://drive.google.com/open?id=0B1Mh4R0LVb41YnZTVWZYaEVUdHc

Now, in the Activiti user guide, you can find the following sub-section in the section about Java Service Task implementation:

"Note: there will be only one instance of that Java class created for the serviceTask it is defined on. All process-instances share the same class instance that will be used to call execute(DelegateExecution). This means that the class must not use any member variables and must be thread-safe, since it can be executed simultaneously from different threads. This also influences the way Field injection is handled."

With that we're a bit concerned that we might run into a threading problem since we're re-using the same class for several Java Service Task in the workflow. Can it happen that one expression injection overrides another one from another execution of the same Java Service Task class?

When debugging into the source code, we noticed that several instances are created of the same Java Service Task class. Sometimes, the same instance is re-used, sometimes, it is instanciated again. We simply cannot identify the exact behavior of Activiti of when it instanciates the class and when not. However, is a private member variable of type Expression always thread-safe?

We highly appreciate any comment on this topic.

Thank you very much.

Regards
Timo Rohrberg

Outcomes