Thread-Safe Data Management Across Delegates

I have a Camunda engine on a web server which uses variables that are specific to the execution of that process at request time. However, I have Java delegates which make external asynchronous calls and thus may sleep and resume at random times.

If I have multiple instances of a process being executed, what is the recommended way to make sure the threads do not share data?

Each process instance has its own data scope. To ensure responses from async service invocation get correlated back to the correct process instance you should use the businessKey (if possible, supported by the external system). The businessKey is a special process data used to search and identify process instances. It should be unique at least across instances of the same process definition, but Camunda does not enforce its uniqueness. Typical examples would be business ids such as application number, support request number or claim id.

The APIs and web portals offer dedicated support for the businessKey, so it is easier to work with than a regular process data.

I hope i understood your question correctly. Does this answer it?

I think the question is if process instances share an instance of a delegate or each process instance has a dedicated instance of a delegate

Hi @Aniruddha_Raje,
A process might have finite number of activity types in which you can configure listeners and java delegates. So each activity(UserTask, ServiceTask, etc.) will have own instance of a delegates and listeners.

Consider the below example,

Lets say in both service tasks you’re updating a shared variable called “counter”, and when two service tasks executed in parallel, both the service tasks will try to synchronize at the join gateway, in that case if it’s not marked as exclusive it will throw an Optimistic locking exception.

For more details on asynchronous continuations refer the docs page.

Across multiple instances of the same process running concurrently, does the same activity, have an instance each of the same delegate?

If I have a process Flow P1 with Activity A1 and an attached delegate D1

If I have 10 java threads each instantiating and executing the same Process Flow P1, when at A1 do I have 10 instances of D1 or all threads share a single instance of D1

A1 will have 10 instances of D1.

Refer this post:

Thank you ! that’s helpful

If i am calling a Service task from different Process, is the Service Task/delegate thread-safe? Please give a look at the given example:
Analytic Event Handler.bpmn (2.9 KB) AssetEventHandler.bpmn (2.7 KB) Process Event Handler.bpmn (2.7 KB)
In all the three process I am calling the same Service Task, in which I am setting some process Variables, do yout hink this is thread-safe? If not, how can i make it thread-safe!

Hi @satyaram413,

according to this page of the Docs: a new object instance is created for each delegate invocation.

Yes, you can use a single delegate in several processes.

More important than the process models is the implementation of your Delegate class.

Hope this helps, Ingo

Hey @Ingo_Richtsmeier,
Thanks for your response, If i am trying to get/set process variables inside the delegate class, would that be thread-safe driven?