executeWithVariablesInReturn vs. async service tasks

Hello,
I have some business code that calls a camunda process like in the following example.

		ProcessInstanceWithVariables withVariablesInReturn = runtimeService.createProcessInstanceByKey("process")
				.executeWithVariablesInReturn();
		VariableMap variableMap = withVariablesInReturn.getVariables();

So far, none of the service tasks in the process has a checked async before/after. The initial idea was to start a process, then different tasks store some results within the variable map of the process and afterwards access these variables outside of the camunda process in the business code to do some other stuff. This works fine as long as the process execution is synchronous.

Now, as I found out, that it is best practice to activate ‘async before’ at service tasks my approach doesn’t work anymore, because the variables that are stored after the first savepoint (async) are not accessible anymore.

So my first question is, am I right that my initial idea of using ‘executeWithVariablesInReturn’ with an synchronous execution of the process is generally a bad design?
Second question is, what would be the best solution? Should I try to move the business code that is outside the camunda process and that accesses the process variables into the camunda process or into a new parent-process, so that I don’t need ‘executeWithVariablesInReturn’ anymore?

Thanks very much for any advice!

Hi @peterg,

if you want the variables as a return value, you have to run the tasks synchronously.

You have to balance how to handle the failures.

With a synchronous process, the caller will get the exception of the first service that fails. If it’s OK for your use case, go for it.

You can also switch the task to asynchronous on demand if it fails. Have a look at this example: https://github.com/camunda-consulting/code/tree/master/snippets/async-on-error.

Hope this helps, Ingo

1 Like