How to query process instance if it exist using Camunda Zeebe Java Client

In camunda 7 we could use history service and business key to check where a process instance with particular business key already exists for a process definition.

historyService.createHistoricProcessInstanceQuery()
                .processInstanceBusinessKey(businessKey)
                .tenantIdIn(tenantId)
                .processDefinitionKeyIn(definitionkey)
                .list();

How the same can be achieved using camunda 8 zeebe client?

Already referred zeebe client java doc and this forum post

Hello @Jason7 ,

welcome to the forum!

In Camunda 8, this kind of request would go to the Operate REST API.

The reason is that Zeebe can by design not expose the internal state. Also, it does not contain any historical data, only the runtime state.

One difficulty with this requirement could also be that you search by business key which does not exist in Camunda 8. Instead, you would use a variable you can refer to as business key.

I would suggest to use:

  1. GET /variables → query for the name and value of the business key variable
  2. GET /process-instances → query for the returned process instance keys from the first request with the bpmnProcessId

I know this is verbose right now, but we plan to allow searching for process instances by variables.

I hope this helps

Jonathan

Hey @jonathan.lukas,

Thanks for the response.

The requirement is to create a process instance provided a process instance with same business key does not exist.

For operate API POST /v1/variables/{key}, the challenge is it requires a key, based on which it searches. Not sure what this key represents, thought it was process instance key but trying on swagger did not return expected result.

For operate API POST /v1/variables/search, it will search the entire space which will be slower and more work would be required to meet the requirement.

Hello @Jason7 ,

the key in the first endpoint you mentioned is the variable instance key.

The second query is exactly the one you require. As the search is performed against an Elasticsearch database in the end, the query is still very fast.

Hint: If the requirement does only affect running process instances (do not start a process instance with the given business as long as there is another process instance running with the same), you can use the Single Instance messaging pattern:

I hope this helps

Jonathan

Hey @jonathan.lukas ,

Thanks for the reponse.

Unfortunately no the requirement requires only one business key per instance irrespective of whether the instance is running or finished.

Hello @Jason7 ,

in this case, you will have to use the proposed solution above.

As alternative, you could save the required information in a separate system as Operate has a limited data retention.

Jonathan