文章目录
部分简介摘抄自官方文档
*后台服务基于Springboot2 + Activiti6,整合文章请参考:https://blog.csdn.net/yy756127197/article/details/101211510 不需要流程设计器就排除3,4步骤 *
1. 开始事件
1.1 简介
开始事件用来指明流程在哪里开始,分为空开始事件,消息开始事件,信号开始事件,定时器开始事件,错误开始事件。
2. 空开始事件
2.1 简介
空开始事件意味着没有指定启动流程实例的触发条件。
启动方式如下:
ProcessInstance processInstance = runtimeService.startProcessInstanceByXXX();
之前所有事例都是空开始事件,就不在实践
3. 定时器开始事件
3.1 简介
定时器开始事件当符合时间条件时触发。常用场景,例如:定时查看系统运行情况。
3.2 流程设计
场景:系统每五秒执行一次任务。
设置消息定义
流程图
流程文件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="timerStart" name="timerStart" isExecutable="true">
<documentation>timerStart</documentation>
<startEvent id="sid-9C504F6F-3405-4549-AA58-AAE4D66EC7E7" activiti:isInterrupting="false">
<timerEventDefinition>
<timeCycle>0/5 * * * * ?</timeCycle>
</timerEventDefinition>
</startEvent>
<sequenceFlow id="sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A" sourceRef="sid-9C504F6F-3405-4549-AA58-AAE4D66EC7E7" targetRef="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3"></sequenceFlow>
<serviceTask id="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" name="任务执行" activiti:class="com.it.cloud.activiti.event.delegate.RunDelegate"></serviceTask>
<endEvent id="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30"></endEvent>
<sequenceFlow id="sid-60A17606-B25B-4DB3-8163-1828D578B951" sourceRef="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" targetRef="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_timerStart">
<bpmndi:BPMNPlane bpmnElement="timerStart" id="BPMNPlane_timerStart">
<bpmndi:BPMNShape bpmnElement="sid-9C504F6F-3405-4549-AA58-AAE4D66EC7E7" id="BPMNShape_sid-9C504F6F-3405-4549-AA58-AAE4D66EC7E7">
<omgdc:Bounds height="31.0" width="31.0" x="99.5" y="162.5"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" id="BPMNShape_sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3">
<omgdc:Bounds height="80.0" width="100.0" x="225.0" y="138.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30" id="BPMNShape_sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30">
<omgdc:Bounds height="28.0" width="28.0" x="405.0" y="164.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A" id="BPMNEdge_sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A">
<omgdi:waypoint x="131.49992138499977" y="178.4498435066301"></omgdi:waypoint>
<omgdi:waypoint x="225.0" y="178.15673981191222"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-60A17606-B25B-4DB3-8163-1828D578B951" id="BPMNEdge_sid-60A17606-B25B-4DB3-8163-1828D578B951">
<omgdi:waypoint x="325.0" y="178.0"></omgdi:waypoint>
<omgdi:waypoint x="405.0" y="178.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
3.3 测试类
TimerStartTest.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.List;
/**
* 定时器开始事件
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class TimerStartTest {
@Autowired
private IActReModelService actReModelService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private RepositoryService repositoryService;
@Autowired
private TaskService taskService;
@Autowired
private HistoryService historyService;
/**
* 部署流程定义
*/
@Test
public void deploy() throws InterruptedException {
Deployment deployment = repositoryService.createDeployment() // 创建部署
.addClasspathResource("diagrams/timerStart.bpmn20.xml") // 加载流程资源文件
.name("timerStart流程") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
// 睡一会
Thread.sleep(1000 * 30);
}
}
3.4 运行
运行deploy()
效果:
流程每5秒启动一次
4. 消息开始事件
4.1 简介
开始时间假如消息定义。
启动方式:
runtimeService.startProcessByMessage("abc");
4.2 流程设计
场景:系统每五秒执行一次任务。
设置消息定义
引用消息
流程图
流程文件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">
<message id="msg" name="msg"></message>
<process id="messageStart" name="messageStart" isExecutable="true">
<documentation>messageStart</documentation>
<sequenceFlow id="sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A" sourceRef="sid-015F02BE-1F2C-4F71-9C0F-3530DF654D01" targetRef="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3"></sequenceFlow>
<serviceTask id="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" name="任务执行" activiti:class="com.it.cloud.activiti.event.delegate.RunDelegate"></serviceTask>
<endEvent id="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30"></endEvent>
<sequenceFlow id="sid-60A17606-B25B-4DB3-8163-1828D578B951" sourceRef="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" targetRef="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30"></sequenceFlow>
<startEvent id="sid-015F02BE-1F2C-4F71-9C0F-3530DF654D01" activiti:isInterrupting="false">
<messageEventDefinition messageRef="msg"></messageEventDefinition>
</startEvent>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_messageStart">
<bpmndi:BPMNPlane bpmnElement="messageStart" id="BPMNPlane_messageStart">
<bpmndi:BPMNShape bpmnElement="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" id="BPMNShape_sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3">
<omgdc:Bounds height="80.0" width="100.0" x="225.0" y="138.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30" id="BPMNShape_sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30">
<omgdc:Bounds height="28.0" width="28.0" x="405.0" y="164.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-015F02BE-1F2C-4F71-9C0F-3530DF654D01" id="BPMNShape_sid-015F02BE-1F2C-4F71-9C0F-3530DF654D01">
<omgdc:Bounds height="30.0" width="30.5" x="100.0" y="163.25"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A" id="BPMNEdge_sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A">
<omgdi:waypoint x="132.00828794655752" y="178.8960242082079"></omgdi:waypoint>
<omgdi:waypoint x="225.0" y="178.3133133366055"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-60A17606-B25B-4DB3-8163-1828D578B951" id="BPMNEdge_sid-60A17606-B25B-4DB3-8163-1828D578B951">
<omgdi:waypoint x="325.0" y="178.0"></omgdi:waypoint>
<omgdi:waypoint x="405.0" y="178.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
4.3 测试类
MessageStartTest.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.List;
/**
* 消息开始事件
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class MessageStartTest {
@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/messageStart.bpmn20.xml") // 加载流程资源文件
.name("messageStart流程") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
/**
* 启动流程实例
*/
@Test
public void start() throws InterruptedException {
ProcessInstance pi = runtimeService.startProcessInstanceByMessage("msg"); // 流程定义表的KEY字段值
System.out.println("流程实例ID:" + pi.getId());
System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}
}
4.4 运行
运行deploy()
4.4.1 启动任务
运行start()
效果:
5. 错误开始事件
5.1 简介
错误开始事件可以用来触发一个事件子流程。
5.2 流程设计
场景:检查端口占用情况
设置消息定义
任务执行设置
import org.activiti.engine.delegate.BpmnError;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import java.net.Socket;
public class ErrorStartRunDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {
System.out.println("开始检查2222端口");
try {
Socket socket = new Socket("127.0.0.1", 2222);
System.out.println("端口检查完成");
} catch (Exception e) {
System.out.println("检查异常");
throw new BpmnError("error");
}
}
}
错误处理任务
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class ErrorDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {
System.out.println("错误处理执行...");
}
}
流程图
流程文件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">
<message id="msg" name="msg"></message>
<process id="errorStart" name="errorStart" isExecutable="true">
<documentation>errorStart</documentation>
<sequenceFlow id="sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A" sourceRef="sid-1EDD2676-D00E-4B23-93F1-9FF1901FBC71" targetRef="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3"></sequenceFlow>
<serviceTask id="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" name="任务执行" activiti:class="com.it.cloud.activiti.event.delegate.ErrorStartRunDelegate"></serviceTask>
<endEvent id="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30"></endEvent>
<sequenceFlow id="sid-60A17606-B25B-4DB3-8163-1828D578B951" sourceRef="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" targetRef="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30"></sequenceFlow>
<subProcess id="sid-AF0C8E16-582C-4DBA-B068-BAFE8440A78D" name="subProcess" triggeredByEvent="true">
<serviceTask id="sid-8C9D7A2D-AD5B-4EE6-8CFD-F92EBEE27620" name="错误处理" activiti:class="com.it.cloud.activiti.event.delegate.ErrorDelegate"></serviceTask>
<endEvent id="sid-E4F3DBDE-68A1-4863-90FC-2CDA0BBD505E"></endEvent>
<startEvent id="sid-AB7EFCB2-C377-4A44-9C82-ACC740872D52" activiti:isInterrupting="false">
<errorEventDefinition errorRef="error"></errorEventDefinition>
</startEvent>
<sequenceFlow id="sid-4809F55B-C08E-412A-944E-10C97C4D9A19" sourceRef="sid-AB7EFCB2-C377-4A44-9C82-ACC740872D52" targetRef="sid-8C9D7A2D-AD5B-4EE6-8CFD-F92EBEE27620"></sequenceFlow>
<sequenceFlow id="sid-994DE044-6D3E-4D7F-973A-ACB5B3368D50" sourceRef="sid-8C9D7A2D-AD5B-4EE6-8CFD-F92EBEE27620" targetRef="sid-E4F3DBDE-68A1-4863-90FC-2CDA0BBD505E"></sequenceFlow>
</subProcess>
<startEvent id="sid-1EDD2676-D00E-4B23-93F1-9FF1901FBC71"></startEvent>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_errorStart">
<bpmndi:BPMNPlane bpmnElement="errorStart" id="BPMNPlane_errorStart">
<bpmndi:BPMNShape bpmnElement="sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3" id="BPMNShape_sid-5F0F7A32-E193-4114-B1E1-8C7773A96BA3">
<omgdc:Bounds height="80.0" width="100.0" x="502.875" y="255.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30" id="BPMNShape_sid-1B8ED453-645D-4E0A-9DB0-8EFFBBB07E30">
<omgdc:Bounds height="28.0" width="28.0" x="682.875" y="281.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-AF0C8E16-582C-4DBA-B068-BAFE8440A78D" id="BPMNShape_sid-AF0C8E16-582C-4DBA-B068-BAFE8440A78D">
<omgdc:Bounds height="170.0" width="425.0" x="323.5" y="30.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-8C9D7A2D-AD5B-4EE6-8CFD-F92EBEE27620" id="BPMNShape_sid-8C9D7A2D-AD5B-4EE6-8CFD-F92EBEE27620">
<omgdc:Bounds height="80.0" width="100.0" x="494.5" y="75.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-E4F3DBDE-68A1-4863-90FC-2CDA0BBD505E" id="BPMNShape_sid-E4F3DBDE-68A1-4863-90FC-2CDA0BBD505E">
<omgdc:Bounds height="28.0" width="28.0" x="674.5" y="101.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-AB7EFCB2-C377-4A44-9C82-ACC740872D52" id="BPMNShape_sid-AB7EFCB2-C377-4A44-9C82-ACC740872D52">
<omgdc:Bounds height="30.0" width="30.0" x="369.75" y="100.375"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-1EDD2676-D00E-4B23-93F1-9FF1901FBC71" id="BPMNShape_sid-1EDD2676-D00E-4B23-93F1-9FF1901FBC71">
<omgdc:Bounds height="30.0" width="30.0" x="378.125" y="280.375"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A" id="BPMNEdge_sid-E097B34F-1F65-4EAA-877E-EEE1CA44363A">
<omgdi:waypoint x="409.6083817420028" y="296.12952465584846"></omgdi:waypoint>
<omgdi:waypoint x="502.875" y="295.3942037124846"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-60A17606-B25B-4DB3-8163-1828D578B951" id="BPMNEdge_sid-60A17606-B25B-4DB3-8163-1828D578B951">
<omgdi:waypoint x="602.875" y="295.0"></omgdi:waypoint>
<omgdi:waypoint x="682.875" y="295.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-4809F55B-C08E-412A-944E-10C97C4D9A19" id="BPMNEdge_sid-4809F55B-C08E-412A-944E-10C97C4D9A19">
<omgdi:waypoint x="401.2333817420028" y="116.12952465584848"></omgdi:waypoint>
<omgdi:waypoint x="494.5" y="115.39420371248465"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-994DE044-6D3E-4D7F-973A-ACB5B3368D50" id="BPMNEdge_sid-994DE044-6D3E-4D7F-973A-ACB5B3368D50">
<omgdi:waypoint x="594.5" y="115.0"></omgdi:waypoint>
<omgdi:waypoint x="674.5" y="115.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
5.3 测试类
ErrorStartTest.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.List;
/**
* 错误开始事件
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ErrorStartTest {
@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/errorStart.bpmn20.xml") // 加载流程资源文件
.name("errorStart流程") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
/**
* 启动流程实例
*/
@Test
public void start() throws InterruptedException {
ProcessInstance pi = runtimeService.startProcessInstanceByKey("errorStart"); // 流程定义表的KEY字段值
System.out.println("流程实例ID:" + pi.getId());
System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}
}
3.4 运行
运行deploy()
3.4.1 启动任务
运行start()
效果:
源码地址
IT-CLOUD-ACTIVITI6
开源项目,持续更新中,喜欢请 Star~
项目推荐
IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
开源项目,持续更新中,喜欢请 Star~