Unable to get updated variable value at runtime

I have a class that stores some metadata. I pass this variable as ProcessInstanceVariable at the process start. My process has only service tasks and each service task updates the metadata variable.

This is how I start the process:

Map<String, Object> processVariables = new HashMap<>();
processVariables.put("metadata", metadata);
runtimeService.startProcessInstanceByKey("myprocess", "mybusinesskey", processVariables);

Then, on each serviceTask, I get the variable, update the content and put it back to the execution:

@Override
public void execute(DelegateExecution execution) {
    Metadata metadata = (Metadata) execution.getVariable("metadata");
    metadata.foo = "new value";

    execution.setVariable(metadata);
}

When the process finishes, I am able to retrieve the variable by processId and variableName from the historic variables table (through the historyService). However, if the process is not yet finished and I try to get the variable using the runtimeService, the variable content is the initial and NOT the desired up-to-date content.

Is there any way I can get the variable with the up-to-date state when the process is still running?

Hi,

Since you mentioned that all you tasks are service task in process, and by the issue mentioned, i am guessing, all are sync tasks. Sync service tasks only commit the changes (updated variable values) when process reaches a wait state(example, some user task) or end state, thats why you are able to fetch updated variables value when the process ends, but if it is still running you will get only the initial value or not even that, i doubt bcoz when you do “runtimeService.startProcessInstanceByKey” and all the service task are sync, execution will only be returned after the process has ended. So if you want to fetch updated values, you can try making them async (if its suits your use case) as async commits the changes as soon as its done with the task.

Hope this helps.

Thank You,

Thanks! Your explanation was very helpful.