使用Camunda流程引擎开发,信号启动事件、条件启动事件、消息启...
momory
共 4014字,需浏览 9分钟
·
2022-03-09 17:17
在使用开源Camunda流程引擎做二次开发时,启动事件是非常有用的,能够帮助我们解决很多的问题。既然能功能那么强大,有哪些启动事件,又能帮我们解决什么问题,以及如何使用呢?
有哪些启动事件?
Camunda 提供了信号启动事件、条件启动事件、消息启动事解决什么问题?
在实际业务中,如果遇上节点多而且逻辑复杂的流程时,需要启动子流程,这个时候只需要发一个信息或者一个消息、又或者到指定一个条件即可轻松发起子流程。如何使用呢?
Camunda 已经帮我们做好了API,只需要理解知道怎么使用即可。流程示例
上面的流程在【自动发起子流程】这个自动服务任务节点,可以帮我们发起下面以3种为开始事件的子流程。
主流程的XML文件:
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="_TZZ5cHYXEeSiaf32_fRHjA" targetNamespace="http://camunda.org/examples" exporter="Camunda Modeler" exporterVersion="4.11.1" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
<bpmn2:process id="Instantiating_Process" name="测试事件方式发起流程" isExecutable="true">
<bpmn2:startEvent id="StartEvent_1" name="开始">
<bpmn2:outgoing>SequenceFlow_1bpmn2:outgoing>
bpmn2:startEvent>
<bpmn2:serviceTask id="ServiceTask_1" name="自动发起子流程" camunda:class="com.bpm.config.SignalEventTypeStartProcDelegate">
<bpmn2:incoming>SequenceFlow_1bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_2bpmn2:outgoing>
bpmn2:serviceTask>
<bpmn2:sequenceFlow id="SequenceFlow_1" name="" sourceRef="StartEvent_1" targetRef="ServiceTask_1" />
<bpmn2:userTask id="UserTask_1" name="业务办理" camunda:assignee="demo">
<bpmn2:incoming>SequenceFlow_2bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_3bpmn2:outgoing>
bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_2" name="" sourceRef="ServiceTask_1" targetRef="UserTask_1" />
<bpmn2:endEvent id="EndEvent_1" name="结束">
<bpmn2:incoming>SequenceFlow_3bpmn2:incoming>
bpmn2:endEvent>
<bpmn2:sequenceFlow id="SequenceFlow_3" name="" sourceRef="UserTask_1" targetRef="EndEvent_1" />
bpmn2:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Instantiating_Process">
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_3" bpmnElement="SequenceFlow_3" sourceElement="_BPMNShape_UserTask_2" targetElement="_BPMNShape_EndEvent_49">
<di:waypoint x="554" y="118" />
<di:waypoint x="662" y="118" />
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_2" bpmnElement="SequenceFlow_2" sourceElement="_BPMNShape_ServiceTask_2" targetElement="_BPMNShape_UserTask_2">
<di:waypoint x="360" y="118" />
<di:waypoint x="454" y="118" />
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_1" bpmnElement="SequenceFlow_1" sourceElement="_BPMNShape_StartEvent_13" targetElement="_BPMNShape_ServiceTask_2">
<di:waypoint x="188" y="118" />
<di:waypoint x="260" y="118" />
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_13" bpmnElement="StartEvent_1">
<dc:Bounds x="152" y="100" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="159" y="143" width="22" height="14" />
bpmndi:BPMNLabel>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="_BPMNShape_ServiceTask_2" bpmnElement="ServiceTask_1">
<dc:Bounds x="260" y="78" width="100" height="80" />
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="_BPMNShape_UserTask_2" bpmnElement="UserTask_1">
<dc:Bounds x="454" y="78" width="100" height="80" />
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="_BPMNShape_EndEvent_49" bpmnElement="EndEvent_1">
<dc:Bounds x="662" y="100" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="670" y="143" width="22" height="14" />
bpmndi:BPMNLabel>
bpmndi:BPMNShape>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
bpmn2:definitions>
其它3条子流程的XML文件分别是:
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_0js57kf" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.11.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
<bpmn:process id="Process_0qplt59" name="信号事件方式启动流程" isExecutable="true">
<bpmn:userTask id="Activity_1jhxkz3" name="任务一" camunda:assignee="${assignee}">
<bpmn:incoming>Flow_0ays4bzbpmn:incoming>
<bpmn:outgoing>Flow_0vld40obpmn:outgoing>
bpmn:userTask>
<bpmn:userTask id="Activity_1d0nrlf" name="任务二" camunda:assignee="${assignee}">
<bpmn:incoming>Flow_0vld40obpmn:incoming>
<bpmn:outgoing>Flow_0z13zvfbpmn:outgoing>
bpmn:userTask>
<bpmn:endEvent id="Event_1kb0wd1">
<bpmn:incoming>Flow_0z13zvfbpmn:incoming>
bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0vld40o" sourceRef="Activity_1jhxkz3" targetRef="Activity_1d0nrlf" />
<bpmn:sequenceFlow id="Flow_0z13zvf" sourceRef="Activity_1d0nrlf" targetRef="Event_1kb0wd1" />
<bpmn:sequenceFlow id="Flow_0ays4bz" sourceRef="Event_0fltzrj" targetRef="Activity_1jhxkz3" />
<bpmn:startEvent id="Event_0fltzrj" name="信号启动">
<bpmn:outgoing>Flow_0ays4bzbpmn:outgoing>
<bpmn:signalEventDefinition id="SignalEventDefinition_000pg4x" signalRef="Signal_1i2qeo0" />
bpmn:startEvent>
bpmn:process>
<bpmn:signal id="Signal_1i2qeo0" name="signal" />
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0qplt59">
<bpmndi:BPMNEdge id="Flow_0ays4bz_di" bpmnElement="Flow_0ays4bz">
<di:waypoint x="188" y="130" />
<di:waypoint x="280" y="130" />
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0z13zvf_di" bpmnElement="Flow_0z13zvf">
<di:waypoint x="550" y="130" />
<di:waypoint x="632" y="130" />
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0vld40o_di" bpmnElement="Flow_0vld40o">
<di:waypoint x="380" y="130" />
<di:waypoint x="450" y="130" />
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="Event_13zdp51_di" bpmnElement="Event_0fltzrj">
<dc:Bounds x="152" y="112" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="153" y="82" width="44" height="14" />
bpmndi:BPMNLabel>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1jhxkz3_di" bpmnElement="Activity_1jhxkz3">
<dc:Bounds x="280" y="90" width="100" height="80" />
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_1kb0wd1_di" bpmnElement="Event_1kb0wd1">
<dc:Bounds x="632" y="112" width="36" height="36" />
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1d0nrlf_di" bpmnElement="Activity_1d0nrlf">
<dc:Bounds x="450" y="90" width="100" height="80" />
bpmndi:BPMNShape>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
bpmn:definitions>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1gofk0s" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.11.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
<bpmn:process id="Process_1gnunqx" name="条件事件方式启动流程" isExecutable="true">
<bpmn:startEvent id="StartEvent_1" name="条件启动">
<bpmn:outgoing>Flow_06zvpffbpmn:outgoing>
<bpmn:conditionalEventDefinition id="ConditionalEventDefinition_11cu5dx">
<bpmn:condition xsi:type="bpmn:tFormalExpression">${isStart==true}bpmn:condition>
bpmn:conditionalEventDefinition>
bpmn:startEvent>
<bpmn:endEvent id="Event_1bw20g9">
<bpmn:incoming>Flow_1vmm5tjbpmn:incoming>
bpmn:endEvent>
<bpmn:userTask id="Activity_0sc060o" name="条件方式,任务一">
<bpmn:incoming>Flow_06zvpffbpmn:incoming>
<bpmn:outgoing>Flow_1vmm5tjbpmn:outgoing>
bpmn:userTask>
<bpmn:sequenceFlow id="Flow_1vmm5tj" sourceRef="Activity_0sc060o" targetRef="Event_1bw20g9" />
<bpmn:sequenceFlow id="Flow_06zvpff" sourceRef="StartEvent_1" targetRef="Activity_0sc060o" />
bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1gnunqx">
<bpmndi:BPMNEdge id="Flow_1vmm5tj_di" bpmnElement="Flow_1vmm5tj">
<di:waypoint x="390" y="117" />
<di:waypoint x="492" y="117" />
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_06zvpff_di" bpmnElement="Flow_06zvpff">
<di:waypoint x="188" y="117" />
<di:waypoint x="290" y="117" />
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="Event_1slknve_di" bpmnElement="StartEvent_1">
<dc:Bounds x="152" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="148" y="142" width="44" height="14" />
bpmndi:BPMNLabel>
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_1bw20g9_di" bpmnElement="Event_1bw20g9">
<dc:Bounds x="492" y="99" width="36" height="36" />
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_16yxguf_di" bpmnElement="Activity_0sc060o">
<dc:Bounds x="290" y="77" width="100" height="80" />
bpmndi:BPMNShape>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
bpmn:definitions>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1uxobnf" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.11.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
<bpmn:process id="Process_0av3mad" name="消息事件方式发起流程" isExecutable="true">
<bpmn:userTask id="Activity_0amzx6i" name="经理审批" camunda:assignee="demo">
<bpmn:incoming>Flow_055trdzbpmn:incoming>
<bpmn:outgoing>Flow_16yqwjtbpmn:outgoing>
bpmn:userTask>
<bpmn:sequenceFlow id="Flow_055trdz" sourceRef="StartEvent_1" targetRef="Activity_0amzx6i" />
<bpmn:userTask id="Activity_0ipk02z" name="财务审批" camunda:assignee="demo">
<bpmn:incoming>Flow_16yqwjtbpmn:incoming>
<bpmn:outgoing>Flow_0lwwz4nbpmn:outgoing>
bpmn:userTask>
<bpmn:sequenceFlow id="Flow_16yqwjt" sourceRef="Activity_0amzx6i" targetRef="Activity_0ipk02z" />
<bpmn:endEvent id="Event_03yf0hk">
<bpmn:incoming>Flow_0lwwz4nbpmn:incoming>
bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0lwwz4n" sourceRef="Activity_0ipk02z" targetRef="Event_03yf0hk" />
<bpmn:startEvent id="StartEvent_1" name="消息">
<bpmn:outgoing>Flow_055trdzbpmn:outgoing>
<bpmn:messageEventDefinition id="MessageEventDefinition_0o0ivuo" messageRef="Message_1i854ob" />
bpmn:startEvent>
bpmn:process>
<bpmn:message id="Message_1i854ob" name="telMessage" />
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0av3mad">
<bpmndi:BPMNEdge id="Flow_0lwwz4n_di" bpmnElement="Flow_0lwwz4n">
<di:waypoint x="630" y="117" />
<di:waypoint x="702" y="117" />
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_16yqwjt_di" bpmnElement="Flow_16yqwjt">
<di:waypoint x="400" y="117" />
<di:waypoint x="530" y="117" />
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_055trdz_di" bpmnElement="Flow_055trdz">
<di:waypoint x="215" y="117" />
<di:waypoint x="300" y="117" />
bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="Activity_0amzx6i_di" bpmnElement="Activity_0amzx6i">
<dc:Bounds x="300" y="77" width="100" height="80" />
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0ipk02z_di" bpmnElement="Activity_0ipk02z">
<dc:Bounds x="530" y="77" width="100" height="80" />
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_03yf0hk_di" bpmnElement="Event_03yf0hk">
<dc:Bounds x="702" y="99" width="36" height="36" />
bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0kfrdnl_di" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="186" y="142" width="22" height="14" />
bpmndi:BPMNLabel>
bpmndi:BPMNShape>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
bpmn:definitions>
画流程还是很简单的,主要还是不同的子流程需配制不同的参数。
部署流程验证测试 现在要做的是启动【测试事件方式发起流程】这条流程,流程发起后会帮助发起其它子流程。依次来,先发帮助发起信号事件子流程:
点Start后,在【信号事件方式启动流程】的流程中产生了第一个节点任务【任务一】:
都能够通过事件方式发起流程,并产生流程中第一个用户任务。在主流程程中,通过自动任务的试去发起流程,相关代码如下:
OK,关于事件方式启动流程就写到定里。package com.bpm.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import java.util.HashMap;
import java.util.Map;
/**
* 消息启动子流程
* 信号启动子流程
* 条件启动子流程
*/
@Slf4j
public class SignalEventTypeStartProcDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
try {
Object type = execution.getVariable("type");
if(ObjectUtils.isEmpty(type)){
log.debug("启动事件方式启动流程,没有设置事件的类型,参数【type】为空");
}
Object obj = execution.getVariable("parameter");
if(ObjectUtils.isEmpty(obj)){
log.error("启动流程,参数【parameter】为必须参数,不能为空");
return;
}
RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
switch (type.toString()) {
case "message": // 消息方式
runtimeService.startProcessInstanceByMessage(obj.toString(), execution.getVariables());
break;
case "sign": // 信号方式
runtimeService.createSignalEvent(obj.toString()).setVariables(execution.getVariables()).send();
break;
case "conditional": // 条件方式
Map<String, Object> map = new HashMap<>();
map.put(obj.toString(), Boolean.TRUE);
map.putAll(execution.getVariables());
// List
instanceList =
runtimeService.createConditionEvaluation().setVariables(map).evaluateStartConditions();
break;
default: // 默认无事件方式启动流程
runtimeService.startProcessInstanceByKey(obj.toString());
break;
}
//// to do
// 如果需要回写启动的流程实例ID,可以用事件的方式去启动
}catch (Exception e){
throw new Exception("启动流程异常:{}", e);
}
}
}
评论