Activiti调用式子流程

1.事物子流程

2.调用式子流程

需要写两个流程定义文件:

其中一个的调用事件和边界事件:

<callActivity id="callActivity" calledElement="my-process-check-order">
    <extensionElements>
        <activiti:in  source="errorflag" target="errorflag" />
        <activiti:out source="key1" target="key1" />
    </extensionElements>
</callActivity>

<boundaryEvent id="boundary" attachedToRef="callActivity">
    <errorEventDefinition errorRef="bpmnError"></errorEventDefinition>
</boundaryEvent>

第二个流程定义文件:

<process id="my-process-check-order">

    <startEvent id="start"/>
    <endEvent id="end"/>
    <parallelGateway id="parallelGateway1" name="并行网关1"/>
    <serviceTask id="pay" name="确认支付" activiti:class="com.syc.activiti.example.MyPayJavaDelegate"/>
    <serviceTask id="take" name="确认收货" activiti:class="com.syc.activiti.example.MyTakeJavaDelegate"/>
    <parallelGateway id="parallelGateway2" name="并行网关2"/>
    <...>
</process>

MyPayJavaDelegate类:

@Override
public void execute(DelegateExecution execution) {
    LOGGER.info("variables = {}", execution.getVariables());
    LOGGER.info("run my pay java delegate {}", this);
    execution.getParent().setVariableLocal("key2", "value2");
    execution.setVariable("key1", "value1");
    execution.setVariable("key3", "value3");
    Object errorflag = execution.getVariable("errorflag");
    if(Objects.equals(errorflag, true)){
        throw new BpmnError("bpmnError");
    }
    LOGGER.info("pay error");
}

测试:

@Test
@Deployment(resources = {"my-process-subprocess3.bpmn20.xml","my-process-subprocess4.bpmn20.xml"})
public void testSubProcess4(){
    Map<String, Object> variables = new HashMap<>();
    variables.put("errorflag", false);
    variables.put("key0", "value0");
    ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
    Task task = activitiRule.getTaskService().createTaskQuery().singleResult();
    LOGGER.info("task.name = {}",task.getName());
    Map<String, Object> variables1 = activitiRule.getRuntimeService().getVariables(processInstance.getId());
    LOGGER.info("variables1 = {}", variables1);
}

errorflag为false,正常执行:

errorflag改为true后的异常输出:

好处:将流程的复杂度降低,把流程定义文件拆分成多个小的文件

原创文章 97 获赞 43 访问量 1万+

猜你喜欢

转载自blog.csdn.net/shao_yc/article/details/105342640