文章目录
- 补偿边界/补偿中间事件
- 代码实现
- xml文件
- 测试流程
- 流程执行步骤
补偿边界/补偿中间事件
补偿事件可以被触发来回滚或修复之前已经完成的任务或活动。
补偿事件通常与错误边界事件(Error Boundary Event)结合使用。当任务或活动发生异常时,错误边界事件将被触发,进而触发相应的补偿事件。
补偿任务必须设置isForCompensation=true,否则报错。
<!-- 补偿边界事件 -->
<boundaryEvent id="sid-e212b29a-ee63-44e7-aa0d-dc3747d65292" name="补偿边界事件" attachedToRef="sid-27eea210-df9e-4d0f-baee-5e14af7fa0a5" cancelActivity="true">
<compensateEventDefinition/>
</boundaryEvent>
<!-- 补偿中间事件-抛出 -->
<intermediateThrowEvent id="sid-d1d8eca9-f921-4e29-9112-2872519f0a3b" name="补偿中间事件">
<compensateEventDefinition/>
</intermediateThrowEvent >
<!-- 虚线连接 -->
<association id="sid-ea9e367e-b7ed-46ed-9403-acea8418ffef" sourceRef="sid-e212b29a-ee63-44e7-aa0d-dc3747d65292" targetRef="sid-0cafc7be-15c3-403d-bbfc-b7ac67d2f768" associationDirection="None"/>
代码实现
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">
<!-- 定义错误 -->
<error id="error1" name="errorCode1" errorCode="123456"/>
<process id="compensate" name="补偿事件" isExecutable="true">
<documentation>补偿事件流程</documentation>
<!-- 开始事件 -->
<startEvent id="sid-7dc3694c-934a-4d35-881b-70bbe4301fcf" name="开始事件"/>
<!-- 钱包出账 -->
<serviceTask id="sid-f12484f1-2a44-49c2-80b9-efcac32b9cf1" activiti:exclusive="true" activiti:delegateExpression="${myServiceTaskListener}" name="钱包出账"/>
<sequenceFlow id="sid-383f9645-2666-4319-8f3a-60029387d412" sourceRef="sid-7dc3694c-934a-4d35-881b-70bbe4301fcf" targetRef="sid-f12484f1-2a44-49c2-80b9-efcac32b9cf1"/>
<!-- 银行入账 -->
<serviceTask id="sid-941a247c-a43b-44b7-bf2a-c4c6c1b97f99" activiti:exclusive="true" activiti:delegateExpression="${myServiceTaskListener}" name="银行入账"/>
<sequenceFlow id="sid-05d783bf-185d-4220-83d8-ae2beccc79f4" sourceRef="sid-f12484f1-2a44-49c2-80b9-efcac32b9cf1" targetRef="sid-941a247c-a43b-44b7-bf2a-c4c6c1b97f99"/>
<!-- 检验结果 -->
<serviceTask id="sid-7ef12140-baa4-4bff-a229-74b368c49fd8" activiti:exclusive="true" activiti:delegateExpression="${myThrowErrorDelegateListener}" name="检验结果"/>
<sequenceFlow id="sid-fc528896-5253-4ecd-814d-45b04e7675fb" sourceRef="sid-941a247c-a43b-44b7-bf2a-c4c6c1b97f99" targetRef="sid-7ef12140-baa4-4bff-a229-74b368c49fd8"/>
<!-- 结束事件1 -->
<endEvent id="sid-86a2c6a3-6e83-46c5-ae45-92820324b502" name="结束事件1"/>
<sequenceFlow id="sid-798a901a-081f-477f-b370-16258ccf0e4b" sourceRef="sid-7ef12140-baa4-4bff-a229-74b368c49fd8" targetRef="sid-86a2c6a3-6e83-46c5-ae45-92820324b502"/>
<!-- 错误边界事件 -->
<boundaryEvent id="sid-e7576742-080e-4dda-9a8b-a6e5241906f0" attachedToRef="sid-7ef12140-baa4-4bff-a229-74b368c49fd8" name="错误边界事件">
<errorEventDefinition errorRef="error1"/>
</boundaryEvent>
<!-- 补偿中间事件-抛出 -->
<intermediateThrowEvent id="sid-d1d8eca9-f921-4e29-9112-2872519f0a3b" name="补偿中间事件">
<compensateEventDefinition/>
</intermediateThrowEvent >
<sequenceFlow id="sid-2fdee7fd-7171-470e-8b7f-2c0c3f351d1d" sourceRef="sid-e7576742-080e-4dda-9a8b-a6e5241906f0" targetRef="sid-d1d8eca9-f921-4e29-9112-2872519f0a3b"/>
<!-- 结束事件2 -->
<endEvent id="sid-76f8e2ab-30a0-46df-8ede-819cee3fd3c4" name="结束事件2"/>
<sequenceFlow id="sid-2435a8ad-769a-464b-8b17-4d8ccaa68db4" sourceRef="sid-d1d8eca9-f921-4e29-9112-2872519f0a3b" targetRef="sid-76f8e2ab-30a0-46df-8ede-819cee3fd3c4"/>
<!-- 钱包补偿任务 -->
<serviceTask id="sid-ae4a072e-bd85-4454-9450-a3e0982fbde4" activiti:exclusive="true" activiti:delegateExpression="${myServiceTaskListener}" isForCompensation="true" name="钱包补偿任务"/>
<!-- 银行补偿任务 -->
<serviceTask id="sid-2ea92408-adf6-474e-8d70-613f21dbf86a" activiti:exclusive="true" activiti:delegateExpression="${myServiceTaskListener}" isForCompensation="true" name="银行补偿任务"/>
<!-- 补偿边界事件 -->
<boundaryEvent id="sid-ad06646b-7513-4da5-98df-88a1f836fb52" attachedToRef="sid-f12484f1-2a44-49c2-80b9-efcac32b9cf1" name="钱包补偿边界事件">
<compensateEventDefinition/>
</boundaryEvent>
<!-- 补偿边界事件 -->
<boundaryEvent id="sid-eace335f-644a-48aa-8eab-a65ff4e7dc41" attachedToRef="sid-941a247c-a43b-44b7-bf2a-c4c6c1b97f99" name="银行补偿边界事件">
<compensateEventDefinition/>
</boundaryEvent>
<!-- 虚线连接 -->
<association id="sid-06dace8e-df61-4b33-b127-87ee5178795e" sourceRef="sid-ad06646b-7513-4da5-98df-88a1f836fb52" targetRef="sid-ae4a072e-bd85-4454-9450-a3e0982fbde4" associationDirection="None"/>
<association id="sid-8be52165-0a91-4ad3-b173-f4eb80f36776" sourceRef="sid-eace335f-644a-48aa-8eab-a65ff4e7dc41" targetRef="sid-2ea92408-adf6-474e-8d70-613f21dbf86a" associationDirection="None"/>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_compensate">
<bpmndi:BPMNPlane bpmnElement="compensate" id="BPMNPlane_compensate">
<bpmndi:BPMNShape id="shape-6cb79578-6929-4743-a0a8-3259ccb657ea" bpmnElement="sid-7dc3694c-934a-4d35-881b-70bbe4301fcf">
<omgdc:Bounds x="-266.1" y="-40.927986" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-d4995e3e-2613-4758-99f1-6992db206f3d" bpmnElement="sid-f12484f1-2a44-49c2-80b9-efcac32b9cf1">
<omgdc:Bounds x="-213.28398" y="-65.927986" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-87b1c4d7-e1fb-4c18-9fb9-14dcede6e55a" bpmnElement="sid-383f9645-2666-4319-8f3a-60029387d412">
<omgdi:waypoint x="-236.1" y="-25.927986"/>
<omgdi:waypoint x="-213.28398" y="-25.927986"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-4452552f-250a-4c44-b337-ed04ccc54199" bpmnElement="sid-941a247c-a43b-44b7-bf2a-c4c6c1b97f99">
<omgdc:Bounds x="-87.28398" y="-65.927986" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-0cd7df69-4eb7-4321-9cb9-d08d44209f72" bpmnElement="sid-05d783bf-185d-4220-83d8-ae2beccc79f4">
<omgdi:waypoint x="-113.28398" y="-25.927986"/>
<omgdi:waypoint x="-87.28398" y="-25.927986"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-4ec3eacd-51c2-45ee-8db0-16c6dba5f15c" bpmnElement="sid-7ef12140-baa4-4bff-a229-74b368c49fd8">
<omgdc:Bounds x="41.116028" y="-65.927986" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-d4331ebf-31c7-430a-b97f-84a8cc612fb1" bpmnElement="sid-fc528896-5253-4ecd-814d-45b04e7675fb">
<omgdi:waypoint x="12.716019" y="-25.927986"/>
<omgdi:waypoint x="41.116028" y="-25.927986"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-56e3283f-bf3c-4bd3-8c6c-07cb6bfe7f02" bpmnElement="sid-86a2c6a3-6e83-46c5-ae45-92820324b502">
<omgdc:Bounds x="171.31602" y="-40.927986" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-634db270-5ae4-417b-99c2-f77af08657e7" bpmnElement="sid-798a901a-081f-477f-b370-16258ccf0e4b">
<omgdi:waypoint x="141.11603" y="-25.927986"/>
<omgdi:waypoint x="171.31602" y="-25.927986"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-4661ada5-3ad5-4e19-b9e5-b9164dcf6cdf" bpmnElement="sid-e7576742-080e-4dda-9a8b-a6e5241906f0">
<omgdc:Bounds x="79.51603" y="-2.3279858" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-58b658fe-6356-413f-9b3a-0be0fe39bccf" bpmnElement="sid-d1d8eca9-f921-4e29-9112-2872519f0a3b">
<omgdc:Bounds x="90.51604" y="69.67202" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-0a0a0cf5-a146-43ac-bdc1-6c6498270b01" bpmnElement="sid-2fdee7fd-7171-470e-8b7f-2c0c3f351d1d">
<omgdi:waypoint x="94.51603" y="27.672014"/>
<omgdi:waypoint x="105.51604" y="69.67201"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-5643facb-06d8-4537-902b-f63664b4fa12" bpmnElement="sid-76f8e2ab-30a0-46df-8ede-819cee3fd3c4">
<omgdc:Bounds x="154.516" y="69.67203" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-7a79b04b-ddbc-42f7-a734-0e24880a4659" bpmnElement="sid-2435a8ad-769a-464b-8b17-4d8ccaa68db4">
<omgdi:waypoint x="120.51604" y="84.67202"/>
<omgdi:waypoint x="154.516" y="84.67203"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-91c9ebe6-43e4-4c70-9a53-b97a39ff9010" bpmnElement="sid-ae4a072e-bd85-4454-9450-a3e0982fbde4">
<omgdc:Bounds x="-225.88408" y="60.872017" width="103.0" height="47.6"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="sid-29019e4e-346b-423d-81b1-3045ea62a5e2" bpmnElement="sid-2ea92408-adf6-474e-8d70-613f21dbf86a">
<omgdc:Bounds x="-88.78398" y="60.87201" width="103.0" height="47.6"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-c3538e72-8fdc-49b1-89e5-376a19e09cac" bpmnElement="sid-ad06646b-7513-4da5-98df-88a1f836fb52">
<omgdc:Bounds x="-183.28398" y="-4.12798" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="sid-9efddae1-fabd-4af6-9a34-0b690d8ee66e" bpmnElement="sid-eace335f-644a-48aa-8eab-a65ff4e7dc41">
<omgdc:Bounds x="-55.483974" y="-1.7279825" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-f042fae5-8388-4e3a-9541-032c9023877e" bpmnElement="sid-06dace8e-df61-4b33-b127-87ee5178795e">
<omgdi:waypoint x="-168.28398" y="25.87202"/>
<omgdi:waypoint x="-174.38408" y="60.872013"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-9df10eda-2955-45e8-9ca9-56c0192a1bfe" bpmnElement="sid-8be52165-0a91-4ad3-b173-f4eb80f36776">
<omgdi:waypoint x="-40.483974" y="28.272018"/>
<omgdi:waypoint x="-37.28398" y="60.872013"/>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
测试流程
@Test
public void deployAndStartProcess() {
// 部署流程
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("processes/compensate.bpmn20.xml")
.deploy();
System.out.println("deploy = " + deploy);
// 启动流程
String processDefinitionKey = "compensate";
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, processDefinitionKey + ":1000001");
System.out.println("processInstance = " + processInstance);
}
流程执行步骤
// 1.部署并启动流程
// 2.触发钱包服务任务、银行服务任务、检验结果服务任务
// 3.触发错误边界事件,抛出错误码,触发补偿中间事件
// 4.触发钱包补偿边界事件,并触发钱包补偿任务
// 5.触发银行补偿边界事件,并触发银行补偿任务
// 6.结束流程