工作流activiti 6 学习笔记十三 网关
【摘要】 网关分为以下四类:,并行网关(ParallelGateway),单项网关(ExclusiveGateway),兼容网关(InclusiveGateway) ,事件网关(EventGateway)。第一类 并行网关(ParallelGateway)同时产生两个流程,两个流程同时跑。bpmn文件<process id="myProcess" name="My process" isExecuta...
网关分为以下四类:,并行网关(ParallelGateway),单项网关(ExclusiveGateway),兼容网关(InclusiveGateway) ,事件网关(EventGateway)。
第一类 并行网关(ParallelGateway)
同时产生两个流程,两个流程同时跑。
bpmn文件
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<parallelGateway id="parallelgateway1" name="Parallel Gateway"></parallelGateway>
<userTask id="usertask1" name="task1"></userTask>
<userTask id="usertask2" name="task2"></userTask>
<userTask id="usertask3" name="task3"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="parallelgateway1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="parallelgateway1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="parallelgateway1" targetRef="usertask2"></sequenceFlow>
<parallelGateway id="parallelgateway2" name="Parallel Gateway"></parallelGateway>
<sequenceFlow id="flow4" sourceRef="usertask1" targetRef="parallelgateway2"></sequenceFlow>
<sequenceFlow id="flow5" sourceRef="usertask2" targetRef="parallelgateway2"></sequenceFlow>
<sequenceFlow id="flow6" sourceRef="parallelgateway2" targetRef="usertask3"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
</process>
java 代码
// 部署流程
Deployment deploye = rs.createDeployment().addClasspathResource("gateway_parallel.bpmn").deploy();
ProcessDefinition pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
ProcessInstance pi = runS.startProcessInstanceById(pdf.getId());
System.out.println(pi.getId());
List<Task> tasks = ts.createTaskQuery().processInstanceId(pi.getId()).list();
System.out.println("流程节点个数:"+tasks.size());
for(Task task:tasks) {
System.out.println("流程节点名称:"+task.getName());
}
运行结果
397505
流程节点个数:2
流程节点名称:task1
流程节点名称:task2
代码解释:
启动流程后,节点同时转向task1和task2
第二类:单项网关(ExclusiveGateway)
只选择一个合适的流向,当同时产生两个分支时,选择bpmn文件中的第一个,如果没有选中的分支,则抛出一个异常终止流程。下面以一个请假示例演示,大于三天由总监审批,小于等于三天由经理审批
流程图
bpmn文件
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="员工申请"></userTask>
<exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
<userTask id="usertask2" name="项目总监"></userTask>
<userTask id="usertask3" name="项目经理"></userTask>
<userTask id="usertask4" name="HR"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="exclusivegateway1" targetRef="usertask2">
<conditionExpression xsi:type="tFormalExpression">
<![CDATA[${days>=3}]]>
</conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow4" sourceRef="usertask2" targetRef="usertask4"></sequenceFlow>
<sequenceFlow id="flow5" sourceRef="usertask4" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow6" sourceRef="exclusivegateway1" targetRef="usertask3"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="usertask3" targetRef="usertask4"></sequenceFlow>
</process>
java代码
// 部署流程
Deployment deploye = rs.createDeployment().addClasspathResource("gateway_exclusive2.bpmn").deploy();
ProcessDefinition pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
Map<String,Object> map = new HashMap<String,Object>();
map.put("days",1);
ProcessInstance pi = runS.startProcessInstanceById(pdf.getId(),map);
System.out.println(pi.getId());
Task task = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
System.out.println(task.getName());
ts.complete(task.getId());
task = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
System.out.println(task.getName());
运行结果,当days参数=3 时,运行结果时
445005
员工申请
项目总监
第三类:兼容网关(InclusiveGateway)
兼容网关时单向网关和并行网关的合并,和单向网关的区别是:当满足多个条件时可以分出多个执行流,单向网关如果多个条件都满足只走第一个。此处不再做示例。
第四类:事件网关(EventGateway)
哪个事件先触发,就往哪里走,下面示例:一个消息事件中间事件,一个定时事件,哪个先触发,就走哪个流向
bpmn文件
<signal id="eventgate" name="eventgate"></signal>
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<eventBasedGateway id="eventgateway1" name="Event Gateway"></eventBasedGateway>
<intermediateCatchEvent id="timerintermediatecatchevent1" name="TimerCatchEvent">
<timerEventDefinition><timeDuration>PT5S</timeDuration></timerEventDefinition>
</intermediateCatchEvent>
<intermediateCatchEvent id="signalintermediatecatchevent1" name="SignalCatchEvent">
<signalEventDefinition signalRef="eventgate"></signalEventDefinition>
</intermediateCatchEvent>
<userTask id="usertask1" name="task1"></userTask>
<userTask id="usertask2" name="task2"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="eventgateway1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="eventgateway1" targetRef="signalintermediatecatchevent1"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="signalintermediatecatchevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow4" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow5" sourceRef="eventgateway1" targetRef="timerintermediatecatchevent1"></sequenceFlow>
<sequenceFlow id="flow6" sourceRef="timerintermediatecatchevent1" targetRef="usertask2"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
</process>
java代码
// 部署流程
Deployment deploye = rs.createDeployment().addClasspathResource("gateway_event.bpmn").deploy();
ProcessDefinition pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
ProcessInstance pi = runS.startProcessInstanceById(pdf.getId());
// 发送信号
//runS.signalEventReceived("eventgate");
try {// 触发定时任务
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(pi.getId());
Task task = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
System.out.println(task.getName());
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)