Own process instance can not be found inside a ServiceTask execution

落爺英雄遲暮 提交于 2019-12-11 01:54:36

问题


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.



来源:https://stackoverflow.com/questions/40419267/own-process-instance-can-not-be-found-inside-a-servicetask-execution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!