Activiti网关--包含网关

1、什么是包含网关

  包含网关可以看做是排他网关和并行网关的结合体;和排他网关一样,你可以在外出顺序流上定义条件,包含网关会解析它们;但是主要的区别是包含网关可以选择多于一条顺序流,这和并行网关一样,包含网关的功能是基于进入和外出顺序流的;

  分支:所有外出顺序流的条件都会被解析,结果为true的顺序会以并行方式继续执行,会为每个顺序流创建一个分支;

  汇聚:所有并行分支达到包含网关,会进入等待状态,知道每个包含流程token的进入顺序流的分支都到达;这是与并行网关的最大不同;换句话说,包含网关只会等待选中执行了的进入顺序流;在汇聚之后,流程会穿过包含网关继续执行;  

2、流程定义

  

  

3、部署流程

复制代码
package com.zn;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;

public class ActivitiInclusiveGateway {
    /**
     * 流程部署
     */
    @Test
    public void deployment() {
        //获取ProcessEngine对象   默认配置文件名称:activiti.cfg.xml  并且configuration的Bean实例ID为processEngineConfiguration
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RepositoryService对象进行流程部署
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //进行部署,将对应的流程定义文件生成到数据库当中,作为记录进行保存
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bmpnfiles/holidayInclusiveGateway.bpmn")     //加载流程文件
                .name("请假流程-包含网关")       //设置流程名称
                .key("holiday")
                .deploy();                      //部署

        //输出部署信息
        System.out.println("流程名称:" + deployment.getName());
        System.out.println("流程ID:" + deployment.getId());
        System.out.println("流程Key:" + deployment.getKey());
    }
}
复制代码

控制台:

  

数据库:

   

4、启动流程实例(userType=1)

当userType=1执行的时候,会走常规体验项和抽血化验以及早餐,如果是userType=1会走全部四项;

复制代码
package com.zn;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ActivitiInclusiveGateway {

    /**
     * 启动流程实例
     */
    @Test
    public void startInstance() {
        //获取ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //设置流程变量
        Map<String, Object> assMap = new HashMap<>();
        assMap.put("userType", 1);
        //启动流程实例
        ProcessInstance holidayGroup = runtimeService.startProcessInstanceByKey("holidayInc", assMap);
        System.out.println("流程实例:" + holidayGroup.getId());
    }
}
复制代码

数据库:

  

5、任务处理

复制代码
package com.zn;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ActivitiInclusiveGateway {

    /*
     *任务处理
     */
    @Test
    public void completeTask(){
        //获取ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取TaskService对象
        TaskService taskService = processEngine.getTaskService();
        //查询到自己的任务,然后通过任务ID进行提交
        Task task = taskService.createTaskQuery().processDefinitionKey("holidayInc").taskAssignee("zhangsan").singleResult();
        //任务处理
        taskService.complete(task.getId());
    }
}
复制代码

数据库:

  

 在走到包含网关时,需要判断条件,符合条件的分支,将会执行,最终进行汇聚;

6、任务处理

复制代码
package com.zn;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ActivitiInclusiveGateway {

    /*
     *任务处理
     */
    @Test
    public void completeTask(){
        //获取ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取TaskService对象
        TaskService taskService = processEngine.getTaskService();
        //查询到自己的任务,然后通过任务ID进行提交
        Task task = taskService.createTaskQuery().processDefinitionKey("holidayInc").taskAssignee("lisi").singleResult();
        //任务处理
        taskService.complete(task.getId());
    }
}
复制代码

数据库:

  

7、任务处理

复制代码
package com.zn;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ActivitiInclusiveGateway {

    /*
     *任务处理
     */
    @Test
    public void completeTask(){
        //获取ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取TaskService对象
        TaskService taskService = processEngine.getTaskService();
        //查询到自己的任务,然后通过任务ID进行提交
        Task task = taskService.createTaskQuery().processDefinitionKey("holidayInc").taskAssignee("wangwu").singleResult();
        //任务处理
        taskService.complete(task.getId());
    }
}
复制代码

数据库:

  

8、任务处理

复制代码
package com.zn;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ActivitiInclusiveGateway {

    /*
     *任务处理
     */
    @Test
    public void completeTask(){
        //获取ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取TaskService对象
        TaskService taskService = processEngine.getTaskService();
        //查询到自己的任务,然后通过任务ID进行提交
        Task task = taskService.createTaskQuery().processDefinitionKey("holidayInc").taskAssignee("sunqi").singleResult();
        //任务处理
        taskService.complete(task.getId());
    }
}
复制代码

数据库:

  

 执行完成后,整个流程就结束:

  

posted @ 2020-04-08 22:20  林染plus  阅读(3300)  评论(0编辑  收藏  举报