Activiti6--Introduction learning--error boundary event

Introduction

Copyright statement: This article is part of the original article of the CSDN blogger "Sima Cylinder smashed", following the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Original link

Definition of error event

The error event is triggered by the specified error.

Error boundary event

Introduction

The error boundary event is attached to a certain process activity and is used to capture errors thrown in the process. It is generally used to embed sub-processes or call sub-processes. The error boundary event will receive the error signal. By setting the errorRef property, if the property is not used, the error boundary event will capture any error event without throwing the errorCode.

Design flow chart

After a service node throws an exception, it is handed over to the user node for processing.

Setting of service task
Insert picture description here
Class class that throws exception

package com.yb.activiti6.error;
import org.activiti.engine.delegate.BpmnError;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import java.io.Serializable;

/**
 * @version 1.0
 * @date 2020/7/29
 */
public class ErrorBoundaryDelegate implements JavaDelegate, Serializable {
    
    
    @Override
    public void execute(DelegateExecution delegateExecution) {
    
    
        String errorCode = "aaa";
        System.out.println("抛出错误errorCode:"+ errorCode);
        throw new BpmnError(errorCode);
    }
}

Error boundary event setting
Insert picture description here
Description: Setting
Error Code to "aaa" means that the error boundary event will capture the exception with errorCode of "aaa".
Process file bpmn

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 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" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1595988588536" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <error errorCode="aaa" id="ERR_1" name="主动抛出的异常"/>
  <process id="error" isClosed="true" isExecutable="true" name="Error" processType="None">
    <startEvent id="_2" name="开始"/>
    <serviceTask activiti:class="com.yb.activiti6.error.ErrorBoundaryDelegate" activiti:exclusive="true" id="_3" name="服务任务"/>
    <endEvent id="_4" name="结束"/>
    <userTask activiti:assignee="errorUser" activiti:exclusive="true" id="_5" name="ErrorTask"/>
    <sequenceFlow id="_6" sourceRef="_2" targetRef="_3"/>
    <boundaryEvent attachedToRef="_3" cancelActivity="true" id="_7" name="BoundaryEvent">
      <errorEventDefinition errorRef="ERR_1" id="_7_ED_1"/>
    </boundaryEvent>
    <sequenceFlow id="_8" sourceRef="_7" targetRef="_5"/>
    <sequenceFlow id="_9" sourceRef="_3" targetRef="_4"/>
  </process>
  <bpmndi:BPMNDiagram documentation="background=#000000;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
    <bpmndi:BPMNPlane bpmnElement="error">
      <bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">
        <omgdc:Bounds height="32.0" width="32.0" x="-10.0" y="265.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3">
        <omgdc:Bounds height="72.0" width="95.0" x="250.0" y="250.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="72.0" width="95.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4">
        <omgdc:Bounds height="32.0" width="32.0" x="530.0" y="265.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_5" id="Shape-_5">
        <omgdc:Bounds height="55.0" width="85.0" x="260.0" y="430.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_7" id="Shape-_7">
        <omgdc:Bounds height="32.0" width="32.0" x="285.0" y="290.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="_2" targetElement="_3">
        <omgdi:waypoint x="22.0" y="281.0"/>
        <omgdi:waypoint x="250.0" y="286.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_8" id="BPMNEdge__8" sourceElement="_7" targetElement="_5">
        <omgdi:waypoint x="301.0" y="322.0"/>
        <omgdi:waypoint x="301.0" y="430.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9" sourceElement="_3" targetElement="_4">
        <omgdi:waypoint x="345.0" y="286.0"/>
        <omgdi:waypoint x="530.0" y="281.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

Test class

package com.yb.activiti6;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.List;

/**
 * @author [email protected]
 * @version 1.0
 * @date 2020/7/23
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiDemoTest09 {
    
    
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private HistoryService historyService;


    /**
     * 发布流程,测试错误边界事件
     * @throws IOException
     */
    @Test
    public void deploymentProcesses_zip(){
    
    
        Deployment deploy = repositoryService.createDeployment()
                .name("测试-错误边界事件-1")//创建流程名称
                .addClasspathResource("processes/activiti_error.bpmn")//指定zip完成部署
                .addClasspathResource("processes/activiti_error.png")
                .deploy();
        System.out.println("部署id:"+deploy.getId());
        System.out.println("部署名称:"+deploy.getName());

    }
    /**
     * 启动流程,测试错误边界事件
     */
    @Test
    public void startProcess() throws InterruptedException{
    
    
        //可根据id,key,message启动流程
        ProcessInstance parallel = runtimeService.startProcessInstanceByKey("error");
        System.out.println("流程实例id:"+parallel.getId());
        System.out.println("流程定义id:"+parallel.getProcessDefinitionId());
        //睡一会
        Thread.sleep(1000 * 5);
        System.out.println("========================================================");
        List<Task> list = taskService.createTaskQuery().taskAssignee("errorUser").list();
        for (Task task : list) {
    
    
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }
    }
    /**
     * 查看任务
     */
    @Test
    public void queryTask(){
    
    
        List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
                .taskAssignee("errorUser") // 指定某个人
                .list();
        for (Task task : taskList) {
    
    
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }
    }
    /**
     * 提交任务
     */
    @Test
    public void completeTask2(){
    
    
        String taskId = "2508";
        taskService.complete(taskId);
    }
}

Effect: The
service task throws an errorCode of "aaa", and the error boundary event reaches the user node after being captured. The
console is as follows.
Insert picture description here
Database pending task (ErrorTask)
Insert picture description here

Guess you like

Origin blog.csdn.net/Lv_vI/article/details/107656166