子流程

目录

bpmn设置

定义流程,id为LeaveProcess2

定义流程,id为LeaveProcess1,在其中使用子流程节点CallActivityTask调用其它流程,它的属性Called Element值为其它流程Id

代码

  1. 部署两个流程,并启动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");
    }
    
    
  2. 此时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());
    }
    
    
  3. 此时就会进入子节点,由此自动基于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());
    }
    
    
  4. 此时就会回到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());
    }
    
    

流程之间的传参

  1. 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);
    }
    
    
  2. 经理直接审批结束,设置变量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);
    }
    
    
  3. 可以看到流程之间调用时,使用taskService.complete(流程id, Map类型变量)完成传参,其中Map类型变量中的key为bpmn中子流程节点CallActivityTask设置好的属性名

posted @ 2022-11-08 14:02  夏·舍  阅读(43)  评论(0编辑  收藏  举报