JavaAPI wait until process is completed

I’m starting process instance using JavaAPI, I would like to then wait until process is completed (or, even preferably, reaches some state) and after that execute some more code, like that:

ProcessInstance instance = engine.getRuntimeService().startProcessInstanceByKey("my-process", variables);
//do something after process instance has finished

Is that possible in Camunda?

@mpawlowski if your whole process is executed in synchronous passion(without wait states), it’s possible to wait till completion of the process. Refer the below model for sync execution:

If your process is executed in asynchronous passion(also contains wait states), it’s not possible to wait till completion of the process. So at that time you will wait in the current wait state of process. By default all user task types will have wait state. In this scenario, you can’t wait until the process completes. Refer the below model for async execution:

Instead of waiting for the process to complete, you can try something like event-driven mechanism or notification process.

1 Like

Yeah, I forgot to mention, some of my tasks needs to be asynchronous. I guess I’ll need to rethink my approach. Thanks anyway.

@mpawlowski in the EndEvent of the process you can configure the ExecutionListener to notify that process is completed.

Listeners used for notification in camunda process are similar to Hollywood principle: Don’t Call Us, We’ll Call You!

As an additional bit of advice, you can also have an end listen on the process itself - so that if you have multiple end events or ways in which the process will end it will always trigger. You can see that in the properties panel when you select the canvas.

Of course you can wait! Just implement a busy waiting, i.e. wait for a period of time (sleep), then check the process state, then wait again if it’s not completed. It’s not very cool but it works well. Be sure to implement some timeout logics.