持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第2天,点击查看活动详情
1.什么是 RepositoryService
RepositoryService提供了管理与控制部署(deployments)与流程定义(process definitions)的操作。管理静态信息等。
1.0 首先我们先准备一个流程文件
<?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:flowable="http://flowable.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.flowable.org/processdef">
<process id="day01" name="测试流程图" isExecutable="true">
<startEvent id="startEvent1" flowable:formFieldValidation="true"></startEvent>
<userTask id="sid-24C112A4-C833-40C2-B642-FB7CD4E404C0" name="测试流程" flowable:assignee="${assignee1}" flowable:formFieldValidation="true">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>
<sequenceFlow id="sid-8DC6D33C-E325-4172-8C52-06420F3D5985" sourceRef="startEvent1" targetRef="sid-24C112A4-C833-40C2-B642-FB7CD4E404C0"></sequenceFlow>
<endEvent id="sid-04DE9E62-130A-492B-8CB2-DD2E852248E1"></endEvent>
<userTask id="sid-DBC538C3-81A7-41BA-A805-F5F145033062" name="审核" flowable:assignee="${assignee1}" flowable:formFieldValidation="true">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>
<sequenceFlow id="sid-2D0D3626-2006-4FA2-8AC5-EA23E469E19C" sourceRef="sid-24C112A4-C833-40C2-B642-FB7CD4E404C0" targetRef="sid-DBC538C3-81A7-41BA-A805-F5F145033062"></sequenceFlow>
<sequenceFlow id="sid-C4A78A1F-254E-49DF-AF78-B897FDF427D3" sourceRef="sid-DBC538C3-81A7-41BA-A805-F5F145033062" targetRef="sid-04DE9E62-130A-492B-8CB2-DD2E852248E1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_day01">
<bpmndi:BPMNPlane bpmnElement="day01" id="BPMNPlane_day01">
<bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
<omgdc:Bounds height="30.0" width="30.0" x="100.0" y="163.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-24C112A4-C833-40C2-B642-FB7CD4E404C0" id="BPMNShape_sid-24C112A4-C833-40C2-B642-FB7CD4E404C0">
<omgdc:Bounds height="80.0" width="100.0" x="216.5" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-04DE9E62-130A-492B-8CB2-DD2E852248E1" id="BPMNShape_sid-04DE9E62-130A-492B-8CB2-DD2E852248E1">
<omgdc:Bounds height="28.0" width="28.0" x="540.0" y="166.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-DBC538C3-81A7-41BA-A805-F5F145033062" id="BPMNShape_sid-DBC538C3-81A7-41BA-A805-F5F145033062">
<omgdc:Bounds height="80.0" width="100.0" x="361.5" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-C4A78A1F-254E-49DF-AF78-B897FDF427D3" id="BPMNEdge_sid-C4A78A1F-254E-49DF-AF78-B897FDF427D3">
<omgdi:waypoint x="461.45000000000005" y="180.0"></omgdi:waypoint>
<omgdi:waypoint x="540.0" y="180.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-2D0D3626-2006-4FA2-8AC5-EA23E469E19C" id="BPMNEdge_sid-2D0D3626-2006-4FA2-8AC5-EA23E469E19C">
<omgdi:waypoint x="316.4499999999907" y="180.0"></omgdi:waypoint>
<omgdi:waypoint x="361.5" y="180.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-8DC6D33C-E325-4172-8C52-06420F3D5985" id="BPMNEdge_sid-8DC6D33C-E325-4172-8C52-06420F3D5985">
<omgdi:waypoint x="129.948660434253" y="178.1973431100433"></omgdi:waypoint>
<omgdi:waypoint x="216.49999999999937" y="179.33993399339934"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
这个文件我们可以在 Flowable UI中设计并下载下来,我之前文章有这个 教程有需要的可以去参考。
1.1 使用 RepositoryService 部署一个流程
@Test
public void DeployProcess(){
Deployment holiday = repositoryService.createDeployment()
.addClasspathResource("测试流程图.bpmn20.xml")
.name("测试流程部署")
.deploy();
}
在正常工作中整个 测试流程图bpmn.xml
文件是前端设计好之后以流的形式添加进来我们这边做演示就直接放在Resource目录下。
结果:执行完成后去
ACT_RE_DEPLOYMENT(部署信息表)
和 ACT_RE_PROCDEF(流程信息定义表)
和 ACT_GE_BYTEARRAY(流程的静态资源信息)
两张表中查看数据。
ACT_RE_PROCDEF
流程定义表:
ACT_RE_DEPLOYMENT
部署信息表:
ACT_RE_PROCDEF
静态资源表:
1.2 使用 RepositoryService 查询我们刚才部署信息
我们可以看到有很多的方法我们这里就演示常用的三种
@Test
public void DeploySelect(){
//批量查询
List<Deployment> list = repositoryService.createDeploymentQuery().list();
//批量分页查询
List<Deployment> listPage = repositoryService.createDeploymentQuery().listPage(0,10);
//精确查询只能返回一个 如果有多条记录会报错singleResult()
Deployment deployment = repositoryService.createDeploymentQuery().deploymentNameLike("测试流程部署").singleResult();
list.forEach(e->{
System.out.println(e.getName()+"list查询");
});
listPage.forEach(e->{
System.out.println(e.getName()+"listPage查询");
});
System.out.println(deployment.getName()+"精确查询");
}
当然 createDeploymentQuery()可调用的方法还有很多 就不 一 一举例了。
DeploymentQuery deploymentId(String var1);
DeploymentQuery deploymentIds(List<String> var1);
DeploymentQuery deploymentName(String var1);
DeploymentQuery deploymentNameLike(String var1);
DeploymentQuery deploymentCategory(String var1);
DeploymentQuery deploymentCategoryLike(String var1);
DeploymentQuery deploymentCategoryNotEquals(String var1);
DeploymentQuery deploymentKey(String var1);
DeploymentQuery deploymentKeyLike(String var1);
DeploymentQuery deploymentTenantId(String var1);
DeploymentQuery deploymentTenantIdLike(String var1);
DeploymentQuery deploymentWithoutTenantId();
DeploymentQuery deploymentEngineVersion(String var1);
DeploymentQuery deploymentDerivedFrom(String var1);
DeploymentQuery parentDeploymentId(String var1);
DeploymentQuery parentDeploymentIdLike(String var1);
DeploymentQuery parentDeploymentIds(List<String> var1);
DeploymentQuery processDefinitionKey(String var1);
DeploymentQuery processDefinitionKeyLike(String var1);
DeploymentQuery latest();
DeploymentQuery orderByDeploymentId();
DeploymentQuery orderByDeploymentName();
DeploymentQuery orderByDeploymentTime();
DeploymentQuery orderByTenantId();
结果:
1.3 流程查询
@Test
public void processSelect(){
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
List<ProcessDefinition> list1 = repositoryService.createNativeProcessDefinitionQuery().sql("select *from ACT_RE_PROCDEF").list();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("day01").singleResult();
list.forEach(e->{
System.out.println(e.getName()+"list查询");
});
list1.forEach(e->{
System.out.println(e.getName()+"自己写 自定义sql 查询");
});
System.out.println(processDefinition.getName()+"精确查询");
}
结果:
1.4 流程的挂起 和 激活流程
@Test
public void suspendActivateProcess(){
//使用流程定义的ID先查询到流程
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey("day01").singleResult();
// isSuspended方法是在激活的时候返回true
boolean suspended = processDefinition.isSuspended();
if(suspended){
//挂起流程
repositoryService.activateProcessDefinitionById(processDefinition.getId());
System.out.println("激活流程...");
}else {
//激活流程
repositoryService.suspendProcessDefinitionById(processDefinition.getId());
System.out.println("挂起流程...");
}
结果:
1.5 删除流程部署
@Test
public void deleteDeploy(){
Deployment deployment = repositoryService.createDeploymentQuery().deploymentNameLike("测试流程部署").singleResult();
// 删除指定流程部署,如果存在该流程部署的实例job或者历史任务,都会抛出异常
repositoryService.deleteDeployment(deployment.getId());
// 级联删除该流程部署所创建的所有任务信息和历史数据
// repositoryService.deleteDeployment(deployment.getId(),true);
}
结果:表中数据都被清除
1.6 指定流程 启动人
@Test
public void processStatrUser(){
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey("day01").singleResult();
//并不会去验证 用户是否存在 。需要我们业务处理
repositoryService.addCandidateStarterUser(processDefinition.getId(),"张三");
}
addCandidateStarterUser
这个方法的实现我们可以看到 传入的参数就是 流程ID 和用户ID 或者account这里为了演示就直接 name了
public void addCandidateStarterUser(String processDefinitionId, String userId) {
this.commandExecutor.execute(new AddIdentityLinkForProcessDefinitionCmd(processDefinitionId, userId, (String)null));
}
结果:ACT_RU_IDENTITYLINK
表中可以看到数据 候选人 张三
1.7 删除流程 启动人
@Test
public void processdeleteUser(){
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey("day01").singleResult();
repositoryService.deleteCandidateStarterUser(processDefinition.getId(),"张三");
}
结果: