Parallel messaging with multi-instance

Hello! I am new to Camunda.

I have parallel multi-instance with Message Catch Event inside. I send messages with some variable to Camunda using localCorrelationKey. This idea i find in this topic Correlate message for multi instance task
Everything was fine when i sent messages in sequential way. But when i sent messages in parallel way OptimisticLockingException occurred. I understand this exception occurred because i try update variable (variable passed in message) concurrently.
So my question is how to avoid this exception? Maybe it is possible to pass variable in message as local variable for multi-instance? Or there is another way?

Thank you!

Hello @Raim ,

welcome to the forum!

The problem you are facing is something that happens from time to time.

My recommendation would be to create variables on a local scope and merge them later in an exclusive async job.

If you need more details ok this, please answer me and I can post a process model and some code.

I hope this helps

Jonathan

1 Like

Hello! Thank you for your reply!
Actually I have solved this problem with setting multi-instance as async before. But now I faced some problem with aggregating results of milti-instances in output collection variable. Sometimes nullPointerException on aggregation variable occurs. I think your recommendation can help me solve this problem, so can you please share your solution?

Hello @Raim ,

here is a working process example:

listDataJoin.bpmn (9.0 KB)

Key features:

  • the process generates a list of strings “A”, “B”, “C” using a script at start and saves it as process variable “dataFetchingOrder”
  • the process generates an empty list and saves it as process variable “fetchedData”
  • the “Data fetching” subprocess creates an empty variable container on each instance of the multi instance called “fetchedDataEntry”. This happens on “Inputs” in the subprocess
  • the service task can be executed async and NOT exclusive (so that it is really parallel)
  • on the end event of the subprocess, there is another async before that IS exclusive. This means that there will only be executed one instance of this job definition at a time.
  • on the end event of the subprocess, there is a script that takes the “fetchedDataEntry” from the subprocess scope and inserts it to the “fetchedData” list on the process scope

Tip: On Camunda 8, this is a core feature.

I hope this helps

Jonathan

2 Likes