activiti-排他网关
Posted wnwn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了activiti-排他网关相关的知识,希望对你有一定的参考价值。
1.什么是排他网关
排他网关也叫异或网关,或叫基于数据的排他网关,用来在流程中实现决策;当流程执行到这个网关,所有分支都会判断条件是否为true,如果为true则执行该分支;
注意:排他网关只会选择一个为true的分支执行(即使有两个分支条件都为true,排他网关也会只选择一条分支去执行);
2.为什么使用排他网关
不使用排他网关也可以实现分支:
上图中,在连线的condition条件上设置分支条件;
缺点:如果条件都不满足,不使用排他网关,流程就结束了(是异常结束);
如果使用排他网关决定分支的走向:
如果从网关出去的线所有条件都不满足则系统抛出异常;
说明:经过排他网关必须要有一条且只有一条分支走;
2.流程定义
3.测试
3.1 部署流程
package com.wn.wangguan;
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/hoildayOld.bpmn") //加载流程文件
.addClasspathResource("bpmnfile/hoildayOld.png")
.name("请假流程-排他网关") //设置流程名称
.key("hoildayOld-Key")
.deploy();//部署
//输出部署信息
System.out.println("流程名称:"+deployment.getName());
System.out.println("流程ID:"+deployment.getId());
System.out.println("流程key:"+deployment.getKey());
}
}
3.2 启动流程实例
package com.wn.wangguan;
import com.wn.entity.Hoilday;
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();
//设置流程变量 启动流程时设置
Hoilday hoilday=new Hoilday();
hoilday.setNum(0);
//定义一个map集合,存放assignee的值
Map<String ,Object> assMap=new HashMap<>();
assMap.put("hoilday",hoilday);
//启动一个流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("hoildayOld", assMap);
System.out.println("流程实例ID:"+processInstance.getId());
}
}
3.3 任务处理zhangsan
package com.wn.wangguan;
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("hoildayOld").taskAssignee("zhangsan").singleResult();
//任务处理
taskService.complete(task.getId());
}
}
3.4 任务处理lisi
package com.wn.wangguan;
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("hoildayOld").taskAssignee("lisi").singleResult();
//任务处理
taskService.complete(task.getId());
}
}
条件如果都不符合,则报出异常org.activiti.engine.ActivitiException: No outgoing sequence flow of the exclusive gateway ‘xxxx‘ could be selected for continuing the process
以上是关于activiti-排他网关的主要内容,如果未能解决你的问题,请参考以下文章