Camunda信号事件示例Demo(Signal Events)

Camunda信号事件(Signal Events)是引用一个命名信号的事件。信号是全局作用域的事件(广播语义),并被传递给所有活动处理程序。。

Camunda消息事件包括:Signal Start Event(信号启动事件)、Signal Intermediate Catching Event(信号中间捕获事件)、Signal Boundary Event(信号边界事件)、Signal Intermediate Throwing Event(信号中断抛出事件)、Signal End Event(信号结束事件)。

本文重点介绍Signal Intermediate Throwing Event(信号中间抛出事件)和Signal Boundary Event(信号边界事件),其它事件请参考camunda官方文档:https://docs.camunda.org/manual/7.15/reference/bpmn20/events/

 

一、设计流程图

以下通过模拟贷款利率变动,通过信号触发,通知正在审批贷款的流程,来说明如何通过信号中断抛出事件和信号边界事件的使用。

修改利率主流程:

 

编辑

 

发送信号事件节点配置:

 

编辑

 

主流程BPMN流程模型文件:

<?xml version="1.0" encoding="UTF-8"?>

<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: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_1r50pfg" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.8.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">

  <bpmn:process id="Process_1k9ge07" name="利率修改主流程" isExecutable="true">

    <bpmn:sequenceFlow id="Flow_0tu0a6n" sourceRef="StartEvent_1" targetRef="Activity_1imwq0f" />

    <bpmn:sequenceFlow id="Flow_14w0uh2" sourceRef="Activity_1imwq0f" targetRef="Event_0pmsgcv" />

    <bpmn:intermediateThrowEvent id="Event_0pmsgcv" name="利率修改信号">

      <bpmn:incoming>Flow_14w0uh2</bpmn:incoming>

      <bpmn:outgoing>Flow_1akp21w</bpmn:outgoing>

      <bpmn:signalEventDefinition id="SignalEventDefinition_13zapm1" signalRef="Signal_0sarl8n" />

    </bpmn:intermediateThrowEvent>

    <bpmn:sequenceFlow id="Flow_1akp21w" sourceRef="Event_0pmsgcv" targetRef="Event_0zxowuc" />

    <bpmn:endEvent id="Event_0zxowuc">

      <bpmn:incoming>Flow_1akp21w</bpmn:incoming>

    </bpmn:endEvent>

    <bpmn:userTask id="Activity_1imwq0f" name="确认修改利率" camunda:assignee="demo">

      <bpmn:incoming>Flow_0tu0a6n</bpmn:incoming>

      <bpmn:outgoing>Flow_14w0uh2</bpmn:outgoing>

    </bpmn:userTask>

    <bpmn:startEvent id="StartEvent_1" name="启动">

      <bpmn:outgoing>Flow_0tu0a6n</bpmn:outgoing>

    </bpmn:startEvent>

  </bpmn:process>

  <bpmn:signal id="Signal_0sarl8n" name="rateChange" />

  <bpmn:signal id="Signal_1yyrmdd" name="initRateSignal" />

  <bpmndi:BPMNDiagram id="BPMNDiagram_1">

    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1k9ge07">

      <bpmndi:BPMNEdge id="Flow_0tu0a6n_di" bpmnElement="Flow_0tu0a6n">

        <di:waypoint x="215" y="117" />

        <di:waypoint x="310" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_14w0uh2_di" bpmnElement="Flow_14w0uh2">

        <di:waypoint x="410" y="117" />

        <di:waypoint x="512" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_1akp21w_di" bpmnElement="Flow_1akp21w">

        <di:waypoint x="548" y="117" />

        <di:waypoint x="642" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNShape id="Event_07p8iga_di" bpmnElement="Event_0pmsgcv">

        <dc:Bounds x="512" y="99" width="36" height="36" />

        <bpmndi:BPMNLabel>

          <dc:Bounds x="497" y="142" width="66" height="14" />

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Event_0zxowuc_di" bpmnElement="Event_0zxowuc">

        <dc:Bounds x="642" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_1bby0f3_di" bpmnElement="Activity_1imwq0f">

        <dc:Bounds x="310" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Event_13sy2jx_di" bpmnElement="StartEvent_1">

        <dc:Bounds x="179" y="99" width="36" height="36" />

        <bpmndi:BPMNLabel>

          <dc:Bounds x="189" y="142" width="21" height="14" />

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNShape>

    </bpmndi:BPMNPlane>

  </bpmndi:BPMNDiagram>

</bpmn:definitions>

贷款申请子流程:

 

编辑

 

信号事件接收节点配置:

 

编辑

 

子流程BPMN流程模型文件:

<?xml version="1.0" encoding="UTF-8"?>

<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: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_08dxmlv" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.8.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">

  <bpmn:process id="Process_0vaumu2" name="贷款申请子流程" isExecutable="true">

    <bpmn:startEvent id="StartEvent_1">

      <bpmn:outgoing>Flow_1cd44ow</bpmn:outgoing>

    </bpmn:startEvent>

    <bpmn:sequenceFlow id="Flow_1cd44ow" sourceRef="StartEvent_1" targetRef="Activity_0gbnjod" />

    <bpmn:sequenceFlow id="Flow_0fcescd" sourceRef="Activity_0gbnjod" targetRef="Activity_1vw95p2" />

    <bpmn:sequenceFlow id="Flow_1s86ukb" sourceRef="Activity_1vw95p2" targetRef="Activity_0gir1sy" />

    <bpmn:userTask id="Activity_0gbnjod" name="贷款申请" camunda:assignee="demo">

      <bpmn:incoming>Flow_1cd44ow</bpmn:incoming>

      <bpmn:incoming>Flow_1agstqr</bpmn:incoming>

      <bpmn:outgoing>Flow_0fcescd</bpmn:outgoing>

    </bpmn:userTask>

    <bpmn:userTask id="Activity_1vw95p2" name="审批确认" camunda:assignee="demo">

      <bpmn:incoming>Flow_0fcescd</bpmn:incoming>

      <bpmn:outgoing>Flow_1s86ukb</bpmn:outgoing>

    </bpmn:userTask>

    <bpmn:userTask id="Activity_0gir1sy" name="利率计算" camunda:assignee="demo">

      <bpmn:incoming>Flow_1s86ukb</bpmn:incoming>

      <bpmn:outgoing>Flow_0lcq0a8</bpmn:outgoing>

    </bpmn:userTask>

    <bpmn:boundaryEvent id="Event_03yrq0u" attachedToRef="Activity_0gir1sy">

      <bpmn:outgoing>Flow_1agstqr</bpmn:outgoing>

      <bpmn:signalEventDefinition id="SignalEventDefinition_1xvgpof" signalRef="Signal_1d2gk0o" />

    </bpmn:boundaryEvent>

    <bpmn:sequenceFlow id="Flow_1agstqr" sourceRef="Event_03yrq0u" targetRef="Activity_0gbnjod" />

    <bpmn:endEvent id="Event_0ghrdqw">

      <bpmn:incoming>Flow_0lcq0a8</bpmn:incoming>

    </bpmn:endEvent>

    <bpmn:sequenceFlow id="Flow_0lcq0a8" sourceRef="Activity_0gir1sy" targetRef="Event_0ghrdqw" />

  </bpmn:process>

  <bpmn:signal id="Signal_1d2gk0o" name="rateChange" />

  <bpmndi:BPMNDiagram id="BPMNDiagram_1">

    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0vaumu2">

      <bpmndi:BPMNEdge id="Flow_1cd44ow_di" bpmnElement="Flow_1cd44ow">

        <di:waypoint x="215" y="117" />

        <di:waypoint x="270" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_0fcescd_di" bpmnElement="Flow_0fcescd">

        <di:waypoint x="370" y="117" />

        <di:waypoint x="430" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_1s86ukb_di" bpmnElement="Flow_1s86ukb">

        <di:waypoint x="530" y="117" />

        <di:waypoint x="590" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_1agstqr_di" bpmnElement="Flow_1agstqr">

        <di:waypoint x="650" y="175" />

        <di:waypoint x="650" y="195" />

        <di:waypoint x="400" y="195" />

        <di:waypoint x="400" y="147" />

        <di:waypoint x="370" y="147" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_0lcq0a8_di" bpmnElement="Flow_0lcq0a8">

        <di:waypoint x="690" y="117" />

        <di:waypoint x="752" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">

        <dc:Bounds x="179" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_12yahc7_di" bpmnElement="Activity_0gbnjod">

        <dc:Bounds x="270" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_0p46s0e_di" bpmnElement="Activity_1vw95p2">

        <dc:Bounds x="430" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_1eaiy3q_di" bpmnElement="Activity_0gir1sy">

        <dc:Bounds x="590" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Event_0ghrdqw_di" bpmnElement="Event_0ghrdqw">

        <dc:Bounds x="752" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Event_0gqzs8v_di" bpmnElement="Event_03yrq0u">

        <dc:Bounds x="632" y="139" width="36" height="36" />

      </bpmndi:BPMNShape>

    </bpmndi:BPMNPlane>

  </bpmndi:BPMNDiagram>

</bpmn:definitions>

 

二、发起流程测试

通过demo用户登录camunda平台http://localhost:8080/camunda/app/tasklist/default/#/login,分别发起主子流程。

编辑

 

 

启动主流程后,流程状态如下,注意还没有到信号事件触发节点:

编辑

 

 

 

启动子流程后,流程状态如下,注意此时子流程在信号事件接收节点等待:

编辑

 

 

 

此时,数据库表中数据的状态如下:

编辑

 

此时,提交主流程,让主流程走到信号触发节点,由于子流程由于一直在监听该信号,子流程自动触发流程状态变化,如下:

编辑

 

三、信号事件接口说明

  1. 触发(抛出)一个信号(Triggering (Throwing) Signals):

信号可以通过BPMN构造,或通过Java API编程构造抛出,RuntimeService提供了流式API去抛出一个信号:

// 广播一个信号

runtimeService

  .createSignalEvent("signalName")

  .setVariables(variables)

  .send();

// 传递一个信号到单个execution

runtimeService

  .createSignalEvent("signalName")

  .executionId(executionId)

  .setVariables(variables)

  .send(); 

2、使用RuntimeService接口:

RuntimeService.signalEventReceived(String signalName);

RuntimeService.signalEventReceived(String signalName, String executionId);

 如果提供了executionId,则信号只会被传递给指定的execution。否则信号将会被全局抛出给所有订阅了的handler(广播)。注意:信号事件不会关联到特定的流程实例,相反,它会广播到所有流程实例。如果你需要传递给特定的流程实例,不要使用抛出信号事件(throwing signal event),查询出这个特定的执行(execution),然后将信号传递给这个特定的执行(execution)。

 

3、查询信号事件订阅(Querying for Signal Event Subscriptions):

可以查询所有订阅了某个特定信号事件的订阅

List<Execution> executions = runtimeService.createExecutionQuery()

    .signalEventSubscriptionName("alert")

    .list();

 

参考:

http://www.yunchengxc.com

https://docs.camunda.org/manual/7.15/reference/bpmn20/events/signal-events/

 

posted @ 2022-06-13 14:35  大龄码农有梦想  阅读(394)  评论(0编辑  收藏  举报