在流程设计当中经常会遇到根据条件来判断流程走向的问题,Activiti中也给出了相应的实现方法:网关
网关(Gateway)用于控制流程走向(在BPMN2.0规范中称为“执行令牌”)。根据功能不同可以划分为以下四种网关:
- 排他网关
- 并行网关
- 包容网关
- 事件网关
其中,排他网关(Exclusive Gateway)用来对流程中的走向进行建模。流程执行到该网关时,Activiti 根据设置的条件进行计算,当条件计算结果为true时,执行当前网关的输出流。

小提示:如果多个线路计算结果都为true,那么只会执行第一个,如果没有为true的值,会抛出异常。

排他网关在流程图当中的形象如下图所示:

下面介绍一下排他网关的使用方法。

1、绘制流程图

ExclusiveProcess.bpmn如下所示:

其中排他网关的三个输出线分别设置Condition值为:

${type == 1}
${type == 2}
${type == 3}

当表达式判断结果为true时会执行后面的系统任务。
三个系统任务分别设置成如图所示:

三个系统任务的Task type 设为Expression,Expression选项中分别填入

${resumeService.showTask1()}
${resumeService.showTask2()}
${resumeService.showTask3()}

流程图的XML代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"><process id="exclusiveProcess" name="ExclusiveProcess.bpmn" isExecutable="true"><startEvent id="startevent1" name="Start"></startEvent><exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway><sequenceFlow id="flow1" sourceRef="startevent1" targetRef="exclusivegateway1"></sequenceFlow><serviceTask id="servicetask1" name="Service Task1" activiti:expression="${resumeService.showTask1()}"></serviceTask><sequenceFlow id="flow2" sourceRef="exclusivegateway1" targetRef="servicetask1"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${type == 1}]]></conditionExpression></sequenceFlow><serviceTask id="servicetask3" name="Service Task3" activiti:expression="${resumeService.showTask3()}"></serviceTask><sequenceFlow id="flow3" sourceRef="exclusivegateway1" targetRef="servicetask3"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${type == 3}]]></conditionExpression></sequenceFlow><serviceTask id="servicetask2" name="Service Task2" activiti:expression="${resumeService.showTask2()}"></serviceTask><sequenceFlow id="flow4" sourceRef="exclusivegateway1" targetRef="servicetask2"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${type == 2}]]></conditionExpression></sequenceFlow><endEvent id="endevent1" name="End"></endEvent><sequenceFlow id="flow5" sourceRef="servicetask2" targetRef="endevent1"></sequenceFlow><sequenceFlow id="flow6" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow><sequenceFlow id="flow7" sourceRef="servicetask3" targetRef="endevent1"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_exclusiveProcess"><bpmndi:BPMNPlane bpmnElement="exclusiveProcess" id="BPMNPlane_exclusiveProcess"><bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"><omgdc:Bounds height="35.0" width="35.0" x="60.0" y="140.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1"><omgdc:Bounds height="40.0" width="40.0" x="140.0" y="138.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1"><omgdc:Bounds height="55.0" width="105.0" x="260.0" y="40.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3"><omgdc:Bounds height="55.0" width="105.0" x="269.0" y="220.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2"><omgdc:Bounds height="55.0" width="105.0" x="260.0" y="131.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"><omgdc:Bounds height="35.0" width="35.0" x="480.0" y="141.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"><omgdi:waypoint x="95.0" y="157.0"></omgdi:waypoint><omgdi:waypoint x="140.0" y="158.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"><omgdi:waypoint x="160.0" y="138.0"></omgdi:waypoint><omgdi:waypoint x="160.0" y="67.0"></omgdi:waypoint><omgdi:waypoint x="260.0" y="67.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"><omgdi:waypoint x="160.0" y="178.0"></omgdi:waypoint><omgdi:waypoint x="160.0" y="247.0"></omgdi:waypoint><omgdi:waypoint x="269.0" y="247.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"><omgdi:waypoint x="180.0" y="158.0"></omgdi:waypoint><omgdi:waypoint x="260.0" y="158.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"><omgdi:waypoint x="365.0" y="158.0"></omgdi:waypoint><omgdi:waypoint x="480.0" y="158.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6"><omgdi:waypoint x="365.0" y="67.0"></omgdi:waypoint><omgdi:waypoint x="497.0" y="67.0"></omgdi:waypoint><omgdi:waypoint x="497.0" y="141.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7"><omgdi:waypoint x="374.0" y="247.0"></omgdi:waypoint><omgdi:waypoint x="497.0" y="247.0"></omgdi:waypoint><omgdi:waypoint x="497.0" y="176.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>

2、部署流程

将流程图文件放到项目当中的resources下的processes文件夹,运行程序 。

3、编写代码

新建spring boot 项目,在项目中实现一个service,并且在service中实现三个方法,代码如下:
ResumeService接口

public interface ResumeService {void showTask1();void showTask2();void showTask3();
}

ResumeServiceImpl实现类

@Service("resumeService")
public class ResumeServiceImpl implements ResumeService {@Overridepublic void showTask1() {System.out.println("#############################执行任务1");}@Overridepublic void showTask2() {System.out.println("#############################执行任务2");}@Overridepublic void showTask3() {System.out.println("#############################执行任务3");}
}

利用spring boot 单元测试来实现流程:
ExclusiveProcessTest测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootActivitiApplication.class)
public class ExclusiveProcessTest {@AutowiredRuntimeService runtimeService;/*** 启动流程,并给流程传入参数type,排他网关根据参数type来判断流程走向*/@Testpublic void testStartProcess() {Map<String, Object> map = new HashMap<>();map.put("type", 3);runtimeService.startProcessInstanceByKey("exclusiveProcess", map);}
}

4、执行结果

运行单元测试,执行结果如下:

小知识点:排他网关中有一个属性为default,当所有条件都不满足时,排他网关默认执行default属性制定的流程。

<exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway" default="flow1"></exclusiveGateway>

Activiti学习之根据条件判断流程走向相关推荐

  1. activiti学习(二十一)——流程虚拟机源码分析(三)——从进入到离开userTask

    前言 承接上文<activiti学习(二十)--流程虚拟机源码分析(二)--从开始节点离开到下个节点前>,假设execution接下来进入的节点是userTask,本文分析一下进入user ...

  2. C++学习笔记3[条件判断语句]

    C++学习目录链接: C++学习笔记目录链接(持续更新中) 文章目录 一.条件判断语句 1.判断语句 二.使用条件运算符进行判断 三.switch语句 四.判断语句嵌套 总结 一.条件判断语句 1.判 ...

  3. php基础教程 第六步 学习数组以及条件判断switch补充

    条件语句 switch 在上一节的学习中,学习了php的条件语句if.在php编程中进行条件判断还可以使用switch语句.switch语句语法如下: <?php switch (值或表达式) ...

  4. JavaScript学习笔记(条件判断)

    JavaScript使用if(){-}else{-}来进行条件判断 ,例如: //单个if else分支 var age = 20; if (age >= 18) {alert('adult') ...

  5. linux循环判断输出结果,Linux Shell基础学习——循环与条件判断

    for循环 1. 注意do必须换行 for i in {1..10} do echo $i done 2. 注意do必须换行 for i in 1 2 3 4 5 do echo $i done 3. ...

  6. 【Makefile由浅入深完全学习记录8】条件判断语句

    加qq:1126137994 微信:liu1126137994 一起学习更多技术~ 今天来学习Makefile中条件判断语句! Makefile也算是一门语言,它有自己的语法,那么它应该也会像C/C+ ...

  7. Activiti 学习笔记十:开始活动节点(判断流程是否结束及查询历史)

    一.流程图 二.演示代码 package jiankunking.start;import java.io.InputStream;import org.activiti.engine.Process ...

  8. [Shell]条件判断与流程控制:if, case, for, while, until

    [条件判断] 1. 按文件类型进行判断 -b 文件    判断该文件是否存在,并且为块设备文件(是块设备文件为真) -c 文件    判断该文件是否存在,并且为字符设备文件(是字符设备文件为真) -d ...

  9. 网络安全零基础学习视频:条件测试语句和if流程控制语句的使用

    全套学习资料移步至公众号[学神来啦]更多学习资料添加扣扣资源群:661308959 本节所讲内容:   20.1  read命令键盘读取变量的值   20.2  流程控制语句if   20.3  te ...

  10. Sass学习笔记 -- 初步了解函数、运算、条件判断及循环

    函数 sass定义了很多函数可供使用,当然你也可以自己定义函数,以@fuction开始.sass的官方函数链接为:sass fuction,实际项目中我们使用最多的应该是颜色函数,而颜色函数中又以li ...

最新文章

  1. Linux系统文件目录
  2. Yii2 配置 Nginx 伪静态
  3. pytorch torch.nn.MSELoss(size_average=True)(均方误差【损失函数】)Mean Squared Error(MSE)、SSE(和方差)
  4. linux安装libfastcommon+fastdfs+nginx
  5. 关于EL表达式取值的问题
  6. LeetCode篇之链表:1290(二进制链表转整数)
  7. Altium Designer之PCB
  8. ​CSDN疯狂盲盒来啦!iPhone 12、机械键盘、Switch等你来拿!
  9. 2014-03-08_javascrit_controllerstatus
  10. linux虚拟化技术 教程,Linux上实现虚拟化技术的优势
  11. Ubuntu20安装向日葵
  12. 基于JSP网上拍卖平台系统
  13. java存根_Java方法存根
  14. 产品活跃度做好很难?提高用户活跃度用对方法一点也不难
  15. SSM+jsp整合步骤(注解开发)
  16. echart柱状图堆叠总计显示
  17. 计算机网络和因特网笔记
  18. C/C++语言入门——鸡兔同笼问题
  19. 可伸缩服务架构:框架与中间件
  20. 代码签名证书的时间戳验证码签名方法

热门文章

  1. 武汉高中计算机网络技术分数线,2021年武汉高中录取分数线排名公布
  2. Android 通知用法
  3. Android保存图片到本地或者数据库,并通知相册更新
  4. better-scroll滚动的同时样式透明度发生变化
  5. CF 581A Vasya the Hipster
  6. 【密码学】七、密钥管理
  7. PostScirpt打印机驱动程序
  8. WordPress安装时未选择语言导致变成英文无法修改成中文
  9. 2022全国高中数学联赛A1卷T2几何答案
  10. html网页两边有空隙,怎么让整个内容缩进?就是四周都有间隙。