Is there a way to start only one process instance per business key

Viewed 599

I have been trying out the Camunda BPMN engine for a couple of days. Using the REST API, I have managed to start a process instance and associate it with a business key. However, I realized that it is possible to start multiple process instances under the same business key. Is there a way to enforce a condition such that only one process instance is allowed per business key per process definition?

Thank you.

2 Answers

One way is to check in the process (synchronously) if a process with this businessKey already exists when a process is started.

Here is a related example model which only allows one instance of the definition: https://raw.githubusercontent.com/rob2universe/process-models/master/bpmn/singleton.bpmn

The interesting part is the expression:

${historyService.createHistoricProcessInstanceQuery().processDefinitionKey(execution.getProcessDefinitionId().split(":")[0]).active().count() > 0}

You can change the filter criteria in the query to check the businessKey instead of the process definition key.

We use the following pattern to solve this: enter image description here This is an Event Sub Process.

Now Correlate Message, like

curl --location --request POST 'http://localhost:8080/engine-rest/message' \
--data-raw '{
    "messageName": "message",
    "businessKey": "my-unique-bk",
    "processVariables": {
    }
}'
  • First time: Starts the Process.
  • Second Time: Starts the Event Sub Process.
Related