Flowable事件之结束事件

Flowable事件之结束事件
在这里插入图片描述

Flowable结束事件

  结束事件顾名思义就是流程结束的事件,除了前面遇到的空结束事件外,结束事件还包括如下几种:

  • 错误结束事件
  • 中断结束事件
  • 取消结束事件

1.错误结束事件

  当流程执行到达错误结束事件(error end event)时,结束执行的当前分支,并抛出错误。这个错误可以由匹配的错误边界中间事件捕获。如果找不到匹配的错误边界事件,将会抛出异常。通过具体案例来详细讲解:

Flowable事件之结束事件
在这里插入图片描述

完整的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef" exporter="Flowable Open Source Modeler" exporterVersion="6.7.2">
  <error id="error03" errorCode="error03" ></error>
  <process id="event5001" name="错误结束事件" isExecutable="true">
    <startEvent id="start01" name="开始任务" flowable:formFieldValidation="true"></startEvent>
    <subProcess id="sid-794CA748-4381-417F-8350-360E89907E84" name="subProcess">
      <startEvent id="startc01" name="开始子流程" flowable:formFieldValidation="true"></startEvent>
      <serviceTask id="task01" name="自动任务一" flowable:class="com.bobo.flow.delegate.MyOneJavaDelegate"></serviceTask>
      <exclusiveGateway id="p001" name="排他网关"></exclusiveGateway>
      <endEvent id="endc01" name="子流程结束"></endEvent>
      <endEvent id="errorend01" name="错误结束事件">
        <errorEventDefinition errorRef="error03" flowable:errorVariableLocalScope="true" flowable:errorVariableTransient="true"></errorEventDefinition>
      </endEvent>
      <sequenceFlow id="sid-1B2782FA-5216-4817-B964-A77EDEBB7547" sourceRef="startc01" targetRef="task01"></sequenceFlow>
      <sequenceFlow id="sid-D2196400-20DC-4D6B-AAD2-AA9308CE8DEA" sourceRef="task01" targetRef="p001"></sequenceFlow>
      <sequenceFlow id="sid-DEC53745-1E74-4D08-B472-95F9A6B92D98" sourceRef="p001" targetRef="errorend01">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag<=0}]]></conditionExpression>
      </sequenceFlow>
    </subProcess>
    <sequenceFlow id="sid-934F4EDF-16A9-4D4E-8D7A-4F8E1EEB0F9E" sourceRef="start01" targetRef="sid-794CA748-4381-417F-8350-360E89907E84"></sequenceFlow>
    <serviceTask id="task2" name="自动任务二" flowable:class="com.bobo.flow.delegate.MyTwoJavaDelegate"></serviceTask>
    <sequenceFlow id="sid-E8166885-00F6-49A1-BE26-3AB98FE95455" sourceRef="sid-794CA748-4381-417F-8350-360E89907E84" targetRef="task2"></sequenceFlow>
    <endEvent id="end02" name="主流程结束"></endEvent>
    <sequenceFlow id="sid-496A30AE-44AC-4298-83E0-3183F3FF935B" sourceRef="task2" targetRef="end02"></sequenceFlow>
    <boundaryEvent id="perror01" name="边界错误事件" attachedToRef="sid-794CA748-4381-417F-8350-360E89907E84">
      <errorEventDefinition errorRef="error03" flowable:errorVariableLocalScope="true" flowable:errorVariableTransient="true"></errorEventDefinition>
    </boundaryEvent>
    <sequenceFlow id="sid-9562301E-B400-4D1F-89B8-23E69ADEAE68" sourceRef="perror01" targetRef="task3"></sequenceFlow>
    <serviceTask id="task3" name="自动任务三" flowable:class="com.bobo.flow.delegate.MyThreeJavaDelegate"></serviceTask>
    <endEvent id="end003" name="主流程结束"></endEvent>
    <sequenceFlow id="sid-436E29BF-7F5E-4A4B-8F62-C0AEA010F4D2" sourceRef="task3" targetRef="end003"></sequenceFlow>
    <sequenceFlow id="sid-C17C73A1-5853-496B-B686-E7911105D459" sourceRef="sid-794CA748-4381-417F-8350-360E89907E84" targetRef="endc01">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag>0}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_event5001">
    <bpmndi:BPMNPlane bpmnElement="event5001" id="BPMNPlane_event5001">
      <bpmndi:BPMNShape bpmnElement="start01" id="BPMNShape_start01">
        <omgdc:Bounds height="30.0" width="30.0" x="75.0" y="205.5"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-794CA748-4381-417F-8350-360E89907E84" id="BPMNShape_sid-794CA748-4381-417F-8350-360E89907E84">
        <omgdc:Bounds height="261.0" width="593.0" x="240.0" y="90.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="startc01" id="BPMNShape_startc01">
        <omgdc:Bounds height="30.0" width="30.0" x="285.0" y="199.5"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task01" id="BPMNShape_task01">
        <omgdc:Bounds height="80.0" width="100.0" x="418.5" y="174.5"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="p001" id="BPMNShape_p001">
        <omgdc:Bounds height="40.0" width="40.0" x="563.5" y="194.5"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endc01" id="BPMNShape_endc01">
        <omgdc:Bounds height="28.0" width="28.0" x="675.0" y="150.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="errorend01" id="BPMNShape_errorend01">
        <omgdc:Bounds height="28.0" width="28.0" x="675.0" y="255.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task2" id="BPMNShape_task2">
        <omgdc:Bounds height="80.0" width="100.0" x="942.5" y="176.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end02" id="BPMNShape_end02">
        <omgdc:Bounds height="28.0" width="28.0" x="1087.5" y="202.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="perror01" id="BPMNShape_perror01">
        <omgdc:Bounds height="30.0" width="30.0" x="478.02252392098524" y="336.450017237985"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task3" id="BPMNShape_task3">
        <omgdc:Bounds height="80.0" width="100.0" x="665.5" y="406.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end003" id="BPMNShape_end003">
        <omgdc:Bounds height="28.0" width="28.0" x="810.5" y="432.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-934F4EDF-16A9-4D4E-8D7A-4F8E1EEB0F9E" id="BPMNEdge_sid-934F4EDF-16A9-4D4E-8D7A-4F8E1EEB0F9E" flowable:sourceDockerX="15.0" flowable:sourceDockerY="15.0" flowable:targetDockerX="296.5" flowable:targetDockerY="130.5">
        <omgdi:waypoint x="104.9499999082861" y="220.5"></omgdi:waypoint>
        <omgdi:waypoint x="240.0" y="220.5"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-9562301E-B400-4D1F-89B8-23E69ADEAE68" id="BPMNEdge_sid-9562301E-B400-4D1F-89B8-23E69ADEAE68" flowable:sourceDockerX="15.0" flowable:sourceDockerY="15.0" flowable:targetDockerX="1.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="493.02252392098524" y="366.40001519452954"></omgdi:waypoint>
        <omgdi:waypoint x="493.02252392098524" y="446.0"></omgdi:waypoint>
        <omgdi:waypoint x="665.4999999999623" y="446.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-E8166885-00F6-49A1-BE26-3AB98FE95455" id="BPMNEdge_sid-E8166885-00F6-49A1-BE26-3AB98FE95455" flowable:sourceDockerX="296.5" flowable:sourceDockerY="130.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="832.9499999999987" y="217.57401315789474"></omgdi:waypoint>
        <omgdi:waypoint x="942.499999999999" y="216.49292763157894"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-436E29BF-7F5E-4A4B-8F62-C0AEA010F4D2" id="BPMNEdge_sid-436E29BF-7F5E-4A4B-8F62-C0AEA010F4D2" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="765.4499999999999" y="446.0"></omgdi:waypoint>
        <omgdi:waypoint x="810.5" y="446.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-D2196400-20DC-4D6B-AAD2-AA9308CE8DEA" id="BPMNEdge_sid-D2196400-20DC-4D6B-AAD2-AA9308CE8DEA" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="20.5" flowable:targetDockerY="20.5">
        <omgdi:waypoint x="518.4499999999978" y="214.71623376623376"></omgdi:waypoint>
        <omgdi:waypoint x="563.9130434782609" y="214.91304347826087"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-496A30AE-44AC-4298-83E0-3183F3FF935B" id="BPMNEdge_sid-496A30AE-44AC-4298-83E0-3183F3FF935B" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="1042.449999999996" y="216.0"></omgdi:waypoint>
        <omgdi:waypoint x="1087.5" y="216.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-C17C73A1-5853-496B-B686-E7911105D459" id="BPMNEdge_sid-C17C73A1-5853-496B-B686-E7911105D459" flowable:sourceDockerX="343.3984375" flowable:sourceDockerY="106.0" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="583.3984375" y="196.0"></omgdi:waypoint>
        <omgdi:waypoint x="583.3984375" y="164.0"></omgdi:waypoint>
        <omgdi:waypoint x="675.0" y="164.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-1B2782FA-5216-4817-B964-A77EDEBB7547" id="BPMNEdge_sid-1B2782FA-5216-4817-B964-A77EDEBB7547" flowable:sourceDockerX="15.0" flowable:sourceDockerY="15.0" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="314.949999356254" y="214.5"></omgdi:waypoint>
        <omgdi:waypoint x="418.5" y="214.5"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-DEC53745-1E74-4D08-B472-95F9A6B92D98" id="BPMNEdge_sid-DEC53745-1E74-4D08-B472-95F9A6B92D98" flowable:sourceDockerX="20.8984375" flowable:sourceDockerY="36.5" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="584.3984375" y="233.54821399278214"></omgdi:waypoint>
        <omgdi:waypoint x="584.3984375" y="269.0"></omgdi:waypoint>
        <omgdi:waypoint x="675.0" y="269.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

定义的error

<error id="error01" errorCode="error01"></error>

在子流程中,当flag的数据为0的时候,就会触发错误结束事件

      <sequenceFlow id="1111" sourceRef="12222" targetRef="xxxxx44">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag==0}]]></conditionExpression>
      </sequenceFlow>

然后我们在子流程上绑定了一个错误边界事件,绑定的也是error01,也就是当子流程触发错误结束事件,就会触发这个边界事件,进而完成自动任务三。

流程中关联的三个自动任务都只是输出一个打印语句~

然后我们需要做的操作有,部署,启动流程绑定流程变量flag的值为0,然后就可以看输出结果了

    /**
    * 部署
    */

    @Test
    public void test02() throws Exception{

        Deployment deployment = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource("错误结束事件.bpmn20.xml")
                .name("错误结束事件")
                .deploy();
        System.out.println("-----");
    }

然后启动流程

    /**
     * 启动流程实例,设置流程变量中的flag=0触发错误结束事件
     */

    @Test
    public void startProcessInstanceByKey()  throws Exception{
        // 设置对应的流程变量的值
        Map<String,Object> map = new HashMap<>();
        map.put("flag",0);// 设置flag为0触发流程结束事件
        processEngine.getRuntimeService()
                .startProcessInstanceById("event2005:1:ebec87e2-b028-11ec-b93f-c03c59ad2248",map);
        System.out.println("开始启动的时间:" + LocalDateTime.now().toString());
        // 需要在此阻塞比等待长的时间
        TimeUnit.MINUTES.sleep(3);
    }

看控制台输出

Flowable事件之结束事件
在这里插入图片描述

控制台输出的结果和我们预期的是一样的

Flowable事件之结束事件
在这里插入图片描述

说明错误结束事件的作用就是在执行到错误结束的节点位置会抛出对应的错误,供需要获取的事件来处理。

2.中断结束事件

  中断结束事件也称为终止结束事件,主要是对流程进行终止的事件,可以在一个复杂的流程中,如果某方想要提前中断这个流程,可以采用这个事件来处理,可以在并行处理任务中。如果你是在流程实例层处理,整个流程都会被中断,如果是在子流程中使用,那么当前作用和作用域内的所有的内部流程都会被终止。具体还是通过两个案例来给大家介绍:

2.1 案例一

  案例一我们介绍没有子流程的情况下终止的场景,具体案例如下

Flowable事件之结束事件
在这里插入图片描述

完整的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef" exporter="Flowable Open Source Modeler" exporterVersion="6.7.2">
  <process id="event5002" name="终止结束事件01" isExecutable="true">
    <startEvent id="start01" name="开始事件" flowable:formFieldValidation="true"></startEvent>
    <parallelGateway id="p001" name="并行网关"></parallelGateway>
    <parallelGateway id="p004" name="并行网关"></parallelGateway>
    <userTask id="task2" name="用户任务二" flowable:assignee="lisi" flowable:formFieldValidation="true">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-E3A5580F-4CE3-4556-894E-46C3D2134071" sourceRef="p001" targetRef="task2"></sequenceFlow>
    <userTask id="task1" name="用户任务一" flowable:assignee="zhangsan" flowable:formFieldValidation="true">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-FF8C06ED-AE40-46A1-A898-9FA4B0D4665A" sourceRef="p001" targetRef="task1"></sequenceFlow>
    <userTask id="task3" name="用户任务三" flowable:assignee="wangwu" flowable:formFieldValidation="true">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-226E06CD-07E8-4E38-A3FC-12CD417F54FC" sourceRef="p001" targetRef="task3"></sequenceFlow>
    <sequenceFlow id="sid-C1280677-F8B9-4C9C-B010-CEEA4DE17176" sourceRef="start01" targetRef="p001"></sequenceFlow>
    <sequenceFlow id="sid-F5ABC1D7-091F-4966-990F-44CE1A927869" sourceRef="task1" targetRef="p004"></sequenceFlow>
    <sequenceFlow id="sid-E1E26394-8B33-4766-9B3B-722958B91972" sourceRef="task2" targetRef="p004"></sequenceFlow>
    <endEvent id="end003" name="结束事件"></endEvent>
    <sequenceFlow id="sid-074345F1-5C34-435D-AECE-912DDBBBE105" sourceRef="p004" targetRef="end003"></sequenceFlow>
    <exclusiveGateway id="p003" name="排他网关"></exclusiveGateway>
    <endEvent id="sid-E9A4380A-AAF8-464C-B14B-15A2C1F1C2D2" name="终止结束事件">
      <terminateEventDefinition></terminateEventDefinition>
    </endEvent>
    <sequenceFlow id="sid-D00B6EDC-F2F4-4D8F-89BD-91272363CD49" sourceRef="task3" targetRef="p003"></sequenceFlow>
    <sequenceFlow id="sid-325962BF-7168-48C0-9107-B232A4627C10" sourceRef="p003" targetRef="sid-E9A4380A-AAF8-464C-B14B-15A2C1F1C2D2">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag<=0}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-413A98AF-8CAF-470C-B751-E32229801310" sourceRef="p003" targetRef="p004">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag>0}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_event5002">
    <bpmndi:BPMNPlane bpmnElement="event5002" id="BPMNPlane_event5002">
      <bpmndi:BPMNShape bpmnElement="start01" id="BPMNShape_start01">
        <omgdc:Bounds height="30.0" width="30.0" x="102.5" y="253.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="p001" id="BPMNShape_p001">
        <omgdc:Bounds height="40.0" width="40.0" x="287.5" y="248.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="p004" id="BPMNShape_p004">
        <omgdc:Bounds height="40.0" width="40.0" x="720.0" y="248.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task2" id="BPMNShape_task2">
        <omgdc:Bounds height="80.0" width="100.0" x="372.5" y="228.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task1" id="BPMNShape_task1">
        <omgdc:Bounds height="80.0" width="100.0" x="372.5" y="105.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task3" id="BPMNShape_task3">
        <omgdc:Bounds height="80.0" width="100.0" x="377.5" y="390.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end003" id="BPMNShape_end003">
        <omgdc:Bounds height="28.0" width="28.0" x="805.0" y="254.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="p003" id="BPMNShape_p003">
        <omgdc:Bounds height="40.0" width="40.0" x="570.0" y="410.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-E9A4380A-AAF8-464C-B14B-15A2C1F1C2D2" id="BPMNShape_sid-E9A4380A-AAF8-464C-B14B-15A2C1F1C2D2">
        <omgdc:Bounds height="28.0" width="28.0" x="576.0" y="495.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-E1E26394-8B33-4766-9B3B-722958B91972" id="BPMNEdge_sid-E1E26394-8B33-4766-9B3B-722958B91972" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="7.5" flowable:targetDockerY="19.0">
        <omgdi:waypoint x="472.4499999999732" y="268.0"></omgdi:waypoint>
        <omgdi:waypoint x="596.25" y="268.0"></omgdi:waypoint>
        <omgdi:waypoint x="596.25" y="267.0"></omgdi:waypoint>
        <omgdi:waypoint x="720.9525123715264" y="267.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-226E06CD-07E8-4E38-A3FC-12CD417F54FC" id="BPMNEdge_sid-226E06CD-07E8-4E38-A3FC-12CD417F54FC" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="308.0" y="287.4441367574257"></omgdi:waypoint>
        <omgdi:waypoint x="308.0" y="430.0"></omgdi:waypoint>
        <omgdi:waypoint x="377.5" y="430.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-325962BF-7168-48C0-9107-B232A4627C10" id="BPMNEdge_sid-325962BF-7168-48C0-9107-B232A4627C10" flowable:sourceDockerX="19.53125" flowable:sourceDockerY="34.5" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="589.5677159590043" y="449.51771595900436"></omgdi:waypoint>
        <omgdi:waypoint x="589.8981518165715" y="495.0001397608249"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-F5ABC1D7-091F-4966-990F-44CE1A927869" id="BPMNEdge_sid-F5ABC1D7-091F-4966-990F-44CE1A927869" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="20.0" flowable:targetDockerY="1.0">
        <omgdi:waypoint x="472.44999999982196" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="740.0" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="740.0" y="248.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-D00B6EDC-F2F4-4D8F-89BD-91272363CD49" id="BPMNEdge_sid-D00B6EDC-F2F4-4D8F-89BD-91272363CD49" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="7.0" flowable:targetDockerY="22.0">
        <omgdi:waypoint x="477.449999999998" y="430.66822742474915"></omgdi:waypoint>
        <omgdi:waypoint x="571.9322033898305" y="431.93220338983053"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-E3A5580F-4CE3-4556-894E-46C3D2134071" id="BPMNEdge_sid-E3A5580F-4CE3-4556-894E-46C3D2134071" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="327.0247370727432" y="268.4166666666667"></omgdi:waypoint>
        <omgdi:waypoint x="372.49999999998806" y="268.2181222707423"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-074345F1-5C34-435D-AECE-912DDBBBE105" id="BPMNEdge_sid-074345F1-5C34-435D-AECE-912DDBBBE105" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="759.5591869398207" y="268.37820512820514"></omgdi:waypoint>
        <omgdi:waypoint x="805.0002755524882" y="268.08885188426405"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-C1280677-F8B9-4C9C-B010-CEEA4DE17176" id="BPMNEdge_sid-C1280677-F8B9-4C9C-B010-CEEA4DE17176" flowable:sourceDockerX="15.0" flowable:sourceDockerY="15.0" flowable:targetDockerX="20.0" flowable:targetDockerY="20.0">
        <omgdi:waypoint x="132.44999949366624" y="268.0"></omgdi:waypoint>
        <omgdi:waypoint x="287.5" y="268.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-413A98AF-8CAF-470C-B751-E32229801310" id="BPMNEdge_sid-413A98AF-8CAF-470C-B751-E32229801310" flowable:sourceDockerX="37.5" flowable:sourceDockerY="20.4375" flowable:targetDockerX="22.5" flowable:targetDockerY="31.0">
        <omgdi:waypoint x="609.5117551813031" y="430.4375"></omgdi:waypoint>
        <omgdi:waypoint x="742.5" y="430.4375"></omgdi:waypoint>
        <omgdi:waypoint x="742.5" y="285.402097959858"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-FF8C06ED-AE40-46A1-A898-9FA4B0D4665A" id="BPMNEdge_sid-FF8C06ED-AE40-46A1-A898-9FA4B0D4665A" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="308.0" y="248.5"></omgdi:waypoint>
        <omgdi:waypoint x="308.0" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="372.5" y="145.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

流程定义说明:并行网关中3个人工审核任务,分别分配的用户为zhangsan lisi wangwu ,然后在wangwu审核后会进入一个排他网关,如果flag是小于等于0就会触发终止结束事件。

<sequenceFlow id="sid-89945688-1D51-4391-A713-3E77ADB2FA06" sourceRef="p001" targetRef="stop001">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag<=0}]]></conditionExpression>
    </sequenceFlow>

具体操作:部署流程–>启动流程实例–>wanwu 处理任务【流程实例 flag <= 0】 触发任务

    @Test
    public void test02() throws Exception{

        Deployment deployment = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource("终止流程1.bpmn20.xml")
                .name("终止流程1")
                .deploy();
        System.out.println("-----");
    }

启动流程实例

    @Test
    public void startProcessInstanceByKey()  throws Exception{
        // 设置对应的流程变量的值
        Map<String,Object> map = new HashMap<>();
        map.put("flag",0);// 设置flag为0触发流程结束事件
        processEngine.getRuntimeService()
                .startProcessInstanceById("event3001:1:8b1663fa-b02d-11ec-b480-c03c59ad2248",map);
    }

这时可以看到三个并行的Task任务:

Flowable事件之结束事件
在这里插入图片描述

这时我们只需王五来处理即可,在上一步中我们已经对流程变量赋值了

    @Test
    public void completeTask(){
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionId("event3001:1:8b1663fa-b02d-11ec-b480-c03c59ad2248")
                .taskAssignee("wangwu")
                .singleResult();
        taskService.complete(task.getId());
    }

然后再去看Task表中已经没有另外两条记录了哦Flowable事件之结束事件

通过案例可以看到在没有子流程的情况下,终止结束事件会把整个流程都进程都结束了,而且在历史记录中也可以看到信息

Flowable事件之结束事件
在这里插入图片描述

2.2 案例二

  然后我们来看看在子流程中触发终止结束事件的案例:

Flowable事件之结束事件
在这里插入图片描述

完整的xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef" exporter="Flowable Open Source Modeler" exporterVersion="6.7.2">
  <process id="event3002" name="终止流程2" isExecutable="true">
    <startEvent id="start01" name="任务开始" flowable:formFieldValidation="true"></startEvent>
    <userTask id="task2" name="用户任务二" flowable:assignee="lisi" flowable:formFieldValidation="true">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-03E93AD6-F173-454F-B29F-B61FE45BE1E0" sourceRef="b001" targetRef="task2"></sequenceFlow>
    <userTask id="task1" name="用户任务一" flowable:assignee="zhangsan" flowable:formFieldValidation="true">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-A4455D52-7DAF-4193-9E4F-D75E133A6EDD" sourceRef="b001" targetRef="task1"></sequenceFlow>
    <subProcess id="sid-7A5C1ACB-D07C-407D-83BB-C55D04425AC7" name="subProcess">
      <startEvent id="cstart01" name="子流程开始" flowable:formFieldValidation="true"></startEvent>
      <userTask id="taskc01" name="子人工任务一" flowable:assignee="user1" flowable:formFieldValidation="true">
        <extensionElements>
          <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
        </extensionElements>
      </userTask>
      <exclusiveGateway id="p001" name="排他网关"></exclusiveGateway>
      <userTask id="taskc02" name="子人工任务二" flowable:assignee="user2" flowable:formFieldValidation="true">
        <extensionElements>
          <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
        </extensionElements>
      </userTask>
      <endEvent id="endc01" name="子流程结束"></endEvent>
      <endEvent id="sid-BD67523C-DDDA-46B8-A788-C54769B081B1">
        <terminateEventDefinition></terminateEventDefinition>
      </endEvent>
      <sequenceFlow id="sid-2E0A3425-3C34-4771-85C0-C8C7B7D7BFF5" sourceRef="cstart01" targetRef="taskc01"></sequenceFlow>
      <sequenceFlow id="sid-B0777673-C3BE-488B-87B2-34A71CEE2C73" sourceRef="taskc01" targetRef="p001"></sequenceFlow>
      <sequenceFlow id="sid-3C938AB5-2FDF-4C0B-AED8-15853AB07FE8" sourceRef="taskc02" targetRef="endc01"></sequenceFlow>
      <sequenceFlow id="sid-AAC6E950-F7BD-454A-9105-F43E5D4530A1" sourceRef="p001" targetRef="sid-BD67523C-DDDA-46B8-A788-C54769B081B1">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag<=0}]]></conditionExpression>
      </sequenceFlow>
      <sequenceFlow id="sid-EFCAE414-03A4-4313-B97A-4BF962888C37" sourceRef="p001" targetRef="taskc02">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag>0}]]></conditionExpression>
      </sequenceFlow>
    </subProcess>
    <parallelGateway id="b001" name="并行网关"></parallelGateway>
    <sequenceFlow id="sid-D87CB0F6-EF35-479A-80A7-5FDACF1022E1" sourceRef="start01" targetRef="b001"></sequenceFlow>
    <parallelGateway id="b002" name="并行网关"></parallelGateway>
    <sequenceFlow id="sid-200E2768-DCA0-4FAD-A3C0-2D69938379A2" sourceRef="task2" targetRef="b002"></sequenceFlow>
    <sequenceFlow id="sid-A554CEA8-FD95-4EA4-BED3-EA748273D775" sourceRef="sid-7A5C1ACB-D07C-407D-83BB-C55D04425AC7" targetRef="b002"></sequenceFlow>
    <sequenceFlow id="sid-C5FB6E94-3563-49C8-9BA2-C0C80670E9FD" sourceRef="task1" targetRef="b002"></sequenceFlow>
    <endEvent id="end003" name="结束事件"></endEvent>
    <sequenceFlow id="sid-679D7677-4828-4BCF-9F2E-CBC426D4B72B" sourceRef="b002" targetRef="end003"></sequenceFlow>
    <sequenceFlow id="sid-0264CAD1-386A-4A4F-ADDA-DCF62EE90594" sourceRef="b001" targetRef="sid-7A5C1ACB-D07C-407D-83BB-C55D04425AC7"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_event3002">
    <bpmndi:BPMNPlane bpmnElement="event3002" id="BPMNPlane_event3002">
      <bpmndi:BPMNShape bpmnElement="start01" id="BPMNShape_start01">
        <omgdc:Bounds height="30.0" width="30.0" x="75.0" y="200.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task2" id="BPMNShape_task2">
        <omgdc:Bounds height="80.0" width="100.0" x="420.0" y="175.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task1" id="BPMNShape_task1">
        <omgdc:Bounds height="80.0" width="100.0" x="420.0" y="15.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-7A5C1ACB-D07C-407D-83BB-C55D04425AC7" id="BPMNShape_sid-7A5C1ACB-D07C-407D-83BB-C55D04425AC7">
        <omgdc:Bounds height="187.0" width="531.0" x="285.0" y="330.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="cstart01" id="BPMNShape_cstart01">
        <omgdc:Bounds height="30.0" width="30.0" x="322.5" y="410.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="taskc01" id="BPMNShape_taskc01">
        <omgdc:Bounds height="80.0" width="100.0" x="390.0" y="385.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="p001" id="BPMNShape_p001">
        <omgdc:Bounds height="40.0" width="40.0" x="542.5" y="405.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="taskc02" id="BPMNShape_taskc02">
        <omgdc:Bounds height="80.0" width="100.0" x="627.5" y="385.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endc01" id="BPMNShape_endc01">
        <omgdc:Bounds height="28.0" width="28.0" x="765.0" y="411.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-BD67523C-DDDA-46B8-A788-C54769B081B1" id="BPMNShape_sid-BD67523C-DDDA-46B8-A788-C54769B081B1">
        <omgdc:Bounds height="28.0" width="28.0" x="548.5" y="480.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="b001" id="BPMNShape_b001">
        <omgdc:Bounds height="40.0" width="40.0" x="165.0" y="195.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="b002" id="BPMNShape_b002">
        <omgdc:Bounds height="40.0" width="40.0" x="867.5" y="195.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end003" id="BPMNShape_end003">
        <omgdc:Bounds height="28.0" width="28.0" x="960.0" y="207.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-EFCAE414-03A4-4313-B97A-4BF962888C37" id="BPMNEdge_sid-EFCAE414-03A4-4313-B97A-4BF962888C37" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="582.0247370727355" y="425.41666666666663"></omgdi:waypoint>
        <omgdi:waypoint x="627.4999999999881" y="425.2181222707423"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-3C938AB5-2FDF-4C0B-AED8-15853AB07FE8" id="BPMNEdge_sid-3C938AB5-2FDF-4C0B-AED8-15853AB07FE8" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="727.4499999997752" y="425.0"></omgdi:waypoint>
        <omgdi:waypoint x="765.0" y="425.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-2E0A3425-3C34-4771-85C0-C8C7B7D7BFF5" id="BPMNEdge_sid-2E0A3425-3C34-4771-85C0-C8C7B7D7BFF5" flowable:sourceDockerX="15.0" flowable:sourceDockerY="15.0" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="352.449998261009" y="425.0"></omgdi:waypoint>
        <omgdi:waypoint x="390.0" y="425.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-A554CEA8-FD95-4EA4-BED3-EA748273D775" id="BPMNEdge_sid-A554CEA8-FD95-4EA4-BED3-EA748273D775" flowable:sourceDockerX="265.5" flowable:sourceDockerY="93.5" flowable:targetDockerX="20.5" flowable:targetDockerY="20.5">
        <omgdi:waypoint x="815.95" y="423.5"></omgdi:waypoint>
        <omgdi:waypoint x="888.0" y="423.5"></omgdi:waypoint>
        <omgdi:waypoint x="888.0" y="234.40453342953353"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-AAC6E950-F7BD-454A-9105-F43E5D4530A1" id="BPMNEdge_sid-AAC6E950-F7BD-454A-9105-F43E5D4530A1" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="562.8602941176471" y="444.5752202643172"></omgdi:waypoint>
        <omgdi:waypoint x="562.6018221538329" y="480.0003662069823"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-200E2768-DCA0-4FAD-A3C0-2D69938379A2" id="BPMNEdge_sid-200E2768-DCA0-4FAD-A3C0-2D69938379A2" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="20.5" flowable:targetDockerY="20.5">
        <omgdi:waypoint x="519.949999999999" y="215.05974880382777"></omgdi:waypoint>
        <omgdi:waypoint x="867.97604790418" y="215.4760479041916"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-B0777673-C3BE-488B-87B2-34A71CEE2C73" id="BPMNEdge_sid-B0777673-C3BE-488B-87B2-34A71CEE2C73" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="20.5" flowable:targetDockerY="20.5">
        <omgdi:waypoint x="489.9499999999965" y="425.2030487804878"></omgdi:waypoint>
        <omgdi:waypoint x="542.9183673469388" y="425.4183673469388"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-679D7677-4828-4BCF-9F2E-CBC426D4B72B" id="BPMNEdge_sid-679D7677-4828-4BCF-9F2E-CBC426D4B72B" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="906.92933478735" y="215.5"></omgdi:waypoint>
        <omgdi:waypoint x="933.75" y="215.5"></omgdi:waypoint>
        <omgdi:waypoint x="933.75" y="221.0"></omgdi:waypoint>
        <omgdi:waypoint x="960.0" y="221.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-D87CB0F6-EF35-479A-80A7-5FDACF1022E1" id="BPMNEdge_sid-D87CB0F6-EF35-479A-80A7-5FDACF1022E1" flowable:sourceDockerX="15.0" flowable:sourceDockerY="15.0" flowable:targetDockerX="20.0" flowable:targetDockerY="20.0">
        <omgdi:waypoint x="104.94999797575196" y="215.0"></omgdi:waypoint>
        <omgdi:waypoint x="165.0" y="215.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-0264CAD1-386A-4A4F-ADDA-DCF62EE90594" id="BPMNEdge_sid-0264CAD1-386A-4A4F-ADDA-DCF62EE90594" flowable:sourceDockerX="22.5" flowable:sourceDockerY="32.015625" flowable:targetDockerX="1.1773835920177385" flowable:targetDockerY="93.5">
        <omgdi:waypoint x="187.5" y="232.44861780088067"></omgdi:waypoint>
        <omgdi:waypoint x="187.5" y="423.5"></omgdi:waypoint>
        <omgdi:waypoint x="285.0" y="423.5"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-03E93AD6-F173-454F-B29F-B61FE45BE1E0" id="BPMNEdge_sid-03E93AD6-F173-454F-B29F-B61FE45BE1E0" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="204.4800158394907" y="215.46654929577466"></omgdi:waypoint>
        <omgdi:waypoint x="419.99999999998107" y="215.08778558875218"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-A4455D52-7DAF-4193-9E4F-D75E133A6EDD" id="BPMNEdge_sid-A4455D52-7DAF-4193-9E4F-D75E133A6EDD" flowable:sourceDockerX="20.5" flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="185.5" y="195.5"></omgdi:waypoint>
        <omgdi:waypoint x="185.5" y="55.0"></omgdi:waypoint>
        <omgdi:waypoint x="420.0" y="55.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-C5FB6E94-3563-49C8-9BA2-C0C80670E9FD" id="BPMNEdge_sid-C5FB6E94-3563-49C8-9BA2-C0C80670E9FD" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="20.0" flowable:targetDockerY="13.7109375">
        <omgdi:waypoint x="519.95" y="55.0"></omgdi:waypoint>
        <omgdi:waypoint x="887.5" y="55.0"></omgdi:waypoint>
        <omgdi:waypoint x="887.5" y="195.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

流程说明:人工任务一 –>zhangsan  人工任务二 –>lisi  子人工任务一 :user1 子人工任务二 :user2

子任务完成任务如果flag<=0,那么子流程中断结束,子人工任务二不会触发

部署流程

    @Test
    public void test02() throws Exception{

        Deployment deployment = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource("终止流程2.bpmn20.xml")
                .name("终止流程2")
                .deploy();
        System.out.println("-----");
    }

然后启动流程

    @Test
    public void startProcessInstanceByKey()  throws Exception{
        // 设置对应的流程变量的值
        Map<String,Object> map = new HashMap<>();
        map.put("flag",0);// 设置flag为0触发流程结束事件
        processEngine.getRuntimeService()
                .startProcessInstanceById("event3002:1:92679bec-b03d-11ec-901e-c03c59ad2248",map);
    }

这时在ACT_RU_TASK中就应该有三条对应的TASK记录了

Flowable事件之结束事件
在这里插入图片描述

然后我们执行user1的子任务,因为现在流程变量flag=0所以会触发终止结束事件

Flowable事件之结束事件
在这里插入图片描述

执行任务:

    @Test
    public void completeTask(){
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionId("event3002:1:92679bec-b03d-11ec-901e-c03c59ad2248")
                .taskAssignee("user1")
                .singleResult();
        taskService.complete(task.getId());
    }

执行成功后我们可以发现子人工任务一没有了,但是也没有子人工任务二,但是主流程中的两个任务都还在

Flowable事件之结束事件
在这里插入图片描述

而且进入到历史数据中也可以看到Flowable事件之结束事件

3.取消结束事件

  取消结束事件(cancel end event)只能与BPMN事务子流程(BPMN transaction subprocess)一起使用。当到达取消结束事件时,会抛出取消事件,且必须由取消边界事件(cancel boundary event)捕获。取消边界事件将取消事务,并触发补偿(compensation)。

具体通过案例来讲解:

Flowable事件之结束事件
在这里插入图片描述

结束取消事件我们只能在事务子流程中使用,在FlowableUI中暂时没有找到这个组件,所以在Eclipse中来绘制Flowable事件之结束事件

完整的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <transaction id="transaction1" name="Transaction">
      <startEvent id="startevent2" name="事务子流程开始"></startEvent>
      <userTask id="usertask1" name="人工任务一" activiti:assignee="zhangsan"></userTask>
      <sequenceFlow id="flow2" sourceRef="startevent2" targetRef="usertask1"></sequenceFlow>
      <exclusiveGateway id="exclusivegateway1" name="排他网关"></exclusiveGateway>
      <sequenceFlow id="flow3" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
      <userTask id="usertask2" name="人工任务二" activiti:assignee="lisi"></userTask>
      <sequenceFlow id="flow4" sourceRef="exclusivegateway1" targetRef="usertask2">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag>0}]]></conditionExpression>
      </sequenceFlow>
      <endEvent id="endevent1" name="事务子流程结束"></endEvent>
      <sequenceFlow id="flow5" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
      <endEvent id="cancelendevent1" name="取消结束事件">
        <cancelEventDefinition></cancelEventDefinition>
      </endEvent>
      <sequenceFlow id="flow6" sourceRef="exclusivegateway1" targetRef="cancelendevent1">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag<=0}]]></conditionExpression>
      </sequenceFlow>
      <boundaryEvent id="boundarycompensation1" name="Compensate" attachedToRef="usertask1" cancelActivity="true">
        <compensateEventDefinition></compensateEventDefinition>
      </boundaryEvent>
      <serviceTask id="servicetask5" name="补偿自动任务" isForCompensation="true" activiti:class="com.bobo.delegate.MyOneDelegate"></serviceTask>
      <association id="association1" sourceRef="boundarycompensation1" targetRef="servicetask5" associationDirection="None"></association>
    </transaction>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="transaction1"></sequenceFlow>
    <boundaryEvent id="boundarycancel1" name="取消边界事件" attachedToRef="transaction1" cancelActivity="true">
      <cancelEventDefinition></cancelEventDefinition>
    </boundaryEvent>
    <serviceTask id="servicetask3" name="取消事件结束" activiti:class="com.bobo.delegate.MyThreeDelegate"></serviceTask>
    <sequenceFlow id="flow8" sourceRef="boundarycancel1" targetRef="servicetask3"></sequenceFlow>
    <endEvent id="endevent2" name="取消事件结束"></endEvent>
    <sequenceFlow id="flow9" sourceRef="servicetask3" targetRef="endevent2"></sequenceFlow>
    <serviceTask id="servicetask4" name="正常结束任务" activiti:class="com.bobo.delegate.MyTwoDelegate"></serviceTask>
    <sequenceFlow id="flow10" sourceRef="transaction1" targetRef="servicetask4"></sequenceFlow>
    <endEvent id="endevent3" name="End"></endEvent>
    <sequenceFlow id="flow11" sourceRef="servicetask4" targetRef="endevent3"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="248.0" y="390.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="transaction1" id="BPMNShape_transaction1">
        <omgdc:Bounds height="291.0" width="761.0" x="371.0" y="262.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="startevent2" id="BPMNShape_startevent2">
        <omgdc:Bounds height="35.0" width="35.0" x="431.0" y="382.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="511.0" y="372.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="boundarycompensation1" id="BPMNShape_boundarycompensation1">
        <omgdc:Bounds height="30.0" width="30.0" x="581.0" y="412.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="661.0" y="380.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="746.0" y="373.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="896.0" y="383.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="cancelendevent1" id="BPMNShape_cancelendevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="664.0" y="472.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask5" id="BPMNShape_servicetask5">
        <omgdc:Bounds height="55.0" width="105.0" x="511.0" y="472.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="boundarycancel1" id="BPMNShape_boundarycancel1">
        <omgdc:Bounds height="30.0" width="30.0" x="871.0" y="542.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
        <omgdc:Bounds height="55.0" width="105.0" x="980.0" y="640.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
        <omgdc:Bounds height="35.0" width="35.0" x="1130.0" y="650.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask4" id="BPMNShape_servicetask4">
        <omgdc:Bounds height="55.0" width="105.0" x="1291.0" y="391.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent3" id="BPMNShape_endevent3">
        <omgdc:Bounds height="35.0" width="35.0" x="1450.0" y="401.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="466.0" y="399.0"></omgdi:waypoint>
        <omgdi:waypoint x="511.0" y="399.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="616.0" y="399.0"></omgdi:waypoint>
        <omgdi:waypoint x="661.0" y="400.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="701.0" y="400.0"></omgdi:waypoint>
        <omgdi:waypoint x="746.0" y="400.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="851.0" y="400.0"></omgdi:waypoint>
        <omgdi:waypoint x="896.0" y="400.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="681.0" y="420.0"></omgdi:waypoint>
        <omgdi:waypoint x="681.0" y="472.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="association1" id="BPMNEdge_association1">
        <omgdi:waypoint x="596.0" y="442.0"></omgdi:waypoint>
        <omgdi:waypoint x="563.0" y="472.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="283.0" y="407.0"></omgdi:waypoint>
        <omgdi:waypoint x="371.0" y="407.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
        <omgdi:waypoint x="886.0" y="572.0"></omgdi:waypoint>
        <omgdi:waypoint x="1032.0" y="640.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
        <omgdi:waypoint x="1085.0" y="667.0"></omgdi:waypoint>
        <omgdi:waypoint x="1130.0" y="667.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
        <omgdi:waypoint x="1132.0" y="407.0"></omgdi:waypoint>
        <omgdi:waypoint x="1291.0" y="418.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11">
        <omgdi:waypoint x="1396.0" y="418.0"></omgdi:waypoint>
        <omgdi:waypoint x="1450.0" y="418.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

流程说明:流程中定义了一个事务子流程和两个自动任务

Flowable事件之结束事件
在这里插入图片描述

然后在事务子流程中定义了两个人工任务用一个排他网关连接,flag<=0 的情况下会触发 取消结束事件

Flowable事件之结束事件
在这里插入图片描述

触发取消结束事件后同时会被取消边界事件捕获而走 取消事件结束的自动任务

Flowable事件之结束事件
在这里插入图片描述

同时自动补偿任务也会触发,关联的自动任务也会触发

Flowable事件之结束事件
在这里插入图片描述

注意在设置的时候需要设置补偿自动任务为可补偿的

Flowable事件之结束事件
在这里插入图片描述

补偿自动任务绑定的JavaDelegate:

public class MyOneDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {

        System.out.println("完成自动审批任务-----》MyOneDelegate" + LocalDateTime.now().toString());
    }
}

正常结束任务绑定的JavaDelegate:

public class MyTwoDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        System.out.println("MyTwoDelegate---->执行了" + LocalDateTime.now().toString());
    }
}

取消事件结束绑定的JavaDelegate

public class MyThreeDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        System.out.println("MyThreeDelegate---->执行了" + LocalDateTime.now().toString());
    }
}

然后我们部署,启动,和执行流程,看输出情况

    /**
     * 部署流程
     * @throws Exception
     */

    @Test
    public void deploy() throws Exception{
        Deployment deployment = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource("取消结束事件.bpmn20.xml")
                .name("取消结束事件")
                .deploy();
        System.out.println("-----");
    }

    /**
     * 启动流程实例,设置流程变量中的flag=0触发取消结束事件
     */

    @Test
    public void startProcessInstanceByKey()  throws Exception{
        // 设置对应的流程变量的值
        Map<String,Object> map = new HashMap<>();
        map.put("flag",0);// 设置flag为0触发流程结束事件
        processEngine.getRuntimeService()
                .startProcessInstanceById("myProcess:3:effb46cf-b168-11ec-926f-c03c59ad2248",map);
    }

    /**
     * 完成任务
     */

    @Test
    public void completeTask(){
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionId("myProcess:3:effb46cf-b168-11ec-926f-c03c59ad2248")
                .taskAssignee("zhangsan")
                .singleResult();
        taskService.complete(task.getId());
    }

输出结果:

Flowable事件之结束事件
在这里插入图片描述

输出结果是满足我们的期望的。

4.补偿事件

  通过补偿达到控制业务流程的目的就是补偿事件,比如我们正常的买机票的流程下订单购买,然后同时弹出支付流程页面。支付成功后就可以等待出票了,但是如果我们支付失败的话,这时要么重新支付,更换支付方式或者取消预订,这时取消预订我们就可以通过补偿事件来实现,具体的案例如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yrBVZsXe-1649556624617)(D:desktop桌面文件工作目录1-录课资料20-flowable1-课件imgimage-20220401202116847.png)]

完整的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
    <error id="payFail" errorCode="payFail" ></error>
    <process id="myProcess" name="My process" isExecutable="true">
        <startEvent id="startevent1" name="开始事件"></startEvent>
        <parallelGateway id="parallelgateway1" name="并行网关"></parallelGateway>
        <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="parallelgateway1"></sequenceFlow>
        <serviceTask id="servicetask1" name="预订机票" activiti:class="com.bobo.delegate.MyTwoDelegate"></serviceTask>
        <serviceTask id="servicetask2" name="微信支付" activiti:class="com.bobo.delegate.MyOneDelegate"></serviceTask>
        <userTask id="usertask1" name="人工出票" activiti:assignee="zhangsan"></userTask>
        <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="usertask1"></sequenceFlow>
        <parallelGateway id="parallelgateway2" name="Parallel Gateway"></parallelGateway>
        <sequenceFlow id="flow3" sourceRef="usertask1" targetRef="parallelgateway2"></sequenceFlow>
        <sequenceFlow id="flow4" sourceRef="parallelgateway1" targetRef="servicetask1"></sequenceFlow>
        <sequenceFlow id="flow5" sourceRef="parallelgateway1" targetRef="servicetask2"></sequenceFlow>
        <sequenceFlow id="flow6" sourceRef="servicetask2" targetRef="parallelgateway2"></sequenceFlow>
        <serviceTask id="servicetask3" name="取消预订" isForCompensation="true" activiti:class="com.bobo.delegate.MyThreeDelegate"></serviceTask>
        <boundaryEvent id="boundarycompensation1" name="补偿边界事件" attachedToRef="servicetask1" cancelActivity="true">
            <compensateEventDefinition></compensateEventDefinition>
        </boundaryEvent>
        <boundaryEvent id="boundaryerror1" name="错误边界事件" attachedToRef="servicetask2">
            <errorEventDefinition errorRef="payFail"></errorEventDefinition>
        </boundaryEvent>
        <intermediateThrowEvent id="compensationintermediatethrowevent1" name="补偿抛出中间事件">
            <compensateEventDefinition></compensateEventDefinition>
        </intermediateThrowEvent>
        <sequenceFlow id="flow7" sourceRef="boundaryerror1" targetRef="compensationintermediatethrowevent1"></sequenceFlow>
        <endEvent id="endevent1" name="End"></endEvent>
        <sequenceFlow id="flow8" sourceRef="compensationintermediatethrowevent1" targetRef="endevent1"></sequenceFlow>
        <endEvent id="endevent2" name="End"></endEvent>
        <sequenceFlow id="flow9" sourceRef="parallelgateway2" targetRef="endevent2"></sequenceFlow>
        <association id="association1" sourceRef="boundarycompensation1" targetRef="servicetask3" associationDirection="None"></association>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
        <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
            <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="160.0" y="360.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="parallelgateway1" id="BPMNShape_parallelgateway1">
                <omgdc:Bounds height="40.0" width="40.0" x="380.0" y="357.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
                <omgdc:Bounds height="55.0" width="105.0" x="580.0" y="220.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="boundarycompensation1" id="BPMNShape_boundarycompensation1">
                <omgdc:Bounds height="30.0" width="30.0" x="650.0" y="270.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
                <omgdc:Bounds height="55.0" width="105.0" x="580.0" y="450.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="boundaryerror1" id="BPMNShape_boundaryerror1">
                <omgdc:Bounds height="30.0" width="30.0" x="650.0" y="490.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
                <omgdc:Bounds height="55.0" width="105.0" x="820.0" y="220.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="parallelgateway2" id="BPMNShape_parallelgateway2">
                <omgdc:Bounds height="40.0" width="40.0" x="1140.0" y="336.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
                <omgdc:Bounds height="55.0" width="105.0" x="830.0" y="336.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="compensationintermediatethrowevent1" id="BPMNShape_compensationintermediatethrowevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="740.0" y="590.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="820.0" y="590.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
                <omgdc:Bounds height="35.0" width="35.0" x="1225.0" y="339.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
                <omgdi:waypoint x="195.0" y="377.0"></omgdi:waypoint>
                <omgdi:waypoint x="380.0" y="377.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
                <omgdi:waypoint x="685.0" y="247.0"></omgdi:waypoint>
                <omgdi:waypoint x="820.0" y="247.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
                <omgdi:waypoint x="925.0" y="247.0"></omgdi:waypoint>
                <omgdi:waypoint x="1160.0" y="247.0"></omgdi:waypoint>
                <omgdi:waypoint x="1160.0" y="336.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
                <omgdi:waypoint x="400.0" y="357.0"></omgdi:waypoint>
                <omgdi:waypoint x="400.0" y="247.0"></omgdi:waypoint>
                <omgdi:waypoint x="580.0" y="247.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
                <omgdi:waypoint x="400.0" y="397.0"></omgdi:waypoint>
                <omgdi:waypoint x="400.0" y="477.0"></omgdi:waypoint>
                <omgdi:waypoint x="580.0" y="477.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
                <omgdi:waypoint x="685.0" y="477.0"></omgdi:waypoint>
                <omgdi:waypoint x="1160.0" y="477.0"></omgdi:waypoint>
                <omgdi:waypoint x="1160.0" y="376.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
                <omgdi:waypoint x="665.0" y="520.0"></omgdi:waypoint>
                <omgdi:waypoint x="664.0" y="607.0"></omgdi:waypoint>
                <omgdi:waypoint x="740.0" y="607.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
                <omgdi:waypoint x="775.0" y="607.0"></omgdi:waypoint>
                <omgdi:waypoint x="820.0" y="607.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
                <omgdi:waypoint x="1180.0" y="356.0"></omgdi:waypoint>
                <omgdi:waypoint x="1225.0" y="356.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="association1" id="BPMNEdge_association1">
                <omgdi:waypoint x="665.0" y="300.0"></omgdi:waypoint>
                <omgdi:waypoint x="664.0" y="363.0"></omgdi:waypoint>
                <omgdi:waypoint x="830.0" y="363.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>

  流程说明:

Flowable事件之结束事件
在这里插入图片描述

微信支付绑定的JavaDelegate:

public class MyOneDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {

        System.out.println("微信支付-----》MyOneDelegate" + LocalDateTime.now().toString());
        System.out.println("余额不足....");
        throw  new BpmnError("payFail");
    }
}

预订机票绑定的JavaDelegate

public class MyTwoDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        System.out.println("MyTwoDelegate---->预订机票流程执行了" + LocalDateTime.now().toString());
    }
}

取消订单绑定的javaDelegate:

public class MyThreeDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        System.out.println("MyThreeDelegate---->机票预订取消了...." + LocalDateTime.now().toString());
    }
}

整个流程执行的过程是:任务开始后会并行的执行机票预订和微信支付,然后在微信支付是抛出payFail错误,同时错误边界事件会捕获到这个错误,然后执行到 补偿抛出中间事件,之后在机票预订的 补偿边界事件 被触发,对应的补偿触发器会执行对应的代码。然后我们部署加启动流程后观察控制台的输出:

    /**
     * 部署流程
     * @throws Exception
     */

    @Test
    public void deploy() throws Exception{
        Deployment deployment = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource("补偿事件.bpmn20.xml")
                .name("补偿事件")
                .deploy();
    }

    /**
     * 启动流程实例
     */

    @Test
    public void startProcessInstanceByKey()  throws Exception{
        processEngine.getRuntimeService()
                .startProcessInstanceById("myProcess:4:a45abe9f-b1b5-11ec-9daf-c03c59ad2248");
    }

控制台输出:

MyTwoDelegate---->预订机票流程执行了2022-04-01T20:17:44.985
微信支付-----》MyOneDelegate2022-04-01T20:17:44.987
余额不足....
MyThreeDelegate---->机票预订取消了....2022-04-01T20:17:45.028

输出的结果和我们前面设计的是一样的,搞定~


原文始发于微信公众号(波哥带你学编程):Flowable事件之结束事件

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/58473.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!