Activiti错误结束事件入门实例

当执行流到达错误结束事件时,会结束该执行流并且抛出错误,该错误可以被错误边界事件捕获,如果没有定义任何的错误边界事件,那么其将会被当作无指定错误事件执行。因此,错误结束事件一般使用在子流程当中,如果错误事件结束,就会触发依附在该子流程上的错误边界事件。错误结束事件和错误边界事件需要一起使用才有意义,错误结束事件触发错误边界事件,错误边界时间捕获错误结束事件抛出的错误信息,而它们一般在嵌套子流程和调用子流程中使用。

当执行流到达错误结束事件时,会结束该执行流并且抛出错误,该错误可以被错误边界事件捕获,如果没有定义任何的错误边界事件,那么其将会被当作无指定错误事件执行。因此,错误结束事件一般使用在子流程当中,如果错误事件结束,就会触发依附在该子流程上的错误边界事件。错误结束事件和错误边界事件需要一起使用才有意义,错误结束事件触发错误边界事件,错误边界时间捕获错误结束事件抛出的错误信息,而它们一般在嵌套子流程和调用子流程中使用。

错误结束事件图标:

错误结束事件

实例流程图:

错误结束事件实例流程

启动流程后自动进入子流程的Sub Task任务节点,然后进入单路选择,如果flag为true,直接完成,跳转到End Tast任务节点。如果flag为false,进入流程错误结束事件,抛出异常信息,错误边界事件捕获到错误信息,进入Error Task任务节点。

注意:此处的错误结束事件、错误边界事件引用同一个错误信息

流程XML定义:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="https://activiti.org/bpmn" xmlns:bpmndi="https://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="https://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="https://www.omg.org/spec/DD/20100524/DI" typeLanguage="https://www.w3.org/2001/XMLSchema" expressionLanguage="https://www.w3.org/1999/XPath" targetNamespace="https://www.activiti.org/processdef">
  <error id="myError" errorCode="myErrorCode"></error>
  
  <process id="errorEndEventTest01" name="错误结束事件测试01" isExecutable="true">
    <startEvent id="start"></startEvent>
    
    <subProcess id="sid-525B581B-6F82-415A-813F-A49B4ADD398C" name="subProcess">
      <startEvent id="subStart01"></startEvent>
      <userTask id="subTask01" name="Sub Task"></userTask>
      <exclusiveGateway id="sid-EA59908D-27A0-4C09-B956-994FBCCAAD87"></exclusiveGateway>
      <endEvent id="subEnd"></endEvent>
      <endEvent id="subErrorEnd">
        <errorEventDefinition errorRef="myError"></errorEventDefinition>
      </endEvent>
      <sequenceFlow id="sid-4E3F6734-59E4-4F7E-B57D-A700FFD48142" sourceRef="subStart01" targetRef="subTask01"></sequenceFlow>
      <sequenceFlow id="sid-6A5C0BA4-5800-442D-BE25-AE43BC4F5AFD" sourceRef="subTask01" targetRef="sid-EA59908D-27A0-4C09-B956-994FBCCAAD87"></sequenceFlow>
      <sequenceFlow id="errorEndF02" sourceRef="sid-EA59908D-27A0-4C09-B956-994FBCCAAD87" targetRef="subErrorEnd">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!flag}]]></conditionExpression>
      </sequenceFlow>
      <sequenceFlow id="errorEndF01" sourceRef="sid-EA59908D-27A0-4C09-B956-994FBCCAAD87" targetRef="subEnd">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag}]]></conditionExpression>
      </sequenceFlow>
    </subProcess>
    <userTask id="endTask01" name="End Task"></userTask>
    <endEvent id="end"></endEvent>
    
    <boundaryEvent id="errorBoundEvent01" attachedToRef="sid-525B581B-6F82-415A-813F-A49B4ADD398C">
      <errorEventDefinition errorRef="myError"></errorEventDefinition>
    </boundaryEvent>
    <userTask id="errorTask01" name="Error Task"></userTask>
    
    <sequenceFlow id="sid-332962AE-DF19-462E-959E-50C620A7372E" sourceRef="start" targetRef="sid-525B581B-6F82-415A-813F-A49B4ADD398C"></sequenceFlow>
    <sequenceFlow id="sid-519DCA9C-ADD3-4E45-B6D4-545463F7D93B" sourceRef="endTask01" targetRef="end"></sequenceFlow>
    <sequenceFlow id="sid-81B3FDC3-6DBD-4B6F-AC7E-F5BAEDE3A911" sourceRef="sid-525B581B-6F82-415A-813F-A49B4ADD398C" targetRef="endTask01"></sequenceFlow>
    <sequenceFlow id="sid-FF1E911B-14F5-489A-AD85-9F5E3379716A" sourceRef="errorBoundEvent01" targetRef="errorTask01"></sequenceFlow>
    
  </process>

  <bpmndi:BPMNDiagram id="BPMNDiagram_errorEndEventTest01">
    <bpmndi:BPMNPlane bpmnElement="errorEndEventTest01" id="BPMNPlane_errorEndEventTest01">
      <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
        <omgdc:Bounds height="30.0" width="30.0" x="49.0" y="144.59090911061313"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-525B581B-6F82-415A-813F-A49B4ADD398C" id="BPMNShape_sid-525B581B-6F82-415A-813F-A49B4ADD398C">
        <omgdc:Bounds height="157.18181822122622" width="397.5537180489298" x="125.0" y="81.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="subStart01" id="BPMNShape_subStart01">
        <omgdc:Bounds height="30.0" width="30.0" x="157.0" y="146.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="subTask01" id="BPMNShape_subTask01">
        <omgdc:Bounds height="80.0" width="100.0" x="221.8181735424958" y="121.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-EA59908D-27A0-4C09-B956-994FBCCAAD87" id="BPMNShape_sid-EA59908D-27A0-4C09-B956-994FBCCAAD87">
        <omgdc:Bounds height="40.0" width="40.0" x="377.0" y="141.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="subEnd" id="BPMNShape_subEnd">
        <omgdc:Bounds height="28.0" width="28.0" x="462.0" y="108.18181670401711"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="subErrorEnd" id="BPMNShape_subErrorEnd">
        <omgdc:Bounds height="28.0" width="28.0" x="462.0" y="189.99999674883762"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endTask01" id="BPMNShape_endTask01">
        <omgdc:Bounds height="80.0" width="100.0" x="570.0" y="121.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
        <omgdc:Bounds height="28.0" width="28.0" x="715.0" y="147.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="errorTask01" id="BPMNShape_errorTask01">
        <omgdc:Bounds height="80.0" width="100.0" x="399.0908969729403" y="258.1818134528547"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="errorBoundEvent01" id="BPMNShape_errorBoundEvent01">
        <omgdc:Bounds height="30.0" width="30.0" x="325.5692952636832" y="223.36128951947114"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-519DCA9C-ADD3-4E45-B6D4-545463F7D93B" id="BPMNEdge_sid-519DCA9C-ADD3-4E45-B6D4-545463F7D93B">
        <omgdi:waypoint x="670.0" y="161.0"></omgdi:waypoint>
        <omgdi:waypoint x="715.0" y="161.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="errorEndF01" id="BPMNEdge_errorEndF01">
        <omgdi:waypoint x="397.5" y="141.5"></omgdi:waypoint>
        <omgdi:waypoint x="397.5" y="122.18181670401711"></omgdi:waypoint>
        <omgdi:waypoint x="462.0" y="122.18181670401711"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="errorEndF02" id="BPMNEdge_errorEndF02">
        <omgdi:waypoint x="397.5" y="180.5"></omgdi:waypoint>
        <omgdi:waypoint x="397.5" y="203.99999674883762"></omgdi:waypoint>
        <omgdi:waypoint x="462.0" y="203.99999674883762"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-6A5C0BA4-5800-442D-BE25-AE43BC4F5AFD" id="BPMNEdge_sid-6A5C0BA4-5800-442D-BE25-AE43BC4F5AFD">
        <omgdi:waypoint x="321.8181735424958" y="161.19891499594377"></omgdi:waypoint>
        <omgdi:waypoint x="377.42011619990706" y="161.42011619990706"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-4E3F6734-59E4-4F7E-B57D-A700FFD48142" id="BPMNEdge_sid-4E3F6734-59E4-4F7E-B57D-A700FFD48142">
        <omgdi:waypoint x="187.0" y="161.0"></omgdi:waypoint>
        <omgdi:waypoint x="221.8181735424958" y="161.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-FF1E911B-14F5-489A-AD85-9F5E3379716A" id="BPMNEdge_sid-FF1E911B-14F5-489A-AD85-9F5E3379716A">
        <omgdi:waypoint x="340.5692952636832" y="253.36128951947114"></omgdi:waypoint>
        <omgdi:waypoint x="340.5692952636832" y="298.1818134528547"></omgdi:waypoint>
        <omgdi:waypoint x="399.0908969729403" y="298.1818134528547"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-332962AE-DF19-462E-959E-50C620A7372E" id="BPMNEdge_sid-332962AE-DF19-462E-959E-50C620A7372E">
        <omgdi:waypoint x="79.0" y="159.59090911061313"></omgdi:waypoint>
        <omgdi:waypoint x="125.0" y="159.59090911061313"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-81B3FDC3-6DBD-4B6F-AC7E-F5BAEDE3A911" id="BPMNEdge_sid-81B3FDC3-6DBD-4B6F-AC7E-F5BAEDE3A911">
        <omgdi:waypoint x="522.5537180489298" y="161.0"></omgdi:waypoint>
        <omgdi:waypoint x="570.0" y="161.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

Java客户端代码:

package com.bug315.event;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;


/**
 * 错误结束事件测试
 * @author Administrator
 *
 */
public class ErrorEndEventTest01 {
	public static void main(String[] args) {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		RepositoryService repositoryService = engine.getRepositoryService();
		repositoryService.createDeployment().addClasspathResource("bpmn/errorEndEventTest01.bpmn20.xml").deploy();
		
		RuntimeService runtimeService = engine.getRuntimeService();
		runtimeService.startProcessInstanceByKey("errorEndEventTest01");
		
		TaskService taskService = engine.getTaskService();
		// 结束子流程中的任务,并设置结束参数
		Task task = taskService.createTaskQuery().singleResult();
		Map<String, Object> taskArgs = new HashMap<String, Object>();
		taskArgs.put("flag", false);
		taskService.complete(task.getId(), taskArgs);
		
		// 查看到达的任务
		List<Task> tasks = taskService.createTaskQuery().list();
		for (Task t : tasks) {
			System.out.println(t.getId() + "  =  " + t.getName());
		}
	}
}

注意:

1、取消结束事件只能使用在事务子流程中

2、取消结束事件会触发依附在子流程上的取消边界事件(取消结束事件会被抛出,而取消边界事件则会捕获事件)

3、取消事件的触发还会导致补偿的触发

4、在BPMN2.0中对于已经完成的活动,可以使用补偿机制,而对于一些正在进行的活动,不能使用补偿机制,而使用取消机制

最大的挑战和突破在于用人,而用人最大的突破在于信任人。——马云
1 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号