结束事件类型
bpmn结束事件表示流程或者分支的结束,当流程执行到结束时会抛出一个结果,是的,了解了开始事件以后,这个结束事件就相对很容易了。结束事件只有4种类型:
- 空结束事件
- 错误结束事件
- 取消结束事件
- 终止结束事件
空结束事件
空结束事件,空心粗边圆圈。
注意:当流程实例中多个流程分支被激活时,只有当最后一个分支出发空结束事件且执行结束后,流程实例才结束。
错误结束事件
当流程到达错误结束事件时会结束执行流并且抛出错误,该错误可以被错误边界事件捕获,如果没有被捕获,那么他就相当于无指定结束事件。一般用在子流程中。
比如付款失败错误
错误设置
流程文件:
<?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/processdef">
<message id="msg" name="msg"></message>
<process id="errorEnd" name="errorEnd" isExecutable="true">
<documentation>errorEnd</documentation>
<startEvent id="sid-AE775DDE-5837-43B3-8290-44A9F5EE34ED"></startEvent>
<subProcess id="sid-579DEB01-C7C9-4704-8C73-070E99663C3E" name="子流程">
<startEvent id="sid-AC21AE14-E120-444F-A872-B6FF67687A59"></startEvent>
<exclusiveGateway id="sid-3AC8191C-07E4-428F-8AD1-145C36595C44"></exclusiveGateway>
<endEvent id="sid-BEBFA059-43EB-44B2-9161-EFCE5B5D4E56"></endEvent>
<endEvent id="sid-961DE02B-8651-4B3A-8C75-C39012E6ED52">
<errorEventDefinition errorRef="error"></errorEventDefinition>
</endEvent>
<userTask id="sid-00774FED-6029-4427-83B4-319A2B22BC4C" name="服务执行" activiti:assignee="errorEndUser">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>
<sequenceFlow id="sid-A756FBC0-9AB5-464E-8A5F-DB23CAE03C22" sourceRef="sid-AC21AE14-E120-444F-A872-B6FF67687A59" targetRef="sid-00774FED-6029-4427-83B4-319A2B22BC4C"></sequenceFlow>
<sequenceFlow id="sid-C6396652-65DF-4C75-A11B-041A44E15F44" sourceRef="sid-00774FED-6029-4427-83B4-319A2B22BC4C" targetRef="sid-3AC8191C-07E4-428F-8AD1-145C36595C44"></sequenceFlow>
<sequenceFlow id="sid-9FE4C81C-85F2-4A35-86CA-EEC5E66E9880" sourceRef="sid-3AC8191C-07E4-428F-8AD1-145C36595C44" targetRef="sid-961DE02B-8651-4B3A-8C75-C39012E6ED52">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag == false}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="sid-C36A05AA-9547-44D0-89AD-21FC4C87AA5B" sourceRef="sid-3AC8191C-07E4-428F-8AD1-145C36595C44" targetRef="sid-BEBFA059-43EB-44B2-9161-EFCE5B5D4E56">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag == true}]]></conditionExpression>
</sequenceFlow>
</subProcess>
<sequenceFlow id="sid-C1D3F58A-4211-488D-AACF-A6165CDAE291" sourceRef="sid-AE775DDE-5837-43B3-8290-44A9F5EE34ED" targetRef="sid-579DEB01-C7C9-4704-8C73-070E99663C3E"></sequenceFlow>
<userTask id="sid-668CDEB1-5312-40FB-B20B-D815FD19C19D" name="错误任务" activiti:assignee="errorEndUser1">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>
<sequenceFlow id="sid-13B4425D-E6F5-49E9-9000-FB4143D7D868" sourceRef="sid-CC662244-F29F-4BE3-990E-C30C3687274C" targetRef="sid-668CDEB1-5312-40FB-B20B-D815FD19C19D"></sequenceFlow>
<boundaryEvent id="sid-CC662244-F29F-4BE3-990E-C30C3687274C" attachedToRef="sid-579DEB01-C7C9-4704-8C73-070E99663C3E">
<errorEventDefinition errorRef="error"></errorEventDefinition>
</boundaryEvent>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_errorEnd">
<bpmndi:BPMNPlane bpmnElement="errorEnd" id="BPMNPlane_errorEnd">
<bpmndi:BPMNShape bpmnElement="sid-AE775DDE-5837-43B3-8290-44A9F5EE34ED" id="BPMNShape_sid-AE775DDE-5837-43B3-8290-44A9F5EE34ED">
<omgdc:Bounds height="30.0" width="30.0" x="135.0" y="115.375"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-579DEB01-C7C9-4704-8C73-070E99663C3E" id="BPMNShape_sid-579DEB01-C7C9-4704-8C73-070E99663C3E">
<omgdc:Bounds height="208.0" width="473.0" x="255.0" y="26.375"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-AC21AE14-E120-444F-A872-B6FF67687A59" id="BPMNShape_sid-AC21AE14-E120-444F-A872-B6FF67687A59">
<omgdc:Bounds height="30.0" width="30.0" x="300.0" y="115.375"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-3AC8191C-07E4-428F-8AD1-145C36595C44" id="BPMNShape_sid-3AC8191C-07E4-428F-8AD1-145C36595C44">
<omgdc:Bounds height="40.0" width="40.0" x="570.0" y="110.375"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-BEBFA059-43EB-44B2-9161-EFCE5B5D4E56" id="BPMNShape_sid-BEBFA059-43EB-44B2-9161-EFCE5B5D4E56">
<omgdc:Bounds height="28.0" width="28.0" x="655.0" y="60.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-961DE02B-8651-4B3A-8C75-C39012E6ED52" id="BPMNShape_sid-961DE02B-8651-4B3A-8C75-C39012E6ED52">
<omgdc:Bounds height="28.0" width="28.0" x="655.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-00774FED-6029-4427-83B4-319A2B22BC4C" id="BPMNShape_sid-00774FED-6029-4427-83B4-319A2B22BC4C">
<omgdc:Bounds height="80.0" width="100.0" x="405.0" y="90.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-668CDEB1-5312-40FB-B20B-D815FD19C19D" id="BPMNShape_sid-668CDEB1-5312-40FB-B20B-D815FD19C19D">
<omgdc:Bounds height="80.0" width="100.0" x="585.0" y="270.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-CC662244-F29F-4BE3-990E-C30C3687274C" id="BPMNShape_sid-CC662244-F29F-4BE3-990E-C30C3687274C">
<omgdc:Bounds height="30.0" width="30.0" x="469.62930100571805" y="219.62464435554702"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-C6396652-65DF-4C75-A11B-041A44E15F44" id="BPMNEdge_sid-C6396652-65DF-4C75-A11B-041A44E15F44">
<omgdi:waypoint x="505.0" y="130.3228782287823"></omgdi:waypoint>
<omgdi:waypoint x="570.3700092850511" y="130.74500928505108"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-C36A05AA-9547-44D0-89AD-21FC4C87AA5B" id="BPMNEdge_sid-C36A05AA-9547-44D0-89AD-21FC4C87AA5B">
<omgdi:waypoint x="590.5" y="110.875"></omgdi:waypoint>
<omgdi:waypoint x="590.5" y="74.0"></omgdi:waypoint>
<omgdi:waypoint x="655.0" y="74.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-13B4425D-E6F5-49E9-9000-FB4143D7D868" id="BPMNEdge_sid-13B4425D-E6F5-49E9-9000-FB4143D7D868">
<omgdi:waypoint x="484.62930100571805" y="249.62464435554702"></omgdi:waypoint>
<omgdi:waypoint x="484.62930100571805" y="310.0"></omgdi:waypoint>
<omgdi:waypoint x="585.0" y="310.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-9FE4C81C-85F2-4A35-86CA-EEC5E66E9880" id="BPMNEdge_sid-9FE4C81C-85F2-4A35-86CA-EEC5E66E9880">
<omgdi:waypoint x="590.5" y="149.875"></omgdi:waypoint>
<omgdi:waypoint x="590.5" y="194.31963470319636"></omgdi:waypoint>
<omgdi:waypoint x="655.0" y="194.31963470319636"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-A756FBC0-9AB5-464E-8A5F-DB23CAE03C22" id="BPMNEdge_sid-A756FBC0-9AB5-464E-8A5F-DB23CAE03C22">
<omgdi:waypoint x="329.9999461897028" y="130.33482157270615"></omgdi:waypoint>
<omgdi:waypoint x="405.0" y="130.13392857142856"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-C1D3F58A-4211-488D-AACF-A6165CDAE291" id="BPMNEdge_sid-C1D3F58A-4211-488D-AACF-A6165CDAE291">
<omgdi:waypoint x="165.0" y="130.375"></omgdi:waypoint>
<omgdi:waypoint x="255.0" y="130.375"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
测试,然后把错误标签,或者条件赋值即可,类似于发了一个信号
import com.it.cloud.modules.activiti.service.IActReModelService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
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.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 错误结束事件
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ErrorEndTest {
@Autowired
private IActReModelService actReModelService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private RepositoryService repositoryService;
@Autowired
private TaskService taskService;
@Autowired
private HistoryService historyService;
/**
* 部署流程定义
*/
@Test
public void deploy() {
Deployment deployment = repositoryService.createDeployment() // 创建部署
.addClasspathResource("diagrams/errorEnd.bpmn20.xml") // 加载流程资源文件
.name("errorEnd流程") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
/**
* 启动流程实例
*/
@Test
public void start() throws InterruptedException {
ProcessInstance pi = runtimeService.startProcessInstanceByKey("errorEnd"); // 流程定义表的KEY字段值
System.out.println("流程实例ID:" + pi.getId());
System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
System.out.println("----------------------一条有想法的分割线------------------------");
// 查询任务
List<Task> taskList1 = taskService.createTaskQuery() // 创建任务查询
.taskAssignee("errorEndUser") // 指定某个人
.list();
for (Task task : taskList1) {
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());
// 完成任务
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("flag", false);
taskService.complete(task.getId(), variables);
}
// 睡一会
Thread.sleep(1000 * 3);
// 查询任务
List<Task> taskList2 = taskService.createTaskQuery() // 创建任务查询
.taskAssignee("errorEndUser1") // 指定某个人
.list();
for (Task task : taskList2) {
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());
}
}
}
终止结束事件
当流程到达终止结束事件时会结束当前执行流,如果activiti:terminateAll属性设置成true,那么该流程实例的所有执行流全部终止。如可以用在子流程,嵌入式子流程,调用子流程,事务字流程中。
是的,逻辑和错误结束事件类似。
取消结束事件
取消结束事件只能在事务子流程中使用,实际应用会与取消事件,事务子流程和补偿事件搭配使用。
<endEventid="sid-ac48382a-e4e6-439d-8ff1-1960bc56391f"name="取消结束事件"><cancelEventDefinition/></endEvent>
完成保存任务,然后启动
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
System.out.println("task = " + task);
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("cancel", true); // true取消结束
taskService.complete(task.getId(), hashMap);