How to check if camunda process is running?

I start camunda process:

final String MyProcessName = "my-process";
final String MyIdName = "my-id";
camundaProcessEngine.getRuntimeService()
                .startProcessInstanceByKey(MyProcessName , MyIdName );

after while another thread want to correlate the message to the running process:

camundaProcessEngine.getRuntimeService().createMessageCorrelation(MyIdName )
                .processInstanceBusinessKey(MyProcessName )
                .setVariable(myVariable.getKey(), myVariable.getValue())
                .correlateWithResult();

but when the process has already stopped the thread gets exceptions. It should first check if the process is alive before creating message correlation. How to do it?

Hi,

you can query the process-instances with POST /process-instance?processInstanceIds=your-id.

For further Information look in the Dokumentation

Best Regards,
Clemens

is there any solution for api? I woudl prefer something like:
camundaProcessEngine.getRuntimeService().isProcessInstanceAlive(myProcessName, myIdName)

Try this

getProcessEngineServices().getRuntimeService().createProcessInstanceQuery().processInstanceId("").active();

What about message correlation id?
If there are 10 instances, each with another correlationId I want to check not only if there is any instance running but want to check if specific instance with specific correlationId is working…

Then try this

 Execution ex = getRuntimeService().createExecutionQuery()
				    .processInstanceId("<Your-processInstance-ID>")
				    .messageEventSubscriptionName("<message-name>")
				    .singleResult();
2 Likes

This is perfect to check if there is a single instance running for .correlate() but now for .correlateAll() I would like to have possibility to check if there are > 1 instances. When I use the above solution for this case then it throws exception but I woold like to get rather true.

This is the answer:

boolean isAllCamundaProcessInstanceEnded(String instanceId, String messageName) {
    List<Execution> executions = getRuntimeService().createExecutionQuery()
				    .processInstanceId(instanceId)
				    .messageEventSubscriptionName(messageName)
				    .unlimitedList();
    return executions == null || executions.allMatch(Execution::isEnded);
}
1 Like