How to return a value from external task back to the engine?

Hello all,

I have implemented a java worker for completing service BPMN tasks.

What I need to achieve is to return a variable from the java worker code to the engine.

I can fetch a process variable (for example, the process variable “name”) into the java worker, like this:
String name= (String) externalTask.getVariable(“name”);

  1. how can I return back to the engine one variable that is defined inside the java worker code?
    Somthing like the following, it doesn’t work: externalTask.setVariable(“number”);

  2. Generally speaking about implementing a service BPMN task, is there any difference between implementation with a java class and an external worker? Since, I do not have technical background, they seem to me more or less the same. Is there any concrete distinction between them that can guide me what to select in each case that I face?

I would appreciate any comments and any help on these questions.

Thank you in advance.

External Workers are external programs that communication with camunda via REST-API.
JavaClasses have to be added in your camunda java-project and deployed (e.g. war file). They are used directly by the camunda-instance when needed.

So in case of an external worker you use this API to complete a task and in the same request you are able to append variables to attach (or update) in your process-instance:

Since external tasks don’t block any camunda-workerthreads, they are the recommended way for implementing long running tasks.

Have you tried this method?

Exchange Process & Local Task Variables

Ref set a process variable 'winning' below…


client.subscribe("topicName", async function({ task, taskService }) {
  // get the process variable 'score'
  const score = task.variables.get("score");

  // set a process variable 'winning'
  const processVariables = new Variables();
  processVariables.set("winning", score > 5);

  // set a local variable 'winningDate'
  const localVariables = new Variables();
  localVariables.set("winningDate", new Date());

  // complete the task
  await taskService.complete(task, processVariables, localVariables);
});

Hey @dcolley, thanks for your reply.

Is this working also for java code? or only for js?

thanks @KeyKon,

so with an external worker I need to use the complete external task API and sent my variables inside the request body?

If I use something like the following in a jave code worker, is it correct?

Map<String, Object> variables = new HashMap<String, Object>();
	         variables.put("message", message);
	         variables.put("trial", true);
	         externalTaskService.complete(externalTask, variables); 

Is it correct or do I need to use the API in every case?

I don’t know.
In what kind of program is this code executed, which classes did you import (what stands behind die externalTaskService-Object and it functions)?

I did just work with C# external task workers, and those just use REST-API (of course also behind helper functions)…

1 Like