A Receive Task is a simple task that waits for the arrival of a certain message. When the process execution arrives at a Receive Task, the process state is committed to the persistence storage. This means that the process will stay in this wait state until a specific message is received by the engine, which triggers continuation of the process beyond the Receive Task.

Receive Task - 图1

A Receive Task with a message reference can be triggered like an ordinary event:

  1. <definitions ...>
  2. <message id="newInvoice" name="newInvoiceMessage"/>
  3. <process ...>
  4. <receiveTask id="waitState" name="wait" messageRef="newInvoice">
  5. ...

You can then either correlate the message:

  1. // correlate the message
  2. runtimeService.createMessageCorrelation(subscription.getEventName())
  3. .processInstanceBusinessKey("AB-123")
  4. .correlate();

Or explicitly query for the subscription and trigger it:

  1. ProcessInstance pi = runtimeService.startProcessInstanceByKey("processWaitingInReceiveTask");
  2. EventSubscription subscription = runtimeService.createEventSubscriptionQuery()
  3. .processInstanceId(pi.getId()).eventType("message").singleResult();
  4. runtimeService.messageEventReceived(subscription.getEventName(), subscription.getExecutionId());

Correlation of a parallel multi instance isn’t possible because the subscription can’t be identified unambiguously.

To continue a process instance that is currently waiting at a Receive Task without a message reference, the runtimeService.signal(executionId) can be called, using the id of the execution that arrived in the Receive Task.

  1. <receiveTask id="waitState" name="wait" />

The following code snippet shows how this works in practice:

  1. ProcessInstance pi = runtimeService.startProcessInstanceByKey("receiveTask");
  2. Execution execution = runtimeService.createExecutionQuery()
  3. .processInstanceId(pi.getId()).activityId("waitState").singleResult();
  4. runtimeService.signal(execution.getId());

Camunda Extensions

Attributes
camunda:asyncBefore, camunda:asyncAfter, camunda:exclusive, camunda:jobPriority
Extension Elements
——-
camunda:failedJobRetryTimeCycle, camunda:inputOutput
Constraints
——-
The camunda:exclusive attribute is only evaluated if the attribute camunda:asyncBefore or camunda:asyncAfter is set to true

Additional Resources

原文: https://docs.camunda.org/manual/7.9/reference/bpmn20/tasks/receive-task/