【Flowable】 错误事件

Metadata

title: 【Flowable】 错误事件
date: 2023-01-23 17:35
tags:
  - 行动阶段/完成
  - 主题场景/组件
  - 笔记空间/KnowladgeSpace/ProgramSpace/ModuleSpace
  - 细化主题/Module/Flowable
categories:
  - Flowable
keywords:
  - Flowable
description: 【Flowable】 错误事件

【Flowable】 错误事件

错误事件可以用做一个流程的开始事件或者作为一个任务或者子流程的边界事件,错误事件没有提供作用中间事件的功能,这一点和前面介绍的定时器事件和消息事件还有区别的。

1. 开始事件

错误启动事件(error start event),可用于触发事件子流程(Event Sub-Process)。错误启动事件不能用于启动流程实例

错误启动事件总是中断。我们通过案例来介绍。此处我们用 Eclipse 来绘制流程图,熟悉下 Eclipse 工具

注意:绘制的是子流程事件:

然后我们再定义一个错误,内容为:

<error id="error01" errorCode="abcd">

在 FlowableUI 中没找到错误定义的选项,我们就在流程文件中自己添加即可。

完整的流程文件

<?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/test">
<error id="error01" errorCode="abcd"></error>
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<serviceTask id="servicetask1" name="自动任务一" activiti:class="com.bobo.delegate.MyOneDelegate"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
<subProcess id="eventsubprocess1" name="Event sub Process" triggeredByEvent="true">
  <startEvent id="errorstartevent1" name="Error start">
    <errorEventDefinition errorRef="error01"></errorEventDefinition>
  </startEvent>
  <serviceTask id="servicetask2" name="自动任务二" activiti:class="com.bobo.delegate.MyTwoDelegate"></serviceTask>
  <endEvent id="endevent2" name="End"></endEvent>
  <sequenceFlow id="flow3" sourceRef="servicetask2" targetRef="endevent2"></sequenceFlow>
  <sequenceFlow id="flow4" sourceRef="errorstartevent1" targetRef="servicetask2"></sequenceFlow>
</subProcess>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
<bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
  <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
    <omgdc:Bounds height="35.0" width="35.0" x="480.0" y="260.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
    <omgdc:Bounds height="55.0" width="105.0" x="710.0" y="250.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
    <omgdc:Bounds height="35.0" width="35.0" x="930.0" y="260.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="eventsubprocess1" id="BPMNShape_eventsubprocess1">
    <omgdc:Bounds height="211.0" width="401.0" x="530.0" y="420.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="errorstartevent1" id="BPMNShape_errorstartevent1">
    <omgdc:Bounds height="35.0" width="35.0" x="600.0" y="520.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
    <omgdc:Bounds height="55.0" width="105.0" x="700.0" y="510.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
    <omgdc:Bounds height="35.0" width="35.0" x="850.0" y="520.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
    <omgdi:waypoint x="515.0" y="277.0"></omgdi:waypoint>
    <omgdi:waypoint x="710.0" y="277.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
    <omgdi:waypoint x="815.0" y="277.0"></omgdi:waypoint>
    <omgdi:waypoint x="930.0" y="277.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
    <omgdi:waypoint x="805.0" y="537.0"></omgdi:waypoint>
    <omgdi:waypoint x="850.0" y="537.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
    <omgdi:waypoint x="635.0" y="537.0"></omgdi:waypoint>
    <omgdi:waypoint x="700.0" y="537.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

然后我们在主流程中的自动任务一中我们抛出异常

public class MyOneDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {

    System.out.println("完成自动审批任务-----》MyOneDelegate" + LocalDateTime.now().toString());
    // 业务执行发现有问题 此处的errorCode需要和定义的error标签中的errorCode保持一致
    throw new BpmnError("abcd");
}
}

然后我们在自定义任务二中简单定义一个输出即可。然后我们部署任务

@Test
public void test01() throws Exception{
    ZipInputStream in = new ZipInputStream(SpringBootFlowableApplicationTests.class.getClassLoader().getResourceAsStream("错误启动事件.bar"));
    Deployment deployment = processEngine.getRepositoryService().createDeployment()
            .addZipInputStream(in)
            .name("错误启动事件")
            .deploy();
    System.out.println("-----");
}

然后我们再启动流程实例,那么自动任务一就会抛出异常,然后对应的子流程就会开始

/**
 * 启动流程实例
 *
 */
@Test
public void startProcessInstanceByKey()  throws Exception{

    processEngine.getRuntimeService()
            .startProcessInstanceById("myProcess:1:c0462994-af79-11ec-8cae-c03c59ad2248");
    System.out.println("开始启动的时间:" + LocalDateTime.now().toString());
    // 需要在此阻塞比等待长的时间
    TimeUnit.MINUTES.sleep(3);
}

输出结果获取到了我们期望的结果

通过输出结果也可以看到执行的自动任务一后,抛出错误事件abcd, 子流程触发并执行了。

2. 边界事件

定义如下的流程图:

注意绘制的时候

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/test">
<error id="error02" errorCode="a123" ></error>
<process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <serviceTask id="servicetask2" name="自动任务二" activiti:class="com.bobo.delegate.MyTwoDelegate"></serviceTask>
    <serviceTask id="servicetask3" name="自动任务三" activiti:class="com.bobo.delegate.MyThreeDelegate"></serviceTask>
    <endEvent id="endevent2" name="End"></endEvent>
    <sequenceFlow id="flow5" sourceRef="servicetask2" targetRef="endevent2"></sequenceFlow>
    <endEvent id="endevent3" name="End"></endEvent>
    <sequenceFlow id="flow6" sourceRef="servicetask3" targetRef="endevent3"></sequenceFlow>
    <subProcess id="subprocess1" name="Sub Process">
        <startEvent id="startevent2" name="Start"></startEvent>
        <serviceTask id="servicetask4" name="Service Task" activiti:class="com.bobo.delegate.MyOneDelegate"></serviceTask>
        <endEvent id="endevent4" name="End"></endEvent>
        <sequenceFlow id="flow10" sourceRef="servicetask4" targetRef="endevent4"></sequenceFlow>
        <sequenceFlow id="flow11" sourceRef="startevent2" targetRef="servicetask4"></sequenceFlow>
    </subProcess>
    <sequenceFlow id="flow8" sourceRef="startevent1" targetRef="subprocess1"></sequenceFlow>
    <sequenceFlow id="flow9" sourceRef="subprocess1" targetRef="servicetask2"></sequenceFlow>
    <boundaryEvent id="boundaryerror1" name="Error" attachedToRef="subprocess1">
        <errorEventDefinition errorRef="error02"></errorEventDefinition>
    </boundaryEvent>
    <sequenceFlow id="flow12" sourceRef="boundaryerror1" targetRef="servicetask3"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
        <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
            <omgdc:Bounds height="35.0" width="35.0" x="402.0" y="388.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
            <omgdc:Bounds height="55.0" width="105.0" x="1285.0" y="368.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
            <omgdc:Bounds height="55.0" width="105.0" x="1099.0" y="590.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
            <omgdc:Bounds height="35.0" width="35.0" x="1440.0" y="378.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="endevent3" id="BPMNShape_endevent3">
            <omgdc:Bounds height="35.0" width="35.0" x="1320.0" y="600.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="subprocess1" id="BPMNShape_subprocess1">
            <omgdc:Bounds height="271.0" width="451.0" x="660.0" y="270.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="startevent2" id="BPMNShape_startevent2">
            <omgdc:Bounds height="35.0" width="35.0" x="720.0" y="390.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="servicetask4" id="BPMNShape_servicetask4">
            <omgdc:Bounds height="55.0" width="105.0" x="850.0" y="381.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="endevent4" id="BPMNShape_endevent4">
            <omgdc:Bounds height="35.0" width="35.0" x="1030.0" y="391.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="boundaryerror1" id="BPMNShape_boundaryerror1">
            <omgdc:Bounds height="30.0" width="30.0" x="950.0" y="520.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
            <omgdi:waypoint x="1390.0" y="395.0"></omgdi:waypoint>
            <omgdi:waypoint x="1440.0" y="395.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
            <omgdi:waypoint x="1204.0" y="617.0"></omgdi:waypoint>
            <omgdi:waypoint x="1320.0" y="617.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
            <omgdi:waypoint x="955.0" y="408.0"></omgdi:waypoint>
            <omgdi:waypoint x="1030.0" y="408.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11">
            <omgdi:waypoint x="755.0" y="407.0"></omgdi:waypoint>
            <omgdi:waypoint x="850.0" y="408.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
            <omgdi:waypoint x="437.0" y="405.0"></omgdi:waypoint>
            <omgdi:waypoint x="660.0" y="405.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
            <omgdi:waypoint x="1111.0" y="405.0"></omgdi:waypoint>
            <omgdi:waypoint x="1285.0" y="395.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge bpmnElement="flow12" id="BPMNEdge_flow12">
            <omgdi:waypoint x="965.0" y="550.0"></omgdi:waypoint>
            <omgdi:waypoint x="964.0" y="617.0"></omgdi:waypoint>
            <omgdi:waypoint x="1099.0" y="617.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

对应的三个自定义任务绑定的 JavaDelegate 为:

public class MyOneDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {

    System.out.println("完成自动审批任务-----》MyOneDelegate" + LocalDateTime.now().toString());
    // 业务执行发现有问题 此处的errorCode需要和定义的error标签中的errorCode保持一致
    throw new BpmnError("a123");
}
}

public class MyTwoDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {
    System.out.println("MyTwoDelegate---->执行了" + LocalDateTime.now().toString());
}
}

public class MyThreeDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {
    System.out.println("MyThreeDelegate---->执行了" + LocalDateTime.now().toString());
}
}

然后我们部署文件

@Test
public void test01() throws Exception{
    ZipInputStream in = new ZipInputStream(SpringBootFlowableApplicationTests.class
            .getClassLoader()
            .getResourceAsStream("错误边界事件.bar"));
    Deployment deployment = processEngine.getRepositoryService().createDeployment()
            .addZipInputStream(in)
            .name("错误边界事件")
            .deploy();
    System.out.println("-----");
}

部署成功后我们再启动一个流程实例,进入到自定义任务一处会抛出异常,触发边界异常处理

输出的结果和我们预期的是一样的