Correlate message

Hi guys,

I try to get familiar with camunda and I would like to send a message from a sub process back to parent process so that the parent process continues. I have defined two processes. The parent process sends a message to the sub process which starts as soon as it receives the message. This works perfect. But the notification from the sub process back to the parent process doesn’t work.

Here are the two processes

This is my attempt:

  • the task “send message to sub process” passes in the ID of the parent process
  • the event “get callback from sub process” listens on a message called “callbackMessage”
  • the task “send callback” resolves the parent id and tried to correlate a message with the following code

String parentId = (String)execution.getVariable(“parentId”);
RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
//correlate the message
runtimeService.createMessageCorrelation(“callbackMessage”)
.processInstanceId(parentId)
.correlate();

The id of the parent process is available but the correlation throws an exception

Cannot correlate message ‘callbackMessage’: No process definition or execution matches the parameters

I read regarding this topic here and found that asynchronous might help. I enable it after the task “send message to sub process”, but without success.

Hope you can help me :slight_smile:

1 Like

To get started can you try using a expression?

See if you can get it to work using a expression in the same style as in the linked post.

Thanks for your reply…but this results in the same error. If I enable asynchrouns behaviour befor the task “send message to sub process”, the sub process will not start.

Can you use the bpmn file that is in the previously posted link and build a example from that example so you can see how messaging works.

Here is a example bpmn file that should get you started Camunda Message and signalling

Thanks stephen that solved my problem :slight_smile:

But I’m wondering if it only works with user tasks. If I replace the user tasks with service tasks and wait 1 second in the lower task, so that the upper process can continue to the gateway, the engine throws the “can not correlate message exception”. What am I doing wrong?

The order of when the process arrives at the receiving event/task and message delivery is important. Correlation only works when there is a process instance waiting for that specific message at the time of correlation. That’s why your initial solution (with and without async after “send message to sub process”) does not work. Regarding your second attempt, I guess by waiting for 1 second you mean Thread#sleep. This won’t help, as it only blocks the executing thread.

It should be fine when you add an asynchronous continuation in the subprocess, e.g. after the message start event. Then in the first transaction, the main process starts the subprocess until the asynchronous continuation is reached and then continues until the next wait state (here: event-based gateway). The second transaction delivers the callback message after that.

You can find some details on this behavior here: https://docs.camunda.org/manual/7.8/user-guide/process-engine/transactions-in-processes/

Cheers,
Thorben

3 Likes

Hi Thorben…thank you! Now I understand it :slight_smile:
The link was also very helpful!

I am facing same issue, but not getting how to implement it, Please can you provide example of it ?

Correlation only works when there is a process instance waiting for that specific message at the time of correlation.
But what if the process has not arrived at the receive activity yet (catching event or receive task) but the sender has already sent the message? We can’t control the world, i.e. when the messages are sent.

Is it possible to somehow mark the process so that the engine knows that it should receive a certain mesage (and hence correlate the message to it)?