activiti学习之错误边界事件

写在前面

如果是希望在服务任务,即程序执行过程中发生了某种类型的错误时将执行流流转到用户任务节点,让人工参与处理错误,此时就可以使用错误边界事件,下面我们来一起看下如何使用。

1:测试

1.1:流程设计

  • 效果图

在这里插入图片描述
服务任务节点执行的类如下:

public class MyErrorBoundaryDelegate implements JavaDelegate, Serializable {
    public static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    @Override
    public void execute(DelegateExecution execution) throws Exception {

//        String errorCode = "aaa";
        String errorCode = threadLocal.get();
        System.out.println("错误编码是:" + errorCode);
        if ("aaa".equalsIgnoreCase(errorCode)) {
            System.out.println("抛出错误errorCode:"+ errorCode);
            throw new BpmnError(errorCode);
        } else {
            System.out.println("服务任务节点正常执行了!!!");
        }
    }
}

在这里插入图片描述

  • 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">
    <process id="errorBoundary" name="错误边界事件" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <serviceTask id="servicetask1" name="服务任务节点" activiti:class="com.jh.activiti.MyErrorBoundaryDelegate"></serviceTask>
        <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
        <endEvent id="endevent1" name="End"></endEvent>
        <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
        <userTask id="usertask1" name="ERROR TASK" activiti:assignee="errorUser"></userTask>
        <boundaryEvent id="boundaryerror1" name="Error" attachedToRef="servicetask1">
            <errorEventDefinition errorRef="aaa"></errorEventDefinition>
        </boundaryEvent>
        <sequenceFlow id="flow3" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
        <sequenceFlow id="flow4" sourceRef="boundaryerror1" targetRef="usertask1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_errorBoundary">
        <bpmndi:BPMNPlane bpmnElement="errorBoundary" id="BPMNPlane_errorBoundary">
            <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="230.0" y="90.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
                <omgdc:Bounds height="55.0" width="105.0" x="390.0" y="80.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="boundaryerror1" id="BPMNShape_boundaryerror1">
                <omgdc:Bounds height="30.0" width="30.0" x="428.0" y="124.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="610.0" y="90.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
                <omgdc:Bounds height="55.0" width="105.0" x="390.0" y="220.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
                <omgdi:waypoint x="265.0" y="107.0"></omgdi:waypoint>
                <omgdi:waypoint x="390.0" y="107.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
                <omgdi:waypoint x="495.0" y="107.0"></omgdi:waypoint>
                <omgdi:waypoint x="610.0" y="107.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
                <omgdi:waypoint x="495.0" y="247.0"></omgdi:waypoint>
                <omgdi:waypoint x="627.0" y="247.0"></omgdi:waypoint>
                <omgdi:waypoint x="627.0" y="125.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
                <omgdi:waypoint x="443.0" y="154.0"></omgdi:waypoint>
                <omgdi:waypoint x="442.0" y="220.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>

1.2:流程部署

/**
 * 部署流程定义
 */
@Test
public void deploy() {
    Deployment deployment = repositoryService.createDeployment() // 创建部署
            .addClasspathResource("com/jh/activiti/error_boundary_event.bpmn20.xml") // 加载流程资源文件
            .name("错误边界事件部署") // 流程名称
            .deploy(); // 部署
    System.out.println("流程部署ID:" + deployment.getId());
    System.out.println("流程部署Name:" + deployment.getName());
}

在这里插入图片描述
在这里插入图片描述

1.3:启动流程实例不触发错误边界事件

/**
 * 启动流程实例
 */
@Test
public void start() {
    // 设置为aaa,此时会触发异常边界事件,进入异常处理任务
//        MyErrorBoundaryDelegate.threadLocal.set("aaa");
    // 设置为非aaa,不会抛出异常,也不会进入到异常处理错误节点
    MyErrorBoundaryDelegate.threadLocal.set("bbb");
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("errorBoundary"); // 流程定义表的KEY字段值
    System.out.println("流程实例ID:" + pi.getId());
    System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}

输出如下:

错误编码是:bbb
服务任务节点正常执行了!!!
流程实例ID:1045002
流程定义ID:errorBoundary:1:497504

1.4:启动流程实例触发错误边界事件

/**
 * 启动流程实例
 */
@Test
public void start() {
    // 设置为aaa,此时会触发异常边界事件,进入异常处理任务
    MyErrorBoundaryDelegate.threadLocal.set("aaa");
    // 设置为非aaa,不会抛出异常,也不会进入到异常处理错误节点
//        MyErrorBoundaryDelegate.threadLocal.set("bbb");
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("errorBoundary"); // 流程定义表的KEY字段值
    System.out.println("流程实例ID:" + pi.getId());
    System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}

输出:

错误编码是:aaa
抛出错误errorCode:aaa
流程实例ID:1047502
流程定义ID:errorBoundary:1:497504

此时会生成ERROR TASK用户任务,如下:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值