BPMN2.0规定了错误开始事件只能使用在事件子流程(Event Sub-Process)中,该该事件不能使用在其他流程中,包括最高级流程(Top-Level Process)、嵌套子流程(Sub-Process)和调用子流程(Call Activity)。
错误消息开始事件图标:
![]()
实例流程图:

启动一个流程,然后执行“Service Task”java服务类,但是该服务类抛出了异常信息。此时被事件子流程的错误开始事件捕获,启动事件子流程,然后执行“Sub Task”java服务类。
流程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="errorId" errorCode="MyErrorCode"></error>
<process id="errorStartEventTest01" name="错误开始事件测试01" isExecutable="true">
<startEvent id="start"></startEvent>
<serviceTask id="serviceTask01" name="Service Task" activiti:class="com.bug315.event.ErrorStartEventDelegate"></serviceTask>
<endEvent id="end"></endEvent>
<sequenceFlow id="sid-1EAADC30-396A-4E19-B181-C688B909510B" sourceRef="serviceTask01" targetRef="end"></sequenceFlow>
<sequenceFlow id="sid-07875D4F-D2C7-47E0-B4C3-6866F110206F" sourceRef="start" targetRef="serviceTask01"></sequenceFlow>
<subProcess id="sid-DDBC6BA0-A097-4FC9-8023-C40E6AD0B499" name="subProcess" triggeredByEvent="true">
<startEvent id="subStart">
<errorEventDefinition errorRef="errorId"></errorEventDefinition>
</startEvent>
<serviceTask id="subServiceTask01" name="Sub Task" activiti:class="com.bug315.event.ErrorHandlerDelegate"></serviceTask>
<endEvent id="subEnd"></endEvent>
<sequenceFlow id="sid-24413ADF-BD05-4BB7-9FA8-E70154B94767" sourceRef="subServiceTask01" targetRef="subEnd"></sequenceFlow>
<sequenceFlow id="sid-A130E45F-9A0B-4362-908E-C8FEB3F640E7" sourceRef="subStart" targetRef="subServiceTask01"></sequenceFlow>
</subProcess>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_errorStartEventTest01">
<bpmndi:BPMNPlane bpmnElement="errorStartEventTest01" id="BPMNPlane_errorStartEventTest01">
<bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
<omgdc:Bounds height="30.0" width="30.0" x="205.0" y="237.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="serviceTask01" id="BPMNShape_serviceTask01">
<omgdc:Bounds height="80.0" width="100.0" x="290.0" y="212.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
<omgdc:Bounds height="28.0" width="28.0" x="440.0" y="238.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-DDBC6BA0-A097-4FC9-8023-C40E6AD0B499" id="BPMNShape_sid-DDBC6BA0-A097-4FC9-8023-C40E6AD0B499">
<omgdc:Bounds height="145.0" width="328.0" x="168.0" y="44.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="subStart" id="BPMNShape_subStart">
<omgdc:Bounds height="30.0" width="30.0" x="203.0" y="99.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="subServiceTask01" id="BPMNShape_subServiceTask01">
<omgdc:Bounds height="80.0" width="100.0" x="288.0" y="74.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="subEnd" id="BPMNShape_subEnd">
<omgdc:Bounds height="28.0" width="28.0" x="438.0" y="100.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-07875D4F-D2C7-47E0-B4C3-6866F110206F" id="BPMNEdge_sid-07875D4F-D2C7-47E0-B4C3-6866F110206F">
<omgdi:waypoint x="235.0" y="252.0"></omgdi:waypoint>
<omgdi:waypoint x="290.0" y="252.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-1EAADC30-396A-4E19-B181-C688B909510B" id="BPMNEdge_sid-1EAADC30-396A-4E19-B181-C688B909510B">
<omgdi:waypoint x="390.0" y="252.0"></omgdi:waypoint>
<omgdi:waypoint x="440.0" y="252.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-A130E45F-9A0B-4362-908E-C8FEB3F640E7" id="BPMNEdge_sid-A130E45F-9A0B-4362-908E-C8FEB3F640E7">
<omgdi:waypoint x="233.0" y="114.0"></omgdi:waypoint>
<omgdi:waypoint x="288.0" y="114.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-24413ADF-BD05-4BB7-9FA8-E70154B94767" id="BPMNEdge_sid-24413ADF-BD05-4BB7-9FA8-E70154B94767">
<omgdi:waypoint x="388.0" y="114.0"></omgdi:waypoint>
<omgdi:waypoint x="438.0" y="114.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>Activiti配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="aaaaaa" />
<property name="jobExecutorActivate" value="true" />
<property name="databaseType" value="mysql"/>
<property name="databaseSchemaUpdate" value="true" />
<property name="history" value="full"/>
</bean>
</beans>Java客户端代码:
ErrorHandlerDelegate.java
package com.bug315.event;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
/**
* 错误处理Delegate
* @author Administrator
*
*/
public class ErrorHandlerDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("ErrorHandlerDelegate ---> execute");
}
}ErrorStartEventDelegate.java
package com.bug315.event;
import org.activiti.engine.delegate.BpmnError;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
/**
* 错误开始事件处理Delegate
* @author Administrator
*
*/
public class ErrorStartEventDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("ErrorStartEventDelegate ----> execute");
throw new BpmnError("MyErrorCode");
}
}ErrorStartEventTest01.java
package com.bug315.event;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
/**
* 错误开始事件
* @author Administrator
*
*/
public class ErrorStartEventTest01 {
public static void main(String[] args) {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = engine.getRepositoryService();
repositoryService.createDeployment().addClasspathResource("bpmn/errorStartEventTest01.bpmn20.xml").deploy();
engine.getRuntimeService().startProcessInstanceByKey("errorStartEventTest01");
}
}