How to set a local variable in an external task in the scope of subprocess?

Hello there,

i am trying to set a local variable in the scope of a subprocess(“multipl jobs”) but don’t know how to accomplish that.
To provide details on this question i have built a simple BPMN model and a test-service that completes a service task as an external task:
image

@Named
public class TestTaskHandler implements ExternalTaskHandler {

@Override
public void execute(ExternalTask externalTask, ExternalTaskService externalTaskService) {

    try {

        final Map<String, Object> localVariables = new HashMap<>();
        localVariables.put("MY_DEPENDENT_JOB_ID", "some_id");

        externalTaskService.complete(externalTask, null, localVariables);
    } catch (Exception e) {

        externalTaskService.handleFailure(externalTask, "failed...",
                "failed...", retries, 60000);
    }
}

}

In this case the process starts with a collection so that the subprocess starts 3 “subinstances” with the following json:

{“variables”: {
“A_COLLECTION”: {“value”:[1,2,3]}
}}

The external task is then processing those instances of the subprocess and should set local variables in the scope of “multiple jobs” so that the next service task is able to read this variable for this explicit execution and not globally.
Till now i have tested the following possibilities:

  • complete the external task and set the variable as a normal variable. The result is that the variable exists globally (as expected)
  • complete the external task and set the variable as a local variable. The result is that the variable exists only in the scope of the external task so that the following task is not able to read this variable
  • the next try was to set an output on the diagram but the result of reading the local variable of the external task and setting this as the output leads to setting a variable globally again
  • then i tried to set a local variable at the execution point by getting the execution id of the external task and calling the rest-api. This also results in setting the variable in the scope of the external task and not being accessible for the following task

At this point i am totally confused how it is possible to set the variable locally for the subprocess as described and i hope someone is able to answer this question.

EDIT:
The first design of the BPMN model had a service task where i was able to complete the task with a local variable in the scope of “multiple jobs” as i wished (by just calling “execution.setVariableLocal([…])” in the JavaDelegate class). The problem was that this task was taking to much resources for the current machine so that we decided to process the task by another machine as an external task. That’s why i assume this feature of putting a local variable in the desired scope should be possible…

BPMN:
test.bpmn (5.7 KB)

Thanks!

You can make the variable local to the subprocess by defining an input parameter of that name on the subprocess itself. If the variable is written subsequently, it will only be set on the subprocess. Keep in mind that the variable won’t be available any more once the process is done.

thank you very much! by setting the variable as an input for the subprocess as you said i could complete the task without setting the variable as local but as global and it updated the variable just for the current “path” in the subprocess.

I also met the save question. My solution as follows,I am not sure If it is the best practice, while it actually realize it :
1.add an end executeableListener for the external Task which you will compete with adding a localVariable


2. In the process engine side,you should add a class with name you put on in first step like this:
pay attention to the SpinValues for json value; now you can increase the localVariable scope from present task to relevant subprocess ;

3. compete the external task with local variable you want . object type localVariable should be set to json string;