SpringBoot整合Activiti7——终止结束事件(十三)

2024-01-08 10:42:09


终止结束事件

终止结束事件

主要是对流程进行终止的事件,可以在一个复杂的流程中,如果某方想要提前中断这个流程,可以采用这个事件来处理。terminateAll属性,true表示不管是否嵌套或子流程都会被终止,false则终止多实例或子流程。

<!-- 终止结束事件 -->
<endEvent id="sid-4c711416-31a4-441f-91ff-f004e70335a2" name="终止结束事件">
  <!-- terminateAll是否终止全部 -->
  <terminateEventDefinition activiti:terminateAll="true"/>
</endEvent>

代码实现

在这里插入图片描述

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/processdef">
  <process id="terminate" name="终止事件" isExecutable="true">
    <documentation>终止事件流程</documentation>
    <startEvent id="sid-863bb93b-eb19-4d2a-b9e1-8ee1fab94788" name="开始事件"/>
    <parallelGateway id="sid-580cbd70-e33b-47d6-a18a-05522a7d2025" name="并行网关1"/>
    <sequenceFlow id="sid-cbcee4ab-05e4-42e1-91ee-a2498ac5e0a4" sourceRef="sid-863bb93b-eb19-4d2a-b9e1-8ee1fab94788" targetRef="sid-580cbd70-e33b-47d6-a18a-05522a7d2025"/>
    <userTask id="sid-11eb2beb-26a2-4967-8092-b0f315ff2d21" name="超级管理员" activiti:assignee="${superAdmin}"/>
    <sequenceFlow id="sid-1a627e97-2c6d-4cc4-b171-62a1522f926e" sourceRef="sid-580cbd70-e33b-47d6-a18a-05522a7d2025" targetRef="sid-11eb2beb-26a2-4967-8092-b0f315ff2d21">
      <conditionExpression xsi:type="tFormalExpression"/>
    </sequenceFlow>
    <userTask id="sid-90543e88-ec7b-4efc-a995-ef924cbdf42a" name="管理员" activiti:assignee="${admin}"/>
    <sequenceFlow id="sid-f8f0cf12-8fcf-4748-b9ff-cd8d95777d77" sourceRef="sid-580cbd70-e33b-47d6-a18a-05522a7d2025" targetRef="sid-90543e88-ec7b-4efc-a995-ef924cbdf42a">
      <conditionExpression xsi:type="tFormalExpression"/>
    </sequenceFlow>
    <userTask id="sid-6a4ff883-3fa4-499d-a380-7373cbcedec0" name="用户" activiti:candidateGroups="${userGroup}"/>
    <sequenceFlow id="sid-4daffcef-5787-4a49-8dd6-10277cee5a8b" sourceRef="sid-580cbd70-e33b-47d6-a18a-05522a7d2025" targetRef="sid-6a4ff883-3fa4-499d-a380-7373cbcedec0">
      <conditionExpression xsi:type="tFormalExpression"/>
    </sequenceFlow>
    <parallelGateway id="sid-ba4b2b53-fbfb-4208-90a0-17daf2aa24ff" name="并行网关2"/>
    <sequenceFlow id="sid-e0319c6b-316c-4f11-9818-bb4a3c685a93" sourceRef="sid-90543e88-ec7b-4efc-a995-ef924cbdf42a" targetRef="sid-e8681835-d9c2-46db-988a-69329dd4e8e6"/>
    <endEvent id="sid-87f454a3-dd80-45ed-8cfe-b31533682500" name="结束事件"/>
    <sequenceFlow id="sid-c4348b1d-d319-49c3-996e-4bf53a09668e" sourceRef="sid-ba4b2b53-fbfb-4208-90a0-17daf2aa24ff" targetRef="sid-87f454a3-dd80-45ed-8cfe-b31533682500">
      <conditionExpression xsi:type="tFormalExpression"/>
    </sequenceFlow>
    <!-- 终止结束事件 -->
    <endEvent id="sid-4c711416-31a4-441f-91ff-f004e70335a2" name="终止结束事件">
      <!-- terminateAll是否终止全部 -->
      <terminateEventDefinition activiti:terminateAll="true"/>
    </endEvent>
    <exclusiveGateway id="sid-5f9fe128-e1dc-4f95-9806-f11f811cb8c5" name="是否终止结束"/>
    <sequenceFlow id="sid-c22902db-b537-498b-80ca-80f10ef27f69" sourceRef="sid-11eb2beb-26a2-4967-8092-b0f315ff2d21" targetRef="sid-5f9fe128-e1dc-4f95-9806-f11f811cb8c5"/>
    <sequenceFlow id="sid-77f0e117-688d-4ec9-ab8d-708f1e3eda3a" sourceRef="sid-5f9fe128-e1dc-4f95-9806-f11f811cb8c5" targetRef="sid-4c711416-31a4-441f-91ff-f004e70335a2" name="">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${done}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-8f7022c5-98c4-4518-bd66-9df07cb96590" sourceRef="sid-5f9fe128-e1dc-4f95-9806-f11f811cb8c5" targetRef="sid-ba4b2b53-fbfb-4208-90a0-17daf2aa24ff" name="">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!done}]]></conditionExpression>
    </sequenceFlow>
    <subProcess id="sid-e8681835-d9c2-46db-988a-69329dd4e8e6" activiti:exclusive="true" name="子流程">
      <startEvent id="sid-c915ca2e-07ea-4551-9486-071b12ca9a75"/>
      <userTask id="sid-08f8df5d-e397-4c00-8cf6-481c7b3f3b3f" name="保存任务" activiti:assignee="${admin}"/>
      <sequenceFlow id="sid-0ee7ed53-3e26-4b57-967a-7daca3cf7b34" sourceRef="sid-c915ca2e-07ea-4551-9486-071b12ca9a75" targetRef="sid-08f8df5d-e397-4c00-8cf6-481c7b3f3b3f"/>
      <userTask id="sid-a8959d20-bcc8-404f-a57c-488f5cf68ee7" name="删除任务"/>
      <sequenceFlow id="sid-7affbd7a-cf2c-4979-bf71-9038271c0540" sourceRef="sid-08f8df5d-e397-4c00-8cf6-481c7b3f3b3f" targetRef="sid-56b471fa-8b46-4f21-ad10-4d00f38abbda"/>
      <endEvent id="sid-ed37695c-0449-4b66-b978-8b87c3f87062"/>
      <sequenceFlow id="sid-75a8b19e-52c0-4828-a78b-65d97e7445ee" sourceRef="sid-a8959d20-bcc8-404f-a57c-488f5cf68ee7" targetRef="sid-ed37695c-0449-4b66-b978-8b87c3f87062"/>
      <exclusiveGateway id="sid-56b471fa-8b46-4f21-ad10-4d00f38abbda" name="是否终止结束"/>
      <sequenceFlow id="sid-d6e685f3-5451-49e3-8552-ca35b526fd1c" sourceRef="sid-56b471fa-8b46-4f21-ad10-4d00f38abbda" targetRef="sid-a8959d20-bcc8-404f-a57c-488f5cf68ee7" name="">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!done}]]></conditionExpression>
      </sequenceFlow>
      <!-- 子流程终止结束事件 -->
      <endEvent id="sid-27c262f9-1b1c-40aa-b1b2-8ec0b65ef597" name="子流程终止结束事件">
        <!-- terminateAll是否终止全部 -->
        <terminateEventDefinition activiti:terminateAll="false"/>
      </endEvent>
      <sequenceFlow id="sid-ef1f99b2-7898-4cb2-9fb9-dc008e5fc77c" sourceRef="sid-56b471fa-8b46-4f21-ad10-4d00f38abbda" targetRef="sid-27c262f9-1b1c-40aa-b1b2-8ec0b65ef597" name="">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${done}]]></conditionExpression>
      </sequenceFlow>
    </subProcess>
    <sequenceFlow id="sid-c25f9150-f65c-4f4f-b68f-435513be2cfe" sourceRef="sid-6a4ff883-3fa4-499d-a380-7373cbcedec0" targetRef="sid-ba4b2b53-fbfb-4208-90a0-17daf2aa24ff"/>
    <sequenceFlow id="sid-b9772e80-3f33-4f9d-baef-b7f43dbd034c" sourceRef="sid-e8681835-d9c2-46db-988a-69329dd4e8e6" targetRef="sid-ba4b2b53-fbfb-4208-90a0-17daf2aa24ff"/>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_terminate">
    <bpmndi:BPMNPlane bpmnElement="terminate" id="BPMNPlane_terminate">
      <bpmndi:BPMNShape id="shape-1f7452d8-2f26-4811-aa95-fa74ad27ebba" bpmnElement="sid-863bb93b-eb19-4d2a-b9e1-8ee1fab94788">
        <omgdc:Bounds x="-295.75" y="-39.0" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-8b3f6d47-b390-4bc8-9606-2f82b8ac0f09" bpmnElement="sid-580cbd70-e33b-47d6-a18a-05522a7d2025">
        <omgdc:Bounds x="-215.25" y="-44.0" width="40.0" height="40.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-ce8ae082-12c5-4461-a4cd-cf0e160a987f" bpmnElement="sid-cbcee4ab-05e4-42e1-91ee-a2498ac5e0a4">
        <omgdi:waypoint x="-265.75" y="-24.0"/>
        <omgdi:waypoint x="-215.25" y="-24.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-7b277561-3677-4f38-8889-6be3eceb0596" bpmnElement="sid-11eb2beb-26a2-4967-8092-b0f315ff2d21">
        <omgdc:Bounds x="-114.97" y="-183.44" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-981075ac-4a51-4605-b21a-882936e6f629" bpmnElement="sid-1a627e97-2c6d-4cc4-b171-62a1522f926e">
        <omgdi:waypoint x="-195.25" y="-43.999996"/>
        <omgdi:waypoint x="-114.96999" y="-143.44"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-d8cf568d-e78a-47d4-b9dc-8c63fcdd1525" bpmnElement="sid-90543e88-ec7b-4efc-a995-ef924cbdf42a">
        <omgdc:Bounds x="-114.97" y="-64.0" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-8c8cb622-18bf-4356-b422-2d578c600aff" bpmnElement="sid-f8f0cf12-8fcf-4748-b9ff-cd8d95777d77">
        <omgdi:waypoint x="-175.25" y="-24.0"/>
        <omgdi:waypoint x="-114.97" y="-24.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-d65116e4-a530-40c8-83b4-6ba5262266f1" bpmnElement="sid-6a4ff883-3fa4-499d-a380-7373cbcedec0">
        <omgdc:Bounds x="-114.97" y="59.859825" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-85bbffe2-9d89-4304-acab-9a522f8614b7" bpmnElement="sid-4daffcef-5787-4a49-8dd6-10277cee5a8b">
        <omgdi:waypoint x="-195.25" y="-4.0"/>
        <omgdi:waypoint x="-114.97" y="99.859825"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-bac69cf2-0f80-452a-af56-58a55f32860e" bpmnElement="sid-ba4b2b53-fbfb-4208-90a0-17daf2aa24ff">
        <omgdc:Bounds x="472.43527" y="-44.0" width="40.0" height="40.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-665eec2e-9e6f-4f12-a86a-2c6b3faf7604" bpmnElement="sid-e0319c6b-316c-4f11-9818-bb4a3c685a93">
        <omgdi:waypoint x="-14.970001" y="-24.0"/>
        <omgdi:waypoint x="23.73204" y="-23.999992"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-23387667-10d7-4565-bfe9-3ff9bc04fa24" bpmnElement="sid-87f454a3-dd80-45ed-8cfe-b31533682500">
        <omgdc:Bounds x="571.7953" y="-39.0" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-116ef748-963a-489d-86dc-000d59c80918" bpmnElement="sid-c4348b1d-d319-49c3-996e-4bf53a09668e">
        <omgdi:waypoint x="512.4353" y="-24.0"/>
        <omgdi:waypoint x="571.7953" y="-24.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-27350e61-c12e-428f-b400-1250ba36b061" bpmnElement="sid-4c711416-31a4-441f-91ff-f004e70335a2">
        <omgdc:Bounds x="203.17828" y="-233.1627" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-ca2880d9-bc96-45d6-b044-f8eb2a8db79e" bpmnElement="sid-5f9fe128-e1dc-4f95-9806-f11f811cb8c5">
        <omgdc:Bounds x="198.17828" y="-163.43999" width="40.0" height="40.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-66f504e4-9c85-485e-be5a-83b57b080811" bpmnElement="sid-c22902db-b537-498b-80ca-80f10ef27f69">
        <omgdi:waypoint x="-14.970001" y="-143.44"/>
        <omgdi:waypoint x="198.17828" y="-143.43999"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge-4154d1cb-f311-4d0a-a2ae-e10dc016d67b" bpmnElement="sid-77f0e117-688d-4ec9-ab8d-708f1e3eda3a">
        <omgdi:waypoint x="218.17828" y="-163.43999"/>
        <omgdi:waypoint x="218.17828" y="-203.1627"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge-7b1d8532-dd72-4784-848d-1cbcda72f81c" bpmnElement="sid-8f7022c5-98c4-4518-bd66-9df07cb96590">
        <omgdi:waypoint x="238.17828" y="-143.43999"/>
        <omgdi:waypoint x="492.43527" y="-143.44003"/>
        <omgdi:waypoint x="492.43527" y="-44.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-a5f7205f-ade1-4625-9417-a89c629a03e9" bpmnElement="sid-e8681835-d9c2-46db-988a-69329dd4e8e6">
        <omgdc:Bounds x="23.73204" y="-105.08775" width="428.89246" height="162.17552"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-53bc044b-7236-420d-b448-e5b95ba5f124" bpmnElement="sid-c25f9150-f65c-4f4f-b68f-435513be2cfe">
        <omgdi:waypoint x="-14.970001" y="99.85983"/>
        <omgdi:waypoint x="492.43527" y="99.85983"/>
        <omgdi:waypoint x="492.43527" y="-4.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-98938dec-dfac-443f-941d-61352617f002" bpmnElement="sid-c915ca2e-07ea-4551-9486-071b12ca9a75">
        <omgdc:Bounds x="40.701683" y="-53.26657" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-0986c699-6bc6-4792-a8b9-e5407493f7d1" bpmnElement="sid-08f8df5d-e397-4c00-8cf6-481c7b3f3b3f">
        <omgdc:Bounds x="89.83081" y="-78.74213" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-7a1725a0-bfa0-4633-9be7-2d184e3042cc" bpmnElement="sid-0ee7ed53-3e26-4b57-967a-7daca3cf7b34">
        <omgdi:waypoint x="70.70168" y="-38.26657"/>
        <omgdi:waypoint x="89.83081" y="-38.742126"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-acc1260b-a6ea-466c-889e-dd8f8febfe84" bpmnElement="sid-a8959d20-bcc8-404f-a57c-488f5cf68ee7">
        <omgdc:Bounds x="288.61197" y="-78.742134" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-d0b2f31d-d762-4ffe-b756-3bb7dbbbec99" bpmnElement="sid-7affbd7a-cf2c-4979-bf71-9038271c0540">
        <omgdi:waypoint x="189.83081" y="-38.742126"/>
        <omgdi:waypoint x="219.12936" y="-38.742126"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-87c8118d-4545-40dd-90be-6f08f66ebd92" bpmnElement="sid-ed37695c-0449-4b66-b978-8b87c3f87062">
        <omgdc:Bounds x="417.01123" y="-53.742134" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-2165ac9f-237e-4fbc-ae8b-b5aefd5dbc2b" bpmnElement="sid-75a8b19e-52c0-4828-a78b-65d97e7445ee">
        <omgdi:waypoint x="388.61197" y="-38.742134"/>
        <omgdi:waypoint x="417.01123" y="-38.742134"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-e1ff47d6-9b36-48dc-b99d-aa1fc92dc773" bpmnElement="sid-56b471fa-8b46-4f21-ad10-4d00f38abbda">
        <omgdc:Bounds x="219.12936" y="-58.742126" width="40.0" height="40.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-7078755c-7ee3-494e-817d-6dac987f283b" bpmnElement="sid-d6e685f3-5451-49e3-8552-ca35b526fd1c">
        <omgdi:waypoint x="259.12936" y="-38.742126"/>
        <omgdi:waypoint x="288.61197" y="-38.742134"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-db69326d-58fb-43c8-820b-012c3c815997" bpmnElement="sid-27c262f9-1b1c-40aa-b1b2-8ec0b65ef597">
        <omgdc:Bounds x="224.12935" y="14.401535" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-4a8cd3a1-e086-4c08-aa6f-4da31d7c750f" bpmnElement="sid-ef1f99b2-7898-4cb2-9fb9-dc008e5fc77c">
        <omgdi:waypoint x="239.12936" y="-18.742126"/>
        <omgdi:waypoint x="239.12935" y="14.401535"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge-e6a4a7a6-f742-44ac-b286-9b9aaa32fda0" bpmnElement="sid-b9772e80-3f33-4f9d-baef-b7f43dbd034c">
        <omgdi:waypoint x="452.6245" y="-23.999992"/>
        <omgdi:waypoint x="472.43527" y="-24.0"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

测试流程

@Test
public void deployAndStartProcess() {
    // 部署流程
    Deployment deploy = repositoryService.createDeployment()
            .addClasspathResource("processes/terminate.bpmn20.xml")
            .deploy();
    System.out.println("deploy = " + deploy);

    // 启动流程
    Map<String, Object> variables = new HashMap<>();
    variables.put("superAdmin", "zhangsan");
    variables.put("admin", "lisi");
    variables.put("userGroup", CollectionUtil.newArrayList("group1", "group2"));
    String processDefinitionKey = "terminate";
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, processDefinitionKey + ":100001", variables);
    System.out.println("processInstance = " + processInstance);

    // 输出当前任务列表
    this.printTaskList(processInstance.getId());
}

@Test
public void doneTask() {
    // 查询任务
    Task task = taskService.createTaskQuery().processInstanceId("8cce09c3-4092-11ee-8531-18c04dcd4aee").taskAssignee("lisi").singleResult();
    System.out.println("task = " + task);
    // 完成任务
    Map<String, Object> hashMap = new HashMap<>();
    hashMap.put("done", true); // true则终止流程
    taskService.complete(task.getId(), hashMap);

    // 输出当前任务列表
    this.printTaskList(task.getProcessInstanceId());
}

流程执行步骤

// terminateAll,父流程设置为true,子流程设置为false
// 1.部署并启动流程
// 2.当前任务为超级管理员、管理员、用户
// 3.超级管理员完成任务并done=true,整个流程终止并结束
// 4.管理员完成任务,进入子流程,完成保存任务done=true,终止子流程,继续父流程任务。

// terminateAll,父流程设置为true,子流程设置为true
// 1.部署并启动流程
// 2.当前任务为超级管理员、管理员、用户
// 3.超级管理员完成任务并done=true,整个流程终止并结束
// 4.管理员完成任务,进入子流程,完成保存任务done=true,整个流程终止并结束

文章来源:https://blog.csdn.net/weixin_51833408/article/details/132549736
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。