子流程
目录
bpmn设置
定义流程,id为LeaveProcess2
定义流程,id为LeaveProcess1
,在其中使用子流程节点CallActivityTask
调用其它流程,它的属性Called Element
值为其它流程Id
代码
-
部署两个流程,并启动
LeaveProcess1
@Test public void delopyAllBpmn() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); processEngine.getRepositoryService() .createDeployment() .addClasspathResource("bpmn/LeaveProcess2.bpmn") .name("请假流程") .deploy(); processEngine.getRepositoryService() .createDeployment() .addClasspathResource("bpmn/LeaveProcess1.bpmn") .name("请假流程2") .deploy(); } @Test public void startProcessInstance() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); processEngine.getRuntimeService() .startProcessInstanceByKey("LeaveProcess1"); }
-
此时
LeaveProcess1
处于第一个节点,让这个节点的责任人调用complete完成任务,推进节点@Test public void testApplyTask() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService .createTaskQuery() .singleResult(); taskService.setAssignee(task.getId(), "zhangsan"); taskService.complete(task.getId()); }
-
此时就会进入子节点,由此自动基于
LeaveProcess2
生成一个流程实例,该流程实例中,经理进行审批,审批完成后调用complete完成任务,推进节点@Test public void testPMTask() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService .createTaskQuery() .singleResult(); taskService.setAssignee(task.getId(), "pm"); taskService.complete(task.getId()); }
-
此时就会回到
LeaveProcess1.bpmn
中的人事审批,人事审批完成后,流程结束public void testHRTask() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService .createTaskQuery() .singleResult(); taskService.setAssignee(task.getId(), "hr"); taskService.complete(task.getId()); }
流程之间的传参
-
LeaveProcess1
流程中,负责人zhangsan
发起申请,并完成任务,同时设置pmAuditor
变量最终赋值给pm变量,最后调用子流程,进入到经理审批@Test public void testApplyTask() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService .createTaskQuery() .singleResult(); taskService.setAssignee(task.getId(), "zhangsan"); Map<String, Object> variables = new HashMap<>(); variables.put("pmAuditor", "wangjingli"); taskService.complete(task.getId(), variables); }
-
经理直接审批结束,设置变量
hrAuditor
赋值给hr,结束LeaveProcess2
,回到LeaveProcess1
,进入人事审批@Test public void testPMTask() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService .createTaskQuery() .singleResult(); Map<String, Object> variables = new HashMap<>(); variables.put("hrAuditor", "zhaoHR"); taskService.complete(task.getId(), variables); }
-
可以看到流程之间调用时,使用
taskService.complete(流程id, Map类型变量)
完成传参,其中Map类型变量中的key为bpmn中子流程节点CallActivityTask
设置好的属性名