Search code examples
javamultithreadingsleepbusiness-process-managementbonita

Bonita BPM do not wait for Bonita's function end


I am creating a JAVA application that use Bonita BPM API, i am using a simple code like this:

ProcessInstance processInstance = App.getProcessAPI().startProcess(App.getProcessDefinitionId(),App.getListOperation(), listVariablesSerializable);
processId = processInstance.getId();
Thread.sleep(1000);
App.getBuilder().filter(ProcessInstanceSearchDescriptor.STARTED_BY, App.getAPISession().getUserId());
App.setPendingTasks(0, 30, null);

This code start a new process and set the new tasks to the user. But only works with Thread.sleep(1000) because App.setPendingTasks(0, 30, null) doesn't want to wait for App.getProcessAPI().startProcess(App.getProcessDefinitionId(),App.getListOperation(), listVariablesSerializable); to end.

I know that Thread.sleep(1000) is not a good programming practice and I need another solution please.


Solution

  • You are obviously experiencing a race condition between when the instance is created and the details of it are persisted to the database.

    Since you have the ProcessInstance returned from the start command. Why don't you simply retrieve the activities (tasks) for the instance directly using the ProcessAPI?

    List<ActivityInstance> activities = App.getProcessAPI().getActivities(processId, 0, 10);
    

    This API should use the current process state and not query the database.