SpringBoot整合Activiti7——取消结束/取消边界事件(十四)

发布时间:2024年01月17日


取消结束/取消边界事件

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

<!-- 取消结束事件 -->
<endEvent id="sid-ac48382a-e4e6-439d-8ff1-1960bc56391f" name="取消结束事件">
  <cancelEventDefinition/>
</endEvent>
<!-- 取消边界事件 当事务取消时触发 -->
<boundaryEvent id="sid-40c1a6b6-80ad-4112-b72a-294b16842a9c" name="取消边界事件" attachedToRef="sid-32b9116c-5a1d-481f-8d76-fc9fea4c0dbf" cancelActivity="true">
  <cancelEventDefinition/>
</boundaryEvent>

代码实现

在这里插入图片描述

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="cancel" name="取消结束事件" isExecutable="true">
    <documentation>取消结束事件流程</documentation>
    <!-- 开始事件 -->
    <startEvent id="sid-eb025e6b-975f-4349-9c29-2c31cc5c0405" name="开始事件"/>
    <!-- 事务子流程 -->
    <transaction id="sid-32b9116c-5a1d-481f-8d76-fc9fea4c0dbf" activiti:exclusive="true" name="事务子流程">
      <!-- 子流程开始事件 -->
      <startEvent id="sid-6385af0c-2463-43d0-9de4-d72649b81716" name="子流程开始事件"/>
      <!-- 保存任务 -->
      <userTask id="sid-27eea210-df9e-4d0f-baee-5e14af7fa0a5" name="保存任务">
        <extensionElements>
          <activiti:taskListener event="create" delegateExpression="${myUserTaskListener}"/>
        </extensionElements>
      </userTask>
      <sequenceFlow id="sid-b4753597-34c1-487f-932a-f9f68b74a8be" sourceRef="sid-6385af0c-2463-43d0-9de4-d72649b81716" targetRef="sid-27eea210-df9e-4d0f-baee-5e14af7fa0a5"/>
      <!-- 是否取消结束 -->
      <exclusiveGateway id="sid-91585bb4-b51b-4c65-a0db-1b558380fdef" name="是否取消结束"/>
      <sequenceFlow id="sid-b927b27d-aa09-41cf-b396-96adfa2bc26f" sourceRef="sid-27eea210-df9e-4d0f-baee-5e14af7fa0a5" targetRef="sid-91585bb4-b51b-4c65-a0db-1b558380fdef"/>
      <!-- 删除任务 -->
      <userTask id="sid-fe803fab-650c-41fd-a815-2f7088cec9cc" name="删除任务"/>
      <sequenceFlow id="sid-1688cc14-0a84-4ba1-8ebb-9720d98355f3" sourceRef="sid-91585bb4-b51b-4c65-a0db-1b558380fdef" targetRef="sid-fe803fab-650c-41fd-a815-2f7088cec9cc" name="">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!cancel}]]></conditionExpression>
      </sequenceFlow>
      <!-- 子流程结束事件 -->
      <endEvent id="sid-becd82a2-70a5-4155-a23c-331d3504dfc2" name="子流程结束事件"/>
      <sequenceFlow id="sid-afd7e9d2-6854-4c62-9723-5d45a3f1be0e" sourceRef="sid-fe803fab-650c-41fd-a815-2f7088cec9cc" targetRef="sid-becd82a2-70a5-4155-a23c-331d3504dfc2"/>
      <!-- 取消结束事件 -->
      <endEvent id="sid-ac48382a-e4e6-439d-8ff1-1960bc56391f" name="取消结束事件">
        <cancelEventDefinition/>
      </endEvent>
      <sequenceFlow id="sid-bbf5cd76-5196-4a9d-8629-49b5ef8350a1" sourceRef="sid-91585bb4-b51b-4c65-a0db-1b558380fdef" targetRef="sid-ac48382a-e4e6-439d-8ff1-1960bc56391f" name="">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${cancel}]]></conditionExpression>
      </sequenceFlow>
      <!-- 补偿边界事件 -->
      <boundaryEvent id="sid-e212b29a-ee63-44e7-aa0d-dc3747d65292" name="补偿边界事件" attachedToRef="sid-27eea210-df9e-4d0f-baee-5e14af7fa0a5" cancelActivity="true">
        <compensateEventDefinition/>
      </boundaryEvent>
      <!-- 补偿自动任务 isForCompensation=true表示是可补偿项 -->
      <serviceTask id="sid-0cafc7be-15c3-403d-bbfc-b7ac67d2f768" activiti:exclusive="true" name="补偿自动任务" activiti:delegateExpression="${myServiceTaskListener}" isForCompensation="true"/>
      <!-- 虚线连接 -->
      <association id="sid-ea9e367e-b7ed-46ed-9403-acea8418ffef" sourceRef="sid-e212b29a-ee63-44e7-aa0d-dc3747d65292" targetRef="sid-0cafc7be-15c3-403d-bbfc-b7ac67d2f768" associationDirection="None"/>
    </transaction>
    <sequenceFlow id="sid-67028a37-79e0-44f4-b9e9-165a7680c5b7" sourceRef="sid-eb025e6b-975f-4349-9c29-2c31cc5c0405" targetRef="sid-32b9116c-5a1d-481f-8d76-fc9fea4c0dbf"/>
    <!-- 记录任务 -->
    <userTask id="sid-83e88053-e62a-4b70-b5e6-0ed796d56af1" name="记录任务"/>
    <sequenceFlow id="sid-d0f2565f-7af8-4855-aa5f-bf32b5f5b237" sourceRef="sid-32b9116c-5a1d-481f-8d76-fc9fea4c0dbf" targetRef="sid-83e88053-e62a-4b70-b5e6-0ed796d56af1"/>
    <!-- 结束事件1 -->
    <endEvent id="sid-8b29a729-c59e-4f68-b6bb-a0b846543ffb" name="结束事件1"/>
    <sequenceFlow id="sid-462b799c-7034-4205-a8bb-3a6cd91b188f" sourceRef="sid-83e88053-e62a-4b70-b5e6-0ed796d56af1" targetRef="sid-8b29a729-c59e-4f68-b6bb-a0b846543ffb"/>
    <!-- 取消自动任务 -->
    <serviceTask id="sid-6b542f98-b063-42d4-851f-493f80564374" activiti:exclusive="true" name="取消自动任务" activiti:delegateExpression="${myServiceTaskListener}"/>
    <!-- 结束事件2 -->
    <endEvent id="sid-a27efa7c-c5fe-4066-8a35-3d8120cfe590" name="结束事件2"/>
    <sequenceFlow id="sid-d88ecf9c-4f8b-4fb5-aa92-a5a7375b7ac7" sourceRef="sid-6b542f98-b063-42d4-851f-493f80564374" targetRef="sid-a27efa7c-c5fe-4066-8a35-3d8120cfe590"/>
    <!-- 取消边界事件 当事务取消时触发 -->
    <boundaryEvent id="sid-40c1a6b6-80ad-4112-b72a-294b16842a9c" name="取消边界事件" attachedToRef="sid-32b9116c-5a1d-481f-8d76-fc9fea4c0dbf" cancelActivity="true">
      <cancelEventDefinition/>
    </boundaryEvent>
    <sequenceFlow id="sid-a8360024-a3d0-485e-b423-c653cbb60fae" sourceRef="sid-40c1a6b6-80ad-4112-b72a-294b16842a9c" targetRef="sid-6b542f98-b063-42d4-851f-493f80564374"/>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_cancel">
    <bpmndi:BPMNPlane bpmnElement="cancel" id="BPMNPlane_cancel">
      <bpmndi:BPMNShape id="shape-5af49f80-3fb3-4ca4-bc14-c7a6343e7000" bpmnElement="sid-eb025e6b-975f-4349-9c29-2c31cc5c0405">
        <omgdc:Bounds x="-301.89" y="13.620003" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-a0265384-8c29-410c-a617-7e239a047498" bpmnElement="sid-32b9116c-5a1d-481f-8d76-fc9fea4c0dbf">
        <omgdc:Bounds x="-220.73544" y="-70.268005" width="461.85605" height="197.77602"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-079abd4e-0dbb-4c30-b0bb-b438452f37f3" bpmnElement="sid-67028a37-79e0-44f4-b9e9-165a7680c5b7">
        <omgdi:waypoint x="-271.89" y="28.620003"/>
        <omgdi:waypoint x="-220.73544" y="28.620003"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-5c876004-d42c-4623-97fc-c457bafcc8af" bpmnElement="sid-6385af0c-2463-43d0-9de4-d72649b81716">
        <omgdc:Bounds x="-203.23544" y="-17.268005" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-b50b51fd-688d-4994-ace4-bc0874ad86a2" bpmnElement="sid-27eea210-df9e-4d0f-baee-5e14af7fa0a5">
        <omgdc:Bounds x="-146.23544" y="-42.268005" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-321c1b08-7ea3-4401-a060-74483945b90a" bpmnElement="sid-b4753597-34c1-487f-932a-f9f68b74a8be">
        <omgdi:waypoint x="-173.23544" y="-2.2680054"/>
        <omgdi:waypoint x="-146.23544" y="-2.2680054"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-fd18cc72-51d4-44f9-902f-4f2a87ee5446" bpmnElement="sid-91585bb4-b51b-4c65-a0db-1b558380fdef">
        <omgdc:Bounds x="-15.335428" y="-21.908005" width="40.0" height="40.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-595ca4d5-1c3b-4fa9-bd81-8bcc48778238" bpmnElement="sid-b927b27d-aa09-41cf-b396-96adfa2bc26f">
        <omgdi:waypoint x="-46.235443" y="-2.2680054"/>
        <omgdi:waypoint x="-15.335428" y="-1.9080048"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-50f054f4-8626-4142-a7e1-b98704b87bab" bpmnElement="sid-fe803fab-650c-41fd-a815-2f7088cec9cc">
        <omgdc:Bounds x="55.224556" y="-41.908005" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-c8918ebd-2e83-4ac9-acc6-7626293ee63a" bpmnElement="sid-1688cc14-0a84-4ba1-8ebb-9720d98355f3">
        <omgdi:waypoint x="24.664572" y="-1.9080048"/>
        <omgdi:waypoint x="55.224556" y="-1.9080048"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-d81534d0-a534-489e-a4bf-46f27801d652" bpmnElement="sid-becd82a2-70a5-4155-a23c-331d3504dfc2">
        <omgdc:Bounds x="189.86458" y="-16.908005" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-e0a7c399-2755-461b-ae68-856e02062ea7" bpmnElement="sid-afd7e9d2-6854-4c62-9723-5d45a3f1be0e">
        <omgdi:waypoint x="155.22455" y="-1.9080048"/>
        <omgdi:waypoint x="189.86458" y="-1.9080048"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-08675595-5495-4f42-a5eb-d4239dffc0b1" bpmnElement="sid-ac48382a-e4e6-439d-8ff1-1960bc56391f">
        <omgdc:Bounds x="-10.335426" y="56.031998" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-3b18b889-402a-4442-8867-f3296f3ae543" bpmnElement="sid-bbf5cd76-5196-4a9d-8629-49b5ef8350a1">
        <omgdi:waypoint x="4.664572" y="18.091995"/>
        <omgdi:waypoint x="4.6645737" y="56.031998"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-9a2e9546-8f2a-483b-b39f-c556f84740d4" bpmnElement="sid-e212b29a-ee63-44e7-aa0d-dc3747d65292">
        <omgdc:Bounds x="-136.42197" y="5.1343784" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-fa56114a-edee-4f22-88e2-4af5e131bed2" bpmnElement="sid-0cafc7be-15c3-403d-bbfc-b7ac67d2f768">
        <omgdc:Bounds x="-199.55963" y="64.099304" width="112.24" height="46.01599"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-e69dbfc3-b99c-48fc-af7e-ce7d31359bde" bpmnElement="sid-83e88053-e62a-4b70-b5e6-0ed796d56af1">
        <omgdc:Bounds x="277.642" y="-11.379997" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-769ab1cf-04aa-414d-a24b-71defc5b709b" bpmnElement="sid-d0f2565f-7af8-4855-aa5f-bf32b5f5b237">
        <omgdi:waypoint x="241.1206" y="28.620003"/>
        <omgdi:waypoint x="277.642" y="28.620003"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-68ee2a71-3a1f-42ca-bda8-c5c17f889573" bpmnElement="sid-8b29a729-c59e-4f68-b6bb-a0b846543ffb">
        <omgdc:Bounds x="415.0181" y="13.620004" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-2ccbb2b9-bed8-4600-a862-6ce704a890b3" bpmnElement="sid-462b799c-7034-4205-a8bb-3a6cd91b188f">
        <omgdi:waypoint x="377.642" y="28.620003"/>
        <omgdi:waypoint x="415.0181" y="28.620003"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-4db33039-76ad-4aae-bb49-215545466432" bpmnElement="sid-40c1a6b6-80ad-4112-b72a-294b16842a9c">
        <omgdc:Bounds x="140.20752" y="114.21324" width="40.0" height="40.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-9c4eb2bf-82cf-48f3-82ec-aae138fadc35" bpmnElement="sid-6b542f98-b063-42d4-851f-493f80564374">
        <omgdc:Bounds x="277.642" y="129.45961" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-1a30aecb-4c39-4203-9dc7-057c05209996" bpmnElement="sid-a8360024-a3d0-485e-b423-c653cbb60fae">
        <omgdi:waypoint x="160.20752" y="154.21323"/>
        <omgdi:waypoint x="278.5125" y="169.4596"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-34eb2e83-1eb5-4684-b13a-13a2d0433ffc" bpmnElement="sid-a27efa7c-c5fe-4066-8a35-3d8120cfe590">
        <omgdc:Bounds x="415.0181" y="154.45961" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-4b0685b5-ae93-477d-aacb-6d42f0265ded" bpmnElement="sid-d88ecf9c-4f8b-4fb5-aa92-a5a7375b7ac7">
        <omgdi:waypoint x="377.642" y="169.45961"/>
        <omgdi:waypoint x="415.0181" y="169.45961"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge-dfa84c5e-6841-4358-aa77-d4bce66d2d16" bpmnElement="sid-ea9e367e-b7ed-46ed-9403-acea8418ffef">
        <omgdi:waypoint x="-121.42197" y="35.134377"/>
        <omgdi:waypoint x="-143.43964" y="64.099304"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

测试流程

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

    // 启动流程
    String processDefinitionKey = "cancel";
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, processDefinitionKey + ":100001");
    System.out.println("processInstance = " + processInstance);

    // 完成保存任务
    Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
    System.out.println("task = " + task);
    Map<String, Object> hashMap = new HashMap<>();
    hashMap.put("cancel", true); // true取消结束
    taskService.complete(task.getId(), hashMap);
}

流程执行步骤

// 1.部署启动流程并完成保存任务
// 2.是否取消结束=true
// 3.触发取消结束事件
// 4.触发取消边界事件
// 5.触发补偿自动任务、触发取消自动任务
// 6.结束流程
文章来源:https://blog.csdn.net/weixin_51833408/article/details/132549793
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。