Own process instance can not be found inside a Ser

2019-07-19 01:16发布

问题:

I have implemented a Camunda ServiceTask class (JavaDelegate) right after the start of my process. In this task I run a ProcessInstanceQuery for the own process instance (don't ask why). Surprisingly the query result is null - and I do not understand how this can happen.

To reproduce the problem I created a very simple demo project at GitHub.

This is the process

and this is my HelloWorldTask execution code

@Override
public void execute(DelegateExecution execution) throws Exception {

    String processInstanceId = execution.getProcessInstanceId();

    System.out.println(
            "Entering HelloWorldTask.execute (processInstanceId=" + processInstanceId + ")");

    ProcessInstanceQuery query =
            runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId);
    ProcessInstance pi = query.singleResult();

    if (pi == null) {
        System.out
                .println("WARN - No process instance with id " + processInstanceId + " found!");
    } else {
        System.out.println("Hello World!");
    }

    System.out.println(
            "Exiting HelloWorldTask.execute (processInstanceId=" + processInstanceId + ")");

}

There is a unit test hello.word.HelloWorldTest.helloWorld() which can be used to start the process. The output unfortunately is

Entering HelloWorldTask.execute (processInstanceId=4)
WARN - No process instance with id 4 found!
Exiting HelloWorldTask.execute (processInstanceId=4)

Can anyone explain this behaviour. Would be very helpful for me.

回答1:

Since you have no wait states the transaction is not comited, which means there is no process instance in the database, until the end of the process.

To access the process instance via your delegate execution simply use the execution.getProcessInstance() method. This method returns itself, if the delegate execution is currently the process instance. In your case it will be the same execution.

For more information about wait states and transaction boundaries in the Camunda Engine please see the documentation.



标签: java camunda