部分简介摘抄自官方文档
事例参考杨大仙的程序空间https://my.oschina.net/JavaLaw
*后台服务基于Springboot2 + Activiti6,整合文章请参考:https://blog.csdn.net/yy756127197/article/details/101211510 不需要流程设计器就排除3,4步骤 *
1. 定时器中间事件
1.1 简介
定时器中间事件是一个Catching事件,当执行到达捕获事件节点, 就会启动一个定时器。 当定时器触发(一段时间之后),流程就会沿着定时中间事件的外出节点继续执行。
1.2 流程设计
场景:用户支付成功后等待一分钟,商家发货
设置
说明
PT1M:一分钟后
参数设置方法同定时器边界事件
流程图
流程文件bpmn
<?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">
<process id="intermediateTimerCatch" name="intermediateTimerCatch" isExecutable="true">
<documentation>intermediateTimerCatch</documentation>
<startEvent id="startEvent1"></startEvent>
<endEvent id="sid-AFE32B9E-81CC-4EC5-ABAA-B99784EBB95A"></endEvent>
<userTask id="sid-388B4ACB-0E9E-4913-83EC-EE919DAF5A9E" name="支付" activiti:assignee="timerUser3">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>
<sequenceFlow id="sid-13C0A8C0-9C37-44CE-BC66-D63998A6D1DD" sourceRef="startEvent1" targetRef="sid-388B4ACB-0E9E-4913-83EC-EE919DAF5A9E"></sequenceFlow>
<intermediateCatchEvent id="sid-0724FCAA-6DE2-4052-A902-4C94E3FA2AA0">
<timerEventDefinition>
<timeDuration>PT1M</timeDuration>
</timerEventDefinition>
</intermediateCatchEvent>
<sequenceFlow id="sid-0D236CC4-3763-445F-8526-FC0355F02FE6" sourceRef="sid-388B4ACB-0E9E-4913-83EC-EE919DAF5A9E" targetRef="sid-0724FCAA-6DE2-4052-A902-4C94E3FA2AA0"></sequenceFlow>
<userTask id="sid-C90A139C-91FF-460C-AF11-93641F21CC3E" name="发货" activiti:assignee="timerUser4">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>
<sequenceFlow id="sid-C4801F9E-EB4F-4919-9239-9F2283B3A473" sourceRef="sid-0724FCAA-6DE2-4052-A902-4C94E3FA2AA0" targetRef="sid-C90A139C-91FF-460C-AF11-93641F21CC3E"></sequenceFlow>
<sequenceFlow id="sid-F2A021B5-A2A6-457B-B3F9-6AF1D5F479BA" sourceRef="sid-C90A139C-91FF-460C-AF11-93641F21CC3E" targetRef="sid-AFE32B9E-81CC-4EC5-ABAA-B99784EBB95A"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_intermediateTimerCatch">
<bpmndi:BPMNPlane bpmnElement="intermediateTimerCatch" id="BPMNPlane_intermediateTimerCatch">
<bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
<omgdc:Bounds height="30.0" width="30.0" x="105.0" y="145.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-AFE32B9E-81CC-4EC5-ABAA-B99784EBB95A" id="BPMNShape_sid-AFE32B9E-81CC-4EC5-ABAA-B99784EBB95A">
<omgdc:Bounds height="28.0" width="28.0" x="690.0" y="146.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-388B4ACB-0E9E-4913-83EC-EE919DAF5A9E" id="BPMNShape_sid-388B4ACB-0E9E-4913-83EC-EE919DAF5A9E">
<omgdc:Bounds height="80.0" width="100.0" x="225.0" y="120.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-0724FCAA-6DE2-4052-A902-4C94E3FA2AA0" id="BPMNShape_sid-0724FCAA-6DE2-4052-A902-4C94E3FA2AA0">
<omgdc:Bounds height="31.0" width="31.0" x="405.0" y="144.5"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-C90A139C-91FF-460C-AF11-93641F21CC3E" id="BPMNShape_sid-C90A139C-91FF-460C-AF11-93641F21CC3E">
<omgdc:Bounds height="80.0" width="100.0" x="510.0" y="120.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-C4801F9E-EB4F-4919-9239-9F2283B3A473" id="BPMNEdge_sid-C4801F9E-EB4F-4919-9239-9F2283B3A473">
<omgdi:waypoint x="436.9998964866937" y="160.4424464155155"></omgdi:waypoint>
<omgdi:waypoint x="510.0" y="160.1798561151079"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-0D236CC4-3763-445F-8526-FC0355F02FE6" id="BPMNEdge_sid-0D236CC4-3763-445F-8526-FC0355F02FE6">
<omgdi:waypoint x="325.0" y="160.17123287671234"></omgdi:waypoint>
<omgdi:waypoint x="405.0000938254085" y="160.44520580077196"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-13C0A8C0-9C37-44CE-BC66-D63998A6D1DD" id="BPMNEdge_sid-13C0A8C0-9C37-44CE-BC66-D63998A6D1DD">
<omgdi:waypoint x="135.0" y="160.0"></omgdi:waypoint>
<omgdi:waypoint x="225.0" y="160.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-F2A021B5-A2A6-457B-B3F9-6AF1D5F479BA" id="BPMNEdge_sid-F2A021B5-A2A6-457B-B3F9-6AF1D5F479BA">
<omgdi:waypoint x="610.0" y="160.0"></omgdi:waypoint>
<omgdi:waypoint x="690.0" y="160.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
1.3 测试类
IntermediateTimerCatchTest.java
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 IntermediateTimerCatchTest {
@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/intermediateTimerCatch.bpmn20.xml") // 加载流程资源文件
.name("intermediateTimerCatch流程") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
/**
* 启动流程实例
*/
@Test
public void start() throws InterruptedException {
ProcessInstance pi = runtimeService.startProcessInstanceByKey("intermediateTimerCatch"); // 流程定义表的KEY字段值
System.out.println("流程实例ID:" + pi.getId());
System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
System.out.println("-------------------完美的分割线-------------------");
Task task = taskService.createTaskQuery()
.taskAssignee("timerUser3") // 指定某个人
.singleResult();
System.out.println("task name: " + task.getName());
taskService.complete(task.getId());
System.out.println("暂停70秒钟");
Thread.sleep(1000 * 70);
task = taskService.createTaskQuery()
.taskAssignee("timerUser4") // 指定某个人
.singleResult();
System.out.println("task name: " + task.getName());
taskService.complete(task.getId());
}
}
1.4 运行
运行deploy()
1.4.1 启动任务
运行start()
效果:
源码地址
IT-CLOUD-ACTIVITI6
开源项目,持续更新中,喜欢请 Star~
项目推荐
IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
开源项目,持续更新中,喜欢请 Star~