部分简介摘抄自官方文档
参考杨大仙的程序空间https://my.oschina.net/JavaLaw
*后台服务基于Springboot2 + Activiti6,整合文章请参考:https://blog.csdn.net/yy756127197/article/details/101211510 不需要流程设计器就排除3,4步骤 *
子流程是一种特殊的流程,它包含在大流程中的一部分。
分为嵌入式子流程,调用式子流程,事件子流程,事务子流程,特别子流程(Activiti6新添加的)。
事件子流程请参考错误开始事件事例5
子流程请参考错误结束事件事例2
当流程执行到调用节点,会创建一个新分支,它是到达调用节点的流程的分支。 这个分支会用来执行子流程,默认创建并行子流程,就像一个普通的流程。 上级流程会等待子流程完成,然后才会继续向下执行。
适用于多个流程共用一个子流程的情况。
主流程和子流程间的参数传递通过In parameters和Out parameters来实现。
设置子流程
说明:
Called element :子流程key
In parameters: 入参
Out parameters: 出参
days是主流程的参数,传递到子流程,用subDays接收
subDays是子流程的参数,传递到主流程,用接收mainDays接收
流程文件bpmn
子流程
<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="subProcess" name="subProcess" isExecutable="true">
<documentation>subProcessdocumentation>
<startEvent id="startEvent1">startEvent>
<userTask id="sid-BE985349-3D74-4164-9F82-B34643CEBD76" name="班长审批" activiti:assignee="user10">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">modeler:initiator-can-complete>
extensionElements>
userTask>
<sequenceFlow id="sid-FB83ECB6-E2A1-4BDC-B5A2-5FAEEA172DF4" sourceRef="startEvent1" targetRef="sid-BE985349-3D74-4164-9F82-B34643CEBD76">sequenceFlow>
<endEvent id="sid-F38218A4-C65E-4605-94B0-D86A95BBC0E4">endEvent>
<sequenceFlow id="sid-EE473731-BF5F-4895-B558-2098898E1E1B" sourceRef="sid-BE985349-3D74-4164-9F82-B34643CEBD76" targetRef="sid-F38218A4-C65E-4605-94B0-D86A95BBC0E4">sequenceFlow>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_subProcess">
<bpmndi:BPMNPlane bpmnElement="subProcess" id="BPMNPlane_subProcess">
<bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
<omgdc:Bounds height="30.0" width="30.0" x="135.0" y="163.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-BE985349-3D74-4164-9F82-B34643CEBD76" id="BPMNShape_sid-BE985349-3D74-4164-9F82-B34643CEBD76">
<omgdc:Bounds height="80.0" width="100.0" x="255.0" y="135.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-F38218A4-C65E-4605-94B0-D86A95BBC0E4" id="BPMNShape_sid-F38218A4-C65E-4605-94B0-D86A95BBC0E4">
<omgdc:Bounds height="28.0" width="28.0" x="435.0" y="161.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-FB83ECB6-E2A1-4BDC-B5A2-5FAEEA172DF4" id="BPMNEdge_sid-FB83ECB6-E2A1-4BDC-B5A2-5FAEEA172DF4">
<omgdi:waypoint x="164.99719121576283" y="177.7097317829207">omgdi:waypoint>
<omgdi:waypoint x="255.0" y="175.96774193548387">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-EE473731-BF5F-4895-B558-2098898E1E1B" id="BPMNEdge_sid-EE473731-BF5F-4895-B558-2098898E1E1B">
<omgdi:waypoint x="355.0" y="175.0">omgdi:waypoint>
<omgdi:waypoint x="435.0" y="175.0">omgdi:waypoint>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
主流程
<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="callActivity" name="callActivity" isExecutable="true">
<documentation>callActivitydocumentation>
<startEvent id="startEvent1">startEvent>
<callActivity id="sid-385431C0-5454-4223-90E0-8FA571498B95" name="调用式子流程" calledElement="subProcess" activiti:inheritVariables="false">
<extensionElements>
<activiti:in source="days" target="subDays">activiti:in>
<activiti:out source="subDays" target="mainDays">activiti:out>
extensionElements>
callActivity>
<userTask id="sid-14207BC0-8F1A-46C5-8647-1E84D43F240E" name="新建请假单" activiti:assignee="callUser">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">modeler:initiator-can-complete>
extensionElements>
userTask>
<sequenceFlow id="sid-336874EB-B1F1-4C66-9245-EBBD8A44FB54" sourceRef="startEvent1" targetRef="sid-14207BC0-8F1A-46C5-8647-1E84D43F240E">sequenceFlow>
<sequenceFlow id="sid-9D5B4CA2-11F9-407F-BC15-EDE5EEE66B53" sourceRef="sid-14207BC0-8F1A-46C5-8647-1E84D43F240E" targetRef="sid-385431C0-5454-4223-90E0-8FA571498B95">sequenceFlow>
<sequenceFlow id="sid-5D0A34F3-B246-42A0-84CA-12EF6CF0FFD2" sourceRef="sid-385431C0-5454-4223-90E0-8FA571498B95" targetRef="sid-E0AC13EA-BD3F-400B-B73E-994FBB4EFC39">sequenceFlow>
<userTask id="sid-E0AC13EA-BD3F-400B-B73E-994FBB4EFC39" name="完成" activiti:assignee="callUser1">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">modeler:initiator-can-complete>
extensionElements>
userTask>
<endEvent id="sid-9E651FE5-A294-4E65-97EA-828804D976E5">endEvent>
<sequenceFlow id="sid-79B537E9-7B0A-46BE-B096-1E44C6D6F496" sourceRef="sid-E0AC13EA-BD3F-400B-B73E-994FBB4EFC39" targetRef="sid-9E651FE5-A294-4E65-97EA-828804D976E5">sequenceFlow>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_callActivity">
<bpmndi:BPMNPlane bpmnElement="callActivity" id="BPMNPlane_callActivity">
<bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
<omgdc:Bounds height="30.0" width="30.0" x="105.0" y="160.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-385431C0-5454-4223-90E0-8FA571498B95" id="BPMNShape_sid-385431C0-5454-4223-90E0-8FA571498B95">
<omgdc:Bounds height="84.0" width="151.0" x="450.0" y="133.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-14207BC0-8F1A-46C5-8647-1E84D43F240E" id="BPMNShape_sid-14207BC0-8F1A-46C5-8647-1E84D43F240E">
<omgdc:Bounds height="80.0" width="100.0" x="225.0" y="135.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-E0AC13EA-BD3F-400B-B73E-994FBB4EFC39" id="BPMNShape_sid-E0AC13EA-BD3F-400B-B73E-994FBB4EFC39">
<omgdc:Bounds height="80.0" width="100.0" x="690.0" y="135.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-9E651FE5-A294-4E65-97EA-828804D976E5" id="BPMNShape_sid-9E651FE5-A294-4E65-97EA-828804D976E5">
<omgdc:Bounds height="28.0" width="28.0" x="870.0" y="161.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-336874EB-B1F1-4C66-9245-EBBD8A44FB54" id="BPMNEdge_sid-336874EB-B1F1-4C66-9245-EBBD8A44FB54">
<omgdi:waypoint x="135.0" y="175.0">omgdi:waypoint>
<omgdi:waypoint x="225.0" y="175.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-9D5B4CA2-11F9-407F-BC15-EDE5EEE66B53" id="BPMNEdge_sid-9D5B4CA2-11F9-407F-BC15-EDE5EEE66B53">
<omgdi:waypoint x="325.0" y="175.0">omgdi:waypoint>
<omgdi:waypoint x="450.0" y="175.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-5D0A34F3-B246-42A0-84CA-12EF6CF0FFD2" id="BPMNEdge_sid-5D0A34F3-B246-42A0-84CA-12EF6CF0FFD2">
<omgdi:waypoint x="601.0" y="175.0">omgdi:waypoint>
<omgdi:waypoint x="690.0" y="175.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-79B537E9-7B0A-46BE-B096-1E44C6D6F496" id="BPMNEdge_sid-79B537E9-7B0A-46BE-B096-1E44C6D6F496">
<omgdi:waypoint x="790.0" y="175.0">omgdi:waypoint>
<omgdi:waypoint x="870.0" y="175.0">omgdi:waypoint>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
CallActivityTest.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 CallActivityTest {
@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/subProcess.bpmn20.xml") // 子流程资源文件
.addClasspathResource("diagrams/callActivity.bpmn20.xml") // 主流程资源文件
.name("callActivity流程") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
/**
* 启动流程实例
*/
@Test
public void start() {
ProcessInstance pi = runtimeService.startProcessInstanceByKey("callActivity"); // 流程定义表的KEY字段值
System.out.println("流程实例ID:" + pi.getId());
System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
//查询任务
List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
.taskAssignee("callUser") // 指定某个人
.list();
for (Task task : taskList) {
System.out.println("--------------------------------------------");
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> vars = new HashMap<String, Object>();
vars.put("days", 10);
// 完成任务
taskService.complete(task.getId(), vars);
}
// 主流程中查询参数
Integer days = (Integer) runtimeService.getVariable(pi.getId(), "days");
System.out.println("主流程中参数days:" + days);
// 调用式子流程中查询参数
ProcessInstance subPi = runtimeService.createProcessInstanceQuery()
.processDefinitionKey("subProcess")
.singleResult();
Integer subDays = (Integer) runtimeService.getVariable(subPi.getId(), "subDays");
System.out.println("子流程中参数subDays:" + subDays);
// 设置子流程参数
runtimeService.setVariable(subPi.getId(), "subDays", subDays -1);
try {
Thread.sleep(1000 * 3);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 查询任务
taskList = taskService.createTaskQuery() // 创建任务查询
.taskAssignee("user10") // 指定某个人
.list();
for (Task task : taskList) {
System.out.println("--------------------------------------------");
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());
// 完成任务
taskService.complete(task.getId());
}
//查询任务
taskList = taskService.createTaskQuery() // 创建任务查询
.taskAssignee("callUser1") // 指定某个人
.list();
for (Task task : taskList) {
System.out.println("--------------------------------------------");
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());
}
System.out.println("--------------------------------------------");
// 主流程中查询参数
Integer mainDays = (Integer) runtimeService.getVariable(pi.getId(), "mainDays");
System.out.println("主流程中参数mainDays:" + mainDays);
}
/**
* 查看任务
*/
@Test
public void queryTask() {
List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
.taskAssignee("callUser1") // 指定某个人
.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 completeTask() {
taskService.complete("270011");
}
}
运行deploy()
运行start()
IT-CLOUD-ACTIVITI6
开源项目,持续更新中,喜欢请 Star~
IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
开源项目,持续更新中,喜欢请 Star~