execution variable gets overridden in child sub-process

Hi All,
I have a probelem scenario ,
So i have a variable called PackageStateChanged , now just before starting a child-subprocess via correlating a message we set this variable inside a map and start correlationg
Sample code of correlation is below:

@Override
public void notifyPackageStateChanged(String logisticOrderId, PackageStateChangeNotification packageStateChangeNotification) {
Map<String, Object> processVariables = Map.of(“packageStateChanged”, Spin.JSON(packageStateChangeNotification),
X_IDEMPOTENCY_KEY, UUID.randomUUID().toString());

runtimeService.correlateMessage(CamundaMessage.EVENT_PACKAGE_STATE_CHANGED.name(),
logisticOrderId,
processVariables);
}

The moment child sub-process starts in my camunda modeller i have a script running on , pasting the sample code for the same below.
var packageStateChanged = JSON.parse(execution.getVariable(“packageStateChanged”));
var operationCode = packageStateChanged.packageStateChange.operationCode;
execution.setVariableLocal(“operationCode”, operationCode);
var packageState = packageStateChanged.packageStateChange.state;
execution.setVariableLocal(“packageState”, packageState);
execution.setVariableLocal(“packageStateChanged”, execution.getVariable(“packageStateChanged”));

Now the problem is say when i am correlating or starting the sub-process i am starting it multiple times so say at time T1 under PackageStateChangeNotification there is an attribute called as state having value as “IN_TRANSIT” and time T2 this state has value called as “DELIVERED” now when the child sub-process starts for “DELIVERED” somehow in the script due to concurrency the state also should have been updated to “DELIVERED” but it gets ovverridden by “IN_TRANSIT” .

Can anyone please help me with this , why this is happening and how to solve it.

Hi @19ajay97singh

Welcome to the forum.

Can you show the process (or relevant parts) to better understand the flow?

BR
Michael


Hi @mimaom
SO if you will observe the first screenshot , then when i click on Package State Changed you will find there is a script running for which i have attached the 2nd screenshot .
The content of the script is as below:
var packageStateChanged = JSON.parse(execution.getVariable(“packageStateChanged”));
var operationCode = packageStateChanged.packageStateChange.operationCode;
execution.setVariableLocal(“operationCode”, operationCode);
var packageState = packageStateChanged.packageStateChange.state;
execution.setVariableLocal(“packageState”, packageState);
execution.setVariableLocal(“packageStateChanged”, execution.getVariable(“packageStateChanged”));

So as i described in my last post when load comes up and concurrency occurs the state of packageStateChanged gets overridden.
What i am suspecting is because execution.setVariableLocal(“packageStateChanged”, execution.getVariable(“packageStateChanged”)); of this line as the variable that we are passing from Parent process is packageStateChanged and in child process i am again using same variable to set localvariable for execution so it should be replaced with
var subPackageStateChanged = execution.getVariable(“packageStateChanged”);
execution.setVariableLocal(“subPackageStateChanged”, subPackageStateChanged);
Let me know


For reference , attaching the 1st screenshot

Hi @19ajay97singh

OK, so your child process is an event sub process.

How is the code (notifyPackageStateChanged) that sends the correlation message called?

BR
Michael

Hi @mimaom

Yes the child process is an event sub process.

notifyPackageStateChanged it is called when x service sends events to my current service then we make use of this code to call child sub process. So basically that correlation part is written in a consumer.

Hi @19ajay97singh

OK, if I understand this correctly, you are using message correlation to trigger the start event of an embedded sub process. As part of the message correlation, you set the global process variable called “packageStateChanged”. Then a listener of the sub process message start event basically copies the value of the global process variable “packageStateChanged” to a local variable of the sub process.

But, this does not always work when load is high.
Sometimes, the local process variable “packageStateChanged” of the sub process, does not always have the same value as the one set, when the message correlation operation was called. Is that right?

Would it be possible for the child process to be a separate process (with its own definition)?
And instead of a message start event, you simply start a new process instance or it could be started using the call activity in the parent process to start a sub process?

BR
Michael

Hi @mimaom
But, this does not always work when load is high.
Sometimes, the local process variable “packageStateChanged” of the sub process, does not always have the same value as the one set, when the message correlation operation was called. Is that right?

This is absolutely right .