好像都是中断的。非中断没测出来!!!
<error id="error1" name="errorCode1" errorCode="123456"/>
<error id="error2" name="errorCode2" errorCode="123456"/>
开始事件只能在事件子流程中被触发,也不能用于启动流程,总是中断的。
可以用于捕获工作流启动时可能出现的各种错误情况,并根据具体的业务需求进行相应的处理。
<startEvent id="sid-9f66c2ee-3281-486e-9aea-e27f29dad682" name="错误开始事件">
<errorEventDefinition errorRef="error1"/>
startEvent>
总是用于用户任务或子流程上。
当某个任务发生错误时,可以通过错误边界事件来捕获并处理该错误,以保证流程的正常执行。
<boundaryEvent id="sid-ef65efec-11aa-4c72-98c5-3cc42a2a8229" attachedToRef="sid-7f48d1e2-a7e5-4717-ad18-cbc5a78b8113" cancelActivity="false" name="错误边界事件1">
<errorEventDefinition errorRef="error1"/>
boundaryEvent>
错误结束事件就是流程抛出一个bpmn错误。然后被工作流的错误边界事件获取。
错误结束事件会在流程到达错误结束事件的时候抛出错误,并结束当前的流程分支。错误可以使用匹配的错误边界事件捕获。如果找不到匹配的错误边界事件,将会抛出异常。
<endEvent id="sid-1600e3d8-61f9-428f-82c8-c048ef84c774" name="错误结束事件">
<errorEventDefinition errorRef="error1"/>
endEvent>
<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="error-start" name="错误开始事件" isExecutable="true">
<documentation>错误开始事件流程documentation>
<startEvent id="sid-0347a044-9d2e-465b-a449-eb6699044ae4" name="开始事件"/>
<serviceTask id="sid-8accfa7d-4feb-4d3d-8088-bd2df124b57a" activiti:exclusive="true" name="服务任务1" activiti:delegateExpression="${myThrowErrorDelegateListener}"/>
<endEvent id="sid-2efe3807-4285-416c-a2b7-c3827ec433d2" name="结束事件"/>
<subProcess id="sid-9405baaf-73a2-4f6d-8c17-3ce20b221bbe" triggeredByEvent="true" activiti:exclusive="true">
<startEvent id="sid-9f66c2ee-3281-486e-9aea-e27f29dad682" name="错误开始事件">
<errorEventDefinition errorRef="error1"/>
startEvent>
<serviceTask id="sid-1b0e1eec-d26b-4a7a-94b3-8e8b5970b3fa" activiti:exclusive="true" name="子流程服务任务" activiti:delegateExpression="${myServiceTaskListener}"/>
<endEvent id="sid-35fbe381-270f-4ade-bf1b-1e32c289f946" name="子流程结束事件"/>
<sequenceFlow id="sid-36a022c4-ff28-44f7-8694-b23cf7592b68" sourceRef="sid-1b0e1eec-d26b-4a7a-94b3-8e8b5970b3fa" targetRef="sid-35fbe381-270f-4ade-bf1b-1e32c289f946"/>
<sequenceFlow id="sid-3ff4163b-067d-4e9b-8e26-ad05a1c9d7a2" sourceRef="sid-9f66c2ee-3281-486e-9aea-e27f29dad682" targetRef="sid-1b0e1eec-d26b-4a7a-94b3-8e8b5970b3fa"/>
subProcess>
<sequenceFlow id="sid-3747e8f1-b5c6-4b83-b8af-c74038303170" sourceRef="sid-8accfa7d-4feb-4d3d-8088-bd2df124b57a" targetRef="sid-2909cd9a-c18a-429b-be89-4543b6524bbc"/>
<sequenceFlow id="sid-b3c67896-5be8-4c59-b046-4c4affb36555" sourceRef="sid-0347a044-9d2e-465b-a449-eb6699044ae4" targetRef="sid-8accfa7d-4feb-4d3d-8088-bd2df124b57a"/>
<serviceTask id="sid-2909cd9a-c18a-429b-be89-4543b6524bbc" activiti:exclusive="true" name="服务任务2" activiti:delegateExpression="${myServiceTaskListener}"/>
<sequenceFlow id="sid-bff0ca04-cd48-4aa9-8405-66e63124e59b" sourceRef="sid-2909cd9a-c18a-429b-be89-4543b6524bbc" targetRef="sid-2efe3807-4285-416c-a2b7-c3827ec433d2"/>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_error">
<bpmndi:BPMNPlane bpmnElement="error-start" id="BPMNPlane_error">
<bpmndi:BPMNShape id="shape-8d5f52a3-a6a2-4f64-9efb-e9837679722e" bpmnElement="sid-0347a044-9d2e-465b-a449-eb6699044ae4">
<omgdc:Bounds x="-235.35788" y="16.2278" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-486bc80b-61bf-464f-be0f-a586c65eb86e" bpmnElement="sid-9405baaf-73a2-4f6d-8c17-3ce20b221bbe">
<omgdc:Bounds x="-217.49924" y="92.053925" width="221.21466" height="121.94145"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-f95a940b-f150-49f8-9d86-09923329f6d0" bpmnElement="sid-1b0e1eec-d26b-4a7a-94b3-8e8b5970b3fa">
<omgdc:Bounds x="-157.19922" y="114.25392" width="100.0" height="80.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-08ea584c-b113-4d02-9d65-156a6fb35a50" bpmnElement="sid-35fbe381-270f-4ade-bf1b-1e32c289f946">
<omgdc:Bounds x="-35.8492" y="139.25392" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-993acd65-bf4f-4337-96d4-eb6c5ce1fdab" bpmnElement="sid-36a022c4-ff28-44f7-8694-b23cf7592b68">
<omgdi:waypoint x="-57.19922" y="154.25392"/>
<omgdi:waypoint x="-35.8492" y="154.25392"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-07af35b3-f623-4fdd-99ae-675c6c7894e6" bpmnElement="sid-2efe3807-4285-416c-a2b7-c3827ec433d2">
<omgdc:Bounds x="105.030174" y="16.227798" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-bae0f412-1c38-4baf-a8d8-a4740dc0c63e" bpmnElement="sid-8accfa7d-4feb-4d3d-8088-bd2df124b57a">
<omgdc:Bounds x="-156.8919" y="-8.7722" width="100.0" height="80.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-b07f91be-b837-448d-a8a7-0892ac5b39a2" bpmnElement="sid-3747e8f1-b5c6-4b83-b8af-c74038303170">
<omgdi:waypoint x="-56.891907" y="31.2278"/>
<omgdi:waypoint x="-23.466818" y="31.227797"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-f2b44140-d7d8-4110-8001-8eb61b3ae601" bpmnElement="sid-9f66c2ee-3281-486e-9aea-e27f29dad682">
<omgdc:Bounds x="-207.4492" y="139.25392" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-26b2554a-e5f0-4b44-a70a-00b3ff5812be" bpmnElement="sid-3ff4163b-067d-4e9b-8e26-ad05a1c9d7a2">
<omgdi:waypoint x="-177.4492" y="154.25392"/>
<omgdi:waypoint x="-157.19922" y="154.25392"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-ea9c91b4-9a2b-4e63-bba6-4f36d68c4f81" bpmnElement="sid-b3c67896-5be8-4c59-b046-4c4affb36555">
<omgdi:waypoint x="-205.35788" y="31.2278"/>
<omgdi:waypoint x="-156.8919" y="31.2278"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-f8f20ee5-036e-492f-b7af-33dde9d12290" bpmnElement="sid-2909cd9a-c18a-429b-be89-4543b6524bbc">
<omgdc:Bounds x="-23.466816" y="-8.772202" width="100.0" height="80.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-004bd7a2-8a83-4bb1-a732-2afcd4bf131e" bpmnElement="sid-bff0ca04-cd48-4aa9-8405-66e63124e59b">
<omgdi:waypoint x="76.53319" y="31.227798"/>
<omgdi:waypoint x="105.030174" y="31.227798"/>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
@Test
public void deployAndStartProcessByErrorStart() {
// 部署流程
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("processes/error-start.bpmn20.xml")
.deploy();
System.out.println("deploy = " + deploy);
// 启动流程
String processDefinitionKey = "error-start";
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, processDefinitionKey + ":100001");
System.out.println("processInstance = " + processInstance);
}
// 1.部署并启动流程
// 2.触发服务任务,服务任务监听器抛出异常,错误码errorCode
// 3.子流程接收到错误码为errorCode,触发错误开始事件
// 4.触发子流程服务任务
// 5.结束流程
流程图设计一个子流程,然后子流程里面有一个错误结束事件。外面的错误捕获边界任务就会捕获异常。捕获了异常后由于是错误边界先去捕获异常,所以捕获错误事件分支会先执行,然后结束。正常结束事件节点所在的分支不会执行。
<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="error-boundary-end" name="错误边界与结束事件" isExecutable="true">
<documentation>错误边界与结束事件documentation>
<startEvent id="sid-0347a044-9d2e-465b-a449-eb6699044ae4" name="开始事件"/>
<subProcess id="sid-9405baaf-73a2-4f6d-8c17-3ce20b221bbe" activiti:exclusive="true">
<startEvent id="sid-9f66c2ee-3281-486e-9aea-e27f29dad682" name="子流程开始事件"/>
<sequenceFlow id="sid-3ff4163b-067d-4e9b-8e26-ad05a1c9d7a2" sourceRef="sid-9f66c2ee-3281-486e-9aea-e27f29dad682" targetRef="sid-1b0e1eec-d26b-4a7a-94b3-8e8b5970b3fa"/>
<userTask id="sid-1b0e1eec-d26b-4a7a-94b3-8e8b5970b3fa" name="保存信息任务" activiti:assignee="${assignee}">
<extensionElements>
<activiti:taskListener event="create" delegateExpression="${myUserTaskListener}"/>
extensionElements>
userTask>
<exclusiveGateway id="sid-8a8b73a7-b4ac-49f7-b572-c1cb77be5734"/>
<endEvent id="sid-216515ff-91cb-4ec3-8d1e-c36a128f2dbf" name="保存成功"/>
<endEvent id="sid-1600e3d8-61f9-428f-82c8-c048ef84c774" name="错误结束事件">
<errorEventDefinition errorRef="error1"/>
endEvent>
<sequenceFlow id="sid-bfbfcc21-b6db-4275-9726-0560d0c397dd" sourceRef="sid-1b0e1eec-d26b-4a7a-94b3-8e8b5970b3fa" targetRef="sid-8a8b73a7-b4ac-49f7-b572-c1cb77be5734"/>
<sequenceFlow id="sid-82b537d9-8377-4d7a-a3e1-7d204afe45dd" sourceRef="sid-8a8b73a7-b4ac-49f7-b572-c1cb77be5734" targetRef="sid-216515ff-91cb-4ec3-8d1e-c36a128f2dbf" name="保存成功">
<conditionExpression xsi:type="tFormalExpression">conditionExpression>
sequenceFlow>
<sequenceFlow id="sid-1905e28e-c5ea-4827-913e-c3c4853056db" sourceRef="sid-8a8b73a7-b4ac-49f7-b572-c1cb77be5734" targetRef="sid-1600e3d8-61f9-428f-82c8-c048ef84c774" name="保存失败">
<conditionExpression xsi:type="tFormalExpression">conditionExpression>
sequenceFlow>
subProcess>
<boundaryEvent id="sid-ddef4f3c-3395-42e5-baa4-1122338da889" attachedToRef="sid-9405baaf-73a2-4f6d-8c17-3ce20b221bbe" cancelActivity="false" name="错误边界事件">
<errorEventDefinition errorRef="error1"/>
boundaryEvent>
<sequenceFlow id="sid-792f09fd-d9f8-44a3-abe6-c01a42f5f1d8" sourceRef="sid-ddef4f3c-3395-42e5-baa4-1122338da889" targetRef="sid-9405baaf-73a2-4f6d-8c17-3ce20b221bbe"/>
<serviceTask id="sid-8accfa7d-4feb-4d3d-8088-bd2df124b57a" activiti:exclusive="true" name="服务任务" activiti:delegateExpression="${myServiceTaskListener}"/>
<endEvent id="sid-2efe3807-4285-416c-a2b7-c3827ec433d2" name="结束事件1"/>
<sequenceFlow id="sid-3747e8f1-b5c6-4b83-b8af-c74038303170" sourceRef="sid-8accfa7d-4feb-4d3d-8088-bd2df124b57a" targetRef="sid-2efe3807-4285-416c-a2b7-c3827ec433d2"/>
<sequenceFlow id="sid-e12fb2e5-97ad-458a-9954-8531754b6bb1" sourceRef="sid-0347a044-9d2e-465b-a449-eb6699044ae4" targetRef="sid-9405baaf-73a2-4f6d-8c17-3ce20b221bbe"/>
<sequenceFlow id="sid-1296aafe-77e0-46ed-be02-40e77e483512" sourceRef="sid-9405baaf-73a2-4f6d-8c17-3ce20b221bbe" targetRef="sid-8accfa7d-4feb-4d3d-8088-bd2df124b57a"/>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_error">
<bpmndi:BPMNPlane bpmnElement="error-boundary-end" id="BPMNPlane_error">
<bpmndi:BPMNShape id="shape-8d5f52a3-a6a2-4f64-9efb-e9837679722e" bpmnElement="sid-0347a044-9d2e-465b-a449-eb6699044ae4">
<omgdc:Bounds x="-366.3918" y="140.0239" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-486bc80b-61bf-464f-be0f-a586c65eb86e" bpmnElement="sid-9405baaf-73a2-4f6d-8c17-3ce20b221bbe">
<omgdc:Bounds x="-289.65228" y="96.042305" width="321.05652" height="117.96317"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-f95a940b-f150-49f8-9d86-09923329f6d0" bpmnElement="sid-1b0e1eec-d26b-4a7a-94b3-8e8b5970b3fa">
<omgdc:Bounds x="-228.40327" y="117.33385" width="100.0" height="80.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-07af35b3-f623-4fdd-99ae-675c6c7894e6" bpmnElement="sid-2efe3807-4285-416c-a2b7-c3827ec433d2">
<omgdc:Bounds x="225.4221" y="139.25394" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-bae0f412-1c38-4baf-a8d8-a4740dc0c63e" bpmnElement="sid-8accfa7d-4feb-4d3d-8088-bd2df124b57a">
<omgdc:Bounds x="79.65822" y="114.25394" width="100.0" height="80.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-b07f91be-b837-448d-a8a7-0892ac5b39a2" bpmnElement="sid-3747e8f1-b5c6-4b83-b8af-c74038303170">
<omgdi:waypoint x="179.65819" y="154.25394"/>
<omgdi:waypoint x="225.4221" y="154.25394"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-f2b44140-d7d8-4110-8001-8eb61b3ae601" bpmnElement="sid-9f66c2ee-3281-486e-9aea-e27f29dad682">
<omgdc:Bounds x="-280.5647" y="142.33385" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-26b2554a-e5f0-4b44-a70a-00b3ff5812be" bpmnElement="sid-3ff4163b-067d-4e9b-8e26-ad05a1c9d7a2">
<omgdi:waypoint x="-250.5647" y="157.33385"/>
<omgdi:waypoint x="-228.40327" y="157.33385"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-61a3490a-54fd-4ab1-a770-b76eb42652a1" bpmnElement="sid-e12fb2e5-97ad-458a-9954-8531754b6bb1">
<omgdi:waypoint x="-336.3918" y="155.0239"/>
<omgdi:waypoint x="-289.65228" y="155.02391"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-5d002380-d175-4884-815c-14fc1759d1a4" bpmnElement="sid-1296aafe-77e0-46ed-be02-40e77e483512">
<omgdi:waypoint x="31.404236" y="155.0239"/>
<omgdi:waypoint x="79.65822" y="154.25394"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-522ed462-bae8-444e-a8b9-477fda868b8c" bpmnElement="sid-ddef4f3c-3395-42e5-baa4-1122338da889">
<omgdc:Bounds x="-116.11634" y="197.29118" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-8a740a35-cad6-4347-8ef6-1a46a2ee8268" bpmnElement="sid-792f09fd-d9f8-44a3-abe6-c01a42f5f1d8">
<omgdi:waypoint x="-101.11635" y="227.29118"/>
<omgdi:waypoint x="-101.11634" y="265.13498"/>
<omgdi:waypoint x="-209.38815" y="265.13498"/>
<omgdi:waypoint x="-209.38815" y="214.0055"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-df93b38f-f194-4162-a655-ec4ee7862752" bpmnElement="sid-1600e3d8-61f9-428f-82c8-c048ef84c774">
<omgdc:Bounds x="-34.357986" y="176.21335" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-6e2c6138-5753-4047-b044-9ce53ef91f30" bpmnElement="sid-bfbfcc21-b6db-4275-9726-0560d0c397dd">
<omgdi:waypoint x="-128.40327" y="157.33385"/>
<omgdi:waypoint x="-102.3249" y="157.33388"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-ed1e9d9e-02f7-445b-be90-8e805e4b99e2" bpmnElement="sid-8a8b73a7-b4ac-49f7-b572-c1cb77be5734">
<omgdc:Bounds x="-102.32489" y="137.33386" width="40.0" height="40.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-92c333a1-02bf-4e06-a7a6-6efabb4c2c07" bpmnElement="sid-216515ff-91cb-4ec3-8d1e-c36a128f2dbf">
<omgdc:Bounds x="-34.357986" y="107.18444" width="30.0" height="30.0"/>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-e6ac924d-c7f7-428e-8f2c-189e005b8682" bpmnElement="sid-82b537d9-8377-4d7a-a3e1-7d204afe45dd">
<omgdi:waypoint x="-82.32488" y="137.33385"/>
<omgdi:waypoint x="-34.357986" y="122.18445"/>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-efeec91d-d6e8-4106-baa0-f5c292ce0e78" bpmnElement="sid-1905e28e-c5ea-4827-913e-c3c4853056db">
<omgdi:waypoint x="-82.32489" y="177.33386"/>
<omgdi:waypoint x="-34.357983" y="191.21335"/>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
@Component
public class MyThrowErrorDelegateListener implements JavaDelegate, Serializable {
private static final long serialVersionUID = 1L;
@Override
public void execute(DelegateExecution execution) {
System.out.println("《================服务任务错误监听器================》");
System.out.println("当前时间:" + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
System.out.println("delegateTask.getProcessInstanceId() = " + execution.getProcessInstanceId());
System.out.println("delegateTask.getProcessInstanceBusinessKey() = " + execution.getProcessInstanceBusinessKey());
System.out.println("delegateTask.getEventName() = " + execution.getEventName());
System.out.println("execution.getCurrentFlowElement().getName() = " + execution.getCurrentFlowElement().getName());
// 抛出异常
String errorCode = "123456";
System.out.println("抛出错误码:" + errorCode);
throw new BpmnError(errorCode);
}
}
@Test
public void deployAndStartProcessByErrorBoundaryAndEnd() {
// 部署流程
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("processes/error-boundary-end.bpmn20.xml")
.deploy();
System.out.println("deploy = " + deploy);
// 启动流程
Map<String, Object> variables = new HashMap<>();
variables.put("assignee", "user123456");
String processDefinitionKey = "error-boundary-end";
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, processDefinitionKey + ":1000011", variables);
System.out.println("processInstance = " + processInstance);
}
@Test
public void completeTaskForErrorBoundaryAndEnd() {
// 查询任务
Task task = taskService.createTaskQuery().processInstanceId(PROCESS_INSTANCE_ID).taskAssignee("user123456").singleResult();
System.out.println("task = " + task);
// 完成任务
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("save", true);
taskService.complete(task.getId(), hashMap, true);
}
// 1.部署并启动流程
// 2.当前任务保存用户任务
// 3.完成用户任务save=false
// 4.触发错误结束事件,边界事件捕获到errorCode=123456,流程循环保存用户任务(如果一直是false)
// 5.当完成用户任务save=true
// 6.子流程结束
// 7.触发服务任务
// 8.结束流程