1.什么是包含网关
包含网关可以看做是排他网关和并行网关的结合体;和排他网关一样,你可以在外出顺序流上定义条件,包含网关会解析它们;但是主要的区别是包含网关可以选择多于一条顺序流,这和并行网关一样,包含网关的功能是基于进入和外出顺序流的;
分支:所有外出顺序流的条件都会被解析,结果为true的顺序会以并行方式继续执行,会为每个顺序流创建一个分支;
汇聚:所有并行分支达到包含网关,会进入等待状态,知道每个包含流程token的进入顺序流的分支都到达;这是与并行网关的最大不同;换句话说,包含网关只会等待选中执行了的进入顺序流;在汇聚之后,流程会穿过包含网关继续执行;
2.流程定义
3.测试
3.1 部署流程
package com.wn.Inc;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
/*流程部署*/
public class Deployment {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService对象进行流程部署
RepositoryService repositoryService = processEngine.getRepositoryService();
//进行部署,将对应的流程定义文件生成到数据库当中,作为记录进行保存
org.activiti.engine.repository.Deployment deployment= repositoryService.createDeployment()
.addClasspathResource("bpmnfile/hoildayInc.bpmn") //加载流程文件
.addClasspathResource("bpmnfile/hoildayInc.png")
.name("请假流程-包含网关") //设置流程名称
.key("hoildayInc-Key")
.deploy();//部署
//输出部署信息
System.out.println("流程名称:"+deployment.getName());
System.out.println("流程ID:"+deployment.getId());
System.out.println("流程key:"+deployment.getKey());
}
}
3.2 启动流程
3.2.1 启动普通员工(1)的流程
package com.wn.Inc;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import java.util.HashMap;
import java.util.Map;
/*启动流程实例*/
/*设置流程变量的值*/
public class StartInstance {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
//设置流程变量 启动流程时设置
//定义一个map集合,存放assignee的值
Map<String ,Object> assMap=new HashMap<>();
assMap.put("userType",1);
//启动一个流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("hoildayInc", assMap);
System.out.println("流程实例ID:"+processInstance.getId());
}
}
3.2.2 启动领导(2)的流程
package com.wn.Inc;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import java.util.HashMap;
import java.util.Map;
/*启动流程实例*/
/*设置流程变量的值*/
public class StartInstance {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
//设置流程变量 启动流程时设置
//定义一个map集合,存放assignee的值
Map<String ,Object> assMap=new HashMap<>();
assMap.put("userType",2);
//启动一个流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("hoildayInc", assMap);
System.out.println("流程实例ID:"+processInstance.getId());
}
}
3.3 提交流程
3.3.1 提交流程(zhangsan)
package com.wn.Inc;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
/*提交任务*/
public class CommitTask {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService对象
TaskService taskService = processEngine.getTaskService();
//查询到自己的任务,然后通过任务ID进行提交
Task task = taskService.createTaskQuery().processDefinitionKey("hoildayInc").taskAssignee("zhangsan").singleResult();
//任务处理
taskService.complete(task.getId());
}
}
当普通员工(1)执行的时候,会走常规体验项和抽血化验以及早餐,如果是高级领导(2)会走全部四项;
在走到包含网关时,需要判断条件,符合条件的分支,将会执行,最终进行汇聚;
3.3.2 提交流程(lisi)
package com.wn.Inc;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
/*提交任务*/
public class CommitTask {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService对象
TaskService taskService = processEngine.getTaskService();
//查询到自己的任务,然后通过任务ID进行提交
Task task = taskService.createTaskQuery().processDefinitionKey("hoildayInc").taskAssignee("lisi").singleResult();
//任务处理
taskService.complete(task.getId());
}
}
3.3.3 提交流程(wangwu)
package com.wn.Inc;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
/*提交任务*/
public class CommitTask {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService对象
TaskService taskService = processEngine.getTaskService();
//查询到自己的任务,然后通过任务ID进行提交
Task task = taskService.createTaskQuery().processDefinitionKey("hoildayInc").taskAssignee("wangwu").singleResult();
//任务处理
taskService.complete(task.getId());
}
}
3.3.4 提交流程(sunqi)
package com.wn.Inc;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
/*提交任务*/
public class CommitTask {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService对象
TaskService taskService = processEngine.getTaskService();
//查询到自己的任务,然后通过任务ID进行提交
Task task = taskService.createTaskQuery().processDefinitionKey("hoildayInc").taskAssignee("sunqi").singleResult();
//任务处理
taskService.complete(task.getId());
}
}
当执行完成后,整个流程结束;