cancel
Showing results for 
Search instead for 
Did you mean: 

Service Task (JavaTaskDelegate) are implemented as Singleton ?

hardiku
Champ in-the-making
Champ in-the-making
Hi,

I am using Activiti 5.16. While reading documentation of service task i found one note about service task instances
"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. "

It confuses me. Looking at the code (DefaultActivitiBehaviour.java) it looks like Activiti creates service task instances for each task node definition. Does the above note means javaDelegates are implemented as Singleton ?

If so i think it takes away all the benefits of having custom service tasks per process definition.

Or am just thinking in all the wrong way ?
1 REPLY 1

frederikherema1
Star Contributor
Star Contributor
The actual behaviour is created for each service-task on a process-definition level. All process instances started for that process definition will use the same delegate object instance. This is because the definition is deployed once and cached by the process engine to be reused by all future process instances. Multiple serviceTask definitions in a process definitions (or across process definitions) that use the same activiti:class, will have a different instances creates.

If so i think it takes away all the benefits of having custom service tasks per process definition.

I thing you're not getting the concept right. You can make service-tasks behave differently by using Expressions. Each execution of that service-task will run in it's own execute() method, passing in the right DelegateExecution. That delegateExecution can be passed to the Expression fields in you're java-delegate, which result in values being returned based on the current execution. All other executions using this java delegate instance will use their own delegateExecution to get values out of the execution, making this reuse thread-safe. All member-fields you use NOT coming from Expressions, you should manage yourself to ensure thread-safety (eg. services than may be used from within a delegate call).