Invoking Receive Task from Callback via rest api

Hi,
I have a service task that opens a document for editing and after the document is finished, I do have a callback. Within the callback I need to call the Camunda Rest API to signal/trigger or whatever, that I want to continue with the process.

I choosed a receive task Dokument wurde editiert, and now I want to invoke it via Camunda Rest API. The name of the receive task is editingFinished. How can I do this?

Hi @Armin1231,

The process could look like this:

(for Schedule Work, it does not matter whether it is a send or service task)

And then you can use the message correlation API to trigger completion of the Work Done activity. For doing this via REST, see the following docs page: Deliver a Message | docs.camunda.org. Note that this API takes the name of the message the receive task waits for, not the id of the task itself.

A similar pattern, however with a single activity, is implemented here as an example: camunda-bpm-examples/servicetask/service-invocation-asynchronous at master · camunda/camunda-bpm-examples · GitHub

Cheers,
Thorben

Hi Thorben,
I already had a look over message tasks. My problem is, that it is not clear to me, how to use them.

Regarding the example:

{"messageName" : "aMessage",
"businessKey" : "aBusinessKey",
"correlationKeys" : {
    "aVariable" : {"value" : "aValue", "type": "String"}
},
"processVariables" : {
    "aVariable" : {"value" : "aNewValue", "type": "String"},
    "anotherVariable" : {"value" : true, "type": "Boolean"}
}
}

What is a business key?
I have searched for it in the forums and it says, that it must be defined by me. But why?
What do I need to add as correlationKeys variables?
Regarding my process, do I have to connect the Service Task Dokument editieren and Receive Task Dokument wurde editiert?

Thank you
Armin

Hi Armin,

On the correlation API: For an explanation what a business key is, see this forum post. You probably already read that post. In addition to that, a business key can be set when a process is started and setting it is optional.

In the correlation API, the paramters businessKey and correlationKeys can be used as criteria to restrict the correlation to process instance instances that match them.

Using the request body above, the request would correlate the message aMessageName to a process instance which has business key aBusinessKey and a variable named aVariable with value aValue but not to other process instances. Note that all parameters are optional, so you do not have to provide a businessKey or correlationKeys parameter for it to work.

On the process model: Yes, you should connect the two activities with sequence flow, since you only want the receive task to become active after Dokument editieren has been executed. If you do not connect the tasks, the BPMN semantics are that this activity is instantiated when the process starts. This would not be dynamic (i.e. would not work if multiple documents could be edited at the same time) and the process engine does not support that either.

Cheers,
Thorben

1 Like

Hi Thorben,
thank you. That clarified a lot.

Actually I am unsing jersey for the Rest call:

      String json = 
      "{\"messageName\" : \"editingFinished\"}";

        Response clientResponse = webTarget
                .path("message")
                .request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(json, MediaType.APPLICATION_JSON_TYPE));

After the call I have in the log the entry:

Cannot correlate message editingFinished with process instance business key null and correlation keys null: 2 executions match the correlation keys. Should be one or zero

Checking http://localhost:8080/engine-rest/execution?messageEventSubscriptionName=editingFinished

I do get 2 entry results.
So, as I understood you, the correlation and bussiness key stuff is used to make things unique and I need to add a business key or a correlation key to get unique results. Why can’t I just use the processInstanceId in combination with the messageName?

e.g:
String json =
“{“messageName” : “editingFinished”,” +
““correlationKeys” : {” +
““processInstanceId” : {“value” : “62cab194-11f1-11e6-9d96-5404a6c0f1f5”, “type”: “String”}” +
“}” +
“}”;

Then I do get in the logs:

Cannot correlate message editingFinished: No process definition or execution matches the parameters

Thank you
Armins

Hi Armin,

In fact you can, but not yet in 7.4.0. In 7.5.0 (to be released end of May), you’ll be able to use a processInstanceId parameter for message correllation (see the corresponding docs). This is already available in the latest 7.5.0-alpha4 release.

With 7.4.0, you could use a process variable to make correlation unique, e.g. set a variable to the process instance id and then use that.

Cheers,
Thorben

Hi Thorben,
sorry, I still don’t get it.
Can you please be a bit more precise what you mean by e.g. set a variable to the process instance id and then use that.

Do you mean via Rest API?

Armin

Hi Armin,

Sure. There’s various ways to set a process variable. This could either be from the outside (e.g. REST API or Java API RuntimeService) or from within process execution.

The latter may be more appropriate for this use case since it is not very convenient to set the variable for every instance from the outside. For example, you can use an execution listener to achieve this. Have a look at the documentation.

  • Create an implementation of the org.camunda.bpm.engine.delegate.ExecutionListener interface:
public class ExampleExecutionListener implements ExecutionListener {

    public void notify(DelegateExecution execution) throws Exception {
      String someGeneratedValue = ...;
      execution.setVariable("variableUsedForCorrelation", someGeneratedValue);
    }
  }
  • Register the listener in XML:
  <process id="executionListenersProcess">
    <extensionElements>
      <camunda:executionListener
          event="start"
          class="org.camunda.bpm.examples.bpmn.executionlistener.ExampleExecutionListener" />
    </extensionElements>
    
   ...
  </process>

(note that you can also define a listener in Camunda Modeler, so no XML fiddling is required)

  • Once the process instance is started, when you call the correlation API you can use the variable value for the correlationKeys parameter to identify the correct process instance.

The listener will be executed whenever a process instance is started for that process definition.

Cheers,
Thorben

Hi Thorben,
I have implemented now both Receive Tasks and I can invoke them both from the Callback via Rest API. I didn’t get, that I need to set a process variable and use the process variable key to access it via Correllation API. From the documentation I think with 7.5 things will get easier, cause the processInstanceId can be used.
Maybe I will update to 7.5 once it is there.

Thank you for your great answers.
Armin

@Armin1231 can you please post your solution for triggering Receive Tasks from camunda rest api?