背景
上一节已经搭建了,具体的开发环境,数据库,并且找了一个可以用bpmnjs流程设计器,这一些,我们对核心api做个基础的实操,有个感性的认知,另外对数据库和基本数据流动有个理解。
部署
模板部署
为了接口调用,暂时不使用线上的流程设计器,使用安装的可视化流程设计器插件
然后如下目录结构下,构建一个标准的bpmn文件
画的比较急促,usertask中,都没有名字,下面的数据库中也没有名字,当然这节我们是感性认知,主要能能跑一些接口就行
<?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="apply" name="apply" isExecutable="true"><startEvent id="sid-93eb9656-e7fa-46db-b867-5bf8c8ccfd28"/><userTask id="sid-f436faf4-75c5-4004-993d-fde4c2efbd15" activiti:assignee="starter"/><userTask id="sid-d78d057a-230f-4d2d-9b73-74907342c644" activiti:assignee="manager"/><userTask id="sid-110669bd-2386-4fd1-a66f-82250f2971b9" activiti:candidateUsers="duster,hrer"/><sequenceFlow id="sid-cf63b67a-6287-4b98-ad3d-3f5a6aab6456" sourceRef="sid-93eb9656-e7fa-46db-b867-5bf8c8ccfd28" targetRef="sid-f436faf4-75c5-4004-993d-fde4c2efbd15"/><sequenceFlow id="sid-dc3e5702-3c9e-4f4f-a5aa-5d12d62e0531" sourceRef="sid-f436faf4-75c5-4004-993d-fde4c2efbd15" targetRef="sid-d78d057a-230f-4d2d-9b73-74907342c644"/><sequenceFlow id="sid-f68d50f0-438c-42e6-b4a8-5441554908c7" sourceRef="sid-d78d057a-230f-4d2d-9b73-74907342c644" targetRef="sid-110669bd-2386-4fd1-a66f-82250f2971b9"/><endEvent id="sid-68257716-77d4-464b-a07d-8a62969df00d"/><sequenceFlow id="sid-b9534685-9617-4e88-9e24-622d58959337" sourceRef="sid-110669bd-2386-4fd1-a66f-82250f2971b9" targetRef="sid-68257716-77d4-464b-a07d-8a62969df00d"/></process><bpmndi:BPMNDiagram id="BPMNDiagram_apply"><bpmndi:BPMNPlane bpmnElement="apply" id="BPMNPlane_apply"><bpmndi:BPMNShape id="shape-b4673c70-2eb5-4096-9631-454c2d4f35f4" bpmnElement="sid-93eb9656-e7fa-46db-b867-5bf8c8ccfd28"><omgdc:Bounds x="-100.0" y="-65.0" width="30.0" height="30.0"/></bpmndi:BPMNShape><bpmndi:BPMNShape id="shape-6cfc9bde-25fa-42f3-82fd-9a681b20e3eb" bpmnElement="sid-f436faf4-75c5-4004-993d-fde4c2efbd15"><omgdc:Bounds x="-35.0" y="-90.0" width="100.0" height="80.0"/></bpmndi:BPMNShape><bpmndi:BPMNShape id="shape-6d9ee9d0-6a3e-4b3b-82ab-d901adcaac05" bpmnElement="sid-d78d057a-230f-4d2d-9b73-74907342c644"><omgdc:Bounds x="115.0" y="-90.0" width="100.0" height="80.0"/></bpmndi:BPMNShape><bpmndi:BPMNShape id="shape-6d716fe2-0c5c-47f5-ab0b-fb5265c9fe58" bpmnElement="sid-110669bd-2386-4fd1-a66f-82250f2971b9"><omgdc:Bounds x="250.0" y="-90.0" width="100.0" height="80.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-edb64b8a-b281-4f74-9d4c-3b41480e7f15" bpmnElement="sid-cf63b67a-6287-4b98-ad3d-3f5a6aab6456"><omgdi:waypoint x="-70.0" y="-50.0"/><omgdi:waypoint x="-35.0" y="-50.0"/></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="edge-0a7821f9-6b78-4009-a6cd-70ad14e278cf" bpmnElement="sid-dc3e5702-3c9e-4f4f-a5aa-5d12d62e0531"><omgdi:waypoint x="65.0" y="-50.0"/><omgdi:waypoint x="115.0" y="-50.0"/></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="edge-5e5f8326-5409-417a-85ab-4b455830a086" bpmnElement="sid-f68d50f0-438c-42e6-b4a8-5441554908c7"><omgdi:waypoint x="215.0" y="-50.0"/><omgdi:waypoint x="250.0" y="-50.0"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-de420915-45e2-4275-9a4a-8096e3f5b152" bpmnElement="sid-68257716-77d4-464b-a07d-8a62969df00d"><omgdc:Bounds x="385.0" y="-65.0" width="30.0" height="30.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-2fe4e62f-ea4a-442c-9842-8b9690acb4f5" bpmnElement="sid-b9534685-9617-4e88-9e24-622d58959337"><omgdi:waypoint x="350.0" y="-50.0"/><omgdi:waypoint x="385.0" y="-50.0"/></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>
以下是流程发布代码
@Test
public void GetDeployInfos() {// 流程模板部署,发布
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();RepositoryService repositoryService = processEngine.getRepositoryService();Deployment deployment = repositoryService.createDeployment().addClasspathResource("apply.bpmn20.xml").addClasspathResource("apply.png").name("申请示例").key("apply").deploy();System.out.println(deployment.getId());
这是print的内容:
2024-04-12 18:07:26.602 INFO 21652 --- [ main] o.a.e.impl.bpmn.deployer.BpmnDeployer : Process deployed: {id: apply:1:76ccd0ba-f8b4-11ee-a0f0-8c8caa7a4f9c, key: apply, name: apply }
76b996d7-f8b4-11ee-a0f0-8c8caa7a4f9c
这是相关的数据库:
发布查询
// 部署后,查询流程模板
// 查询流程部署
Deployment deployment = repositoryService.createDeploymentQuery().deploymentKey("apply").singleResult();
// 查询流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
// 流程定义是否被挂起/暂停
boolean suspended = processDefinition.isSuspended();
System.out.println(deployment.getId() + "当前流程状态:"+suspended);
// 删除部署 repositoryService.deleteDeployment(deployment.getId());// 挂起/暂停流程定义
repositoryService.suspendProcessDefinitionById(processDefinition.getId());
processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
// 流程定义是否被挂起/暂停
suspended = processDefinition.isSuspended();
System.out.println(deployment.getId() + "当前流程状态:"+suspended);
// 激活流程定义
repositoryService.activateProcessDefinitionById(processDefinition.getId());
processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
// 流程定义是否被挂起/暂停
suspended = processDefinition.isSuspended();
System.out.println(deployment.getId() + "当前流程状态:"+suspended);
启动流程实例
// 启动实例
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();RuntimeService runtimeService= processEngine.getRuntimeService();
Map<String, Object> userVariables = new HashMap<>();
//小兵申请,经理审批 user
userVariables.put("starter", "xiaobing");
userVariables.put("manager", "jingli");ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("apply", userVariables);
// runtimeService.startProcessInstanceByKey("apply");System.out.println(processInstance.getProcessInstanceId());
System.out.println(processInstance.getProcessDefinitionId());
用户任务
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
// 查询待办任务
List<Task> taskList = taskService.createTaskQuery().taskAssignee("starter").list();for (Task task : taskList) {System.out.println("taskID:"+task.getId());// 完成任务taskService.complete(task.getId());
taskID:73cd61cd-f8b6-11ee-b5e3-8c8caa7a4f9c
接着是到了经理的任务
我们继续执行
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
// 查询待办任务
List<Task> taskList = taskService.createTaskQuery().taskAssignee("manager").list();for (Task task : taskList) {System.out.println("taskID:"+task.getId());// 完成任务taskService.complete(task.getId());
}
taskID:82209be4-f8b8-11ee-9d92-8c8caa7a4f9c
历史记录
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();HistoryService historyService = processEngine.getHistoryService();// 历史查询List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery().processInstanceId("73cb8d05-f8b6-11ee-b5e3-8c8caa7a4f9c").orderByHistoricActivityInstanceStartTime().asc().list();
for (HistoricActivityInstance historicActivityInstance : list) {System.out.println(historicActivityInstance.getActivityName() + ":" + historicActivityInstance.getAssignee());}
由于是为了清楚使用接口,并没有构建复杂场景,我们会一个个的完成基础学习,分开解构各个小细节。
总结
- 基本的接口会用了,知道如何调用了
- 表结构并没有很细的解构
- 各个方法细节没有解构
- 业务逻辑,还有一些特殊的指令用法,网关,参数,等具体用法,当我们把各个bpmn概念理一遍以后,这些内容随着场景案例就会一一呈现在我们面前