【第十四篇】Flowable事件-错误事件

举报
波波烤鸭 发表于 2022/04/09 00:18:56 2022/04/09
【摘要】 Flowable事件之错误事件   错误事件可以用做一个流程的开始事件或者作为一个任务或者子流程的边界事件,错误事件没有提供作用中间事件的功能,这一点和前面介绍的定时器事件和消息事件还有区别的。 ...

在这里插入图片描述

Flowable事件之错误事件

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

1.开始事件

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

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

在这里插入图片描述

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

在这里插入图片描述

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

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

  
 
  • 1

在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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

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

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");
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

    @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("-----");
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

    /**
     * 启动流程实例
     *
     */
    @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);
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出结果获取到了我们期望的结果
在这里插入图片描述

通过输出结果也可以看到执行的自动任务一后,抛出错误事件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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

对应的三个自定义任务绑定的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());
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

然后我们部署文件

    @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("-----");
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

在这里插入图片描述

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

文章来源: dpb-bobokaoya-sm.blog.csdn.net,作者:波波烤鸭,版权归原作者所有,如需转载,请联系作者。

原文链接:dpb-bobokaoya-sm.blog.csdn.net/article/details/124034885

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。