Camunda设置子流程(Call Activity)的BusinessKey
背景
子流程
BPMN2.0将嵌入式子流程(SubProcess)和调用活动(Call Activity)进行了区分。首先要明确的是,本文介绍的是Call Activity,也就是下图所示中,加粗黑色边框的圆角矩形。本文介绍如何在主流程调用子流程的时候为其设置一个BusinessKey。


BusinessKey
Camunda引擎可以会为每个流程实例分配ProcessInstanceId、为任务分配TaskId,但是这些怎么与我们业务系统进行关联呢?答案就是使用BusinessKey(业务键)。
BusinessKey是BPMN为我们提供的一个便利的功能,通过它与业务系统关联是很容易的,可能你会想到是不是也可以用流程实例变量为保存业务系统的ID呢,答案是可以的,但是相较于process-instance variables,使用BusinessKey还有另外2个特别原因:
- 性能:按BusinessKey查询通常比按变量值查询更有效。举例来说就是查询时它不需要Join变量表。
- 可见性:BusinessKey在Camunda Cockpit等应用程序中会被突出显示。
步骤
主流程配置
选中CallActivity活动,在属性面板中勾选中“Business Key”之后,会在下一行出现一个“Business Key Expression”输入框,默认值是#{execution.processBusinessKey}
,也就是说默认情况下,主流程在启动时,会将自己的BusinessKey也作为子流程实例的Business Key。
你可以自定义这个表达式,使用一些预置的变量或者常量,比如:SubProcess-For-#{execution.processBusinessKey}
,这样当主流程发起实例时指定Business Key是TonyTest,子流程实例的Business Key就是SubProcess-For-TonyTest。

创建主流程


跳转到子流程

附件
父流程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| <?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" id="Definitions_02dfyd8" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.1.1"> <bpmn:process id="SubProcessEvent" name="子流程事件通知演示" isExecutable="true"> <bpmn:startEvent id="StartEvent_1"> <bpmn:outgoing>Flow_02dlqgj</bpmn:outgoing> </bpmn:startEvent> <bpmn:sequenceFlow id="Flow_02dlqgj" sourceRef="StartEvent_1" targetRef="Activity_1bttv2o" /> <bpmn:callActivity id="Activity_1bttv2o" name="HR子流程" calledElement="SubProcessEvent_HR" camunda:calledElementTenantId="demo"> <bpmn:extensionElements> <camunda:in businessKey="#{execution.processBusinessKey}" /> </bpmn:extensionElements> <bpmn:incoming>Flow_02dlqgj</bpmn:incoming> <bpmn:outgoing>Flow_0u3noaj</bpmn:outgoing> </bpmn:callActivity> <bpmn:sequenceFlow id="Flow_0u3noaj" sourceRef="Activity_1bttv2o" targetRef="Activity_0m4y88u" /> <bpmn:endEvent id="Event_19skg3f"> <bpmn:incoming>Flow_147a93e</bpmn:incoming> </bpmn:endEvent> <bpmn:sequenceFlow id="Flow_147a93e" sourceRef="Activity_0m4y88u" targetRef="Event_19skg3f" /> <bpmn:userTask id="Activity_0m4y88u" name="领导审批" camunda:assignee="initializer"> <bpmn:incoming>Flow_0u3noaj</bpmn:incoming> <bpmn:incoming>Flow_19qoczq</bpmn:incoming> <bpmn:outgoing>Flow_147a93e</bpmn:outgoing> </bpmn:userTask> <bpmn:sequenceFlow id="Flow_16apd01" sourceRef="Event_1agnt1i" targetRef="Activity_17zxmt1" /> <bpmn:sequenceFlow id="Flow_19qoczq" sourceRef="Activity_17zxmt1" targetRef="Activity_0m4y88u" /> <bpmn:userTask id="Activity_17zxmt1" name="法务审批" camunda:assignee="initializer"> <bpmn:incoming>Flow_16apd01</bpmn:incoming> <bpmn:outgoing>Flow_19qoczq</bpmn:outgoing> </bpmn:userTask> <bpmn:boundaryEvent id="Event_1agnt1i" name="监听子流程事件" attachedToRef="Activity_1bttv2o"> <bpmn:outgoing>Flow_16apd01</bpmn:outgoing> <bpmn:messageEventDefinition id="MessageEventDefinition_1j7zxtw" messageRef="Message_03ju95h" /> </bpmn:boundaryEvent> </bpmn:process> <bpmn:message id="Message_03ju95h" name="Message_3vqgn79" /> <bpmndi:BPMNDiagram id="BPMNDiagram_1"> <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="SubProcessEvent"> <bpmndi:BPMNEdge id="Flow_02dlqgj_di" bpmnElement="Flow_02dlqgj"> <di:waypoint x="188" y="120" /> <di:waypoint x="240" y="120" /> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge id="Flow_0u3noaj_di" bpmnElement="Flow_0u3noaj"> <di:waypoint x="340" y="120" /> <di:waypoint x="400" y="120" /> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge id="Flow_147a93e_di" bpmnElement="Flow_147a93e"> <di:waypoint x="500" y="120" /> <di:waypoint x="562" y="120" /> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge id="Flow_16apd01_di" bpmnElement="Flow_16apd01"> <di:waypoint x="320" y="178" /> <di:waypoint x="320" y="240" /> <di:waypoint x="400" y="240" /> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge id="Flow_19qoczq_di" bpmnElement="Flow_19qoczq"> <di:waypoint x="450" y="200" /> <di:waypoint x="450" y="160" /> </bpmndi:BPMNEdge> <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1"> <dc:Bounds x="152" y="102" width="36" height="36" /> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Activity_0z1rjjp_di" bpmnElement="Activity_1bttv2o"> <dc:Bounds x="240" y="80" width="100" height="80" /> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Event_19skg3f_di" bpmnElement="Event_19skg3f"> <dc:Bounds x="562" y="102" width="36" height="36" /> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Activity_1mv2zv4_di" bpmnElement="Activity_0m4y88u"> <dc:Bounds x="400" y="80" width="100" height="80" /> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Activity_07rk6fj_di" bpmnElement="Activity_17zxmt1"> <dc:Bounds x="400" y="200" width="100" height="80" /> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Event_0177lhu_di" bpmnElement="Event_1agnt1i"> <dc:Bounds x="302" y="142" width="36" height="36" /> <bpmndi:BPMNLabel> <dc:Bounds x="283" y="185" width="77" height="14" /> </bpmndi:BPMNLabel> </bpmndi:BPMNShape> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </bpmn:definitions>
|
子流程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <?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" id="Definitions_06lirnu" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.1.1"> <bpmn:process id="SubProcessEvent_HR" name="子流程事件通知演示_HR环节" isExecutable="true"> <bpmn:startEvent id="StartEvent_1"> <bpmn:outgoing>Flow_18l7yxe</bpmn:outgoing> </bpmn:startEvent> <bpmn:sequenceFlow id="Flow_18l7yxe" sourceRef="StartEvent_1" targetRef="Activity_0rszoyy" /> <bpmn:sequenceFlow id="Flow_0kugaj6" sourceRef="Activity_0rszoyy" targetRef="Event_0xszd8c" /> <bpmn:intermediateThrowEvent id="Event_0xszd8c" name="抛出消息"> <bpmn:incoming>Flow_0kugaj6</bpmn:incoming> <bpmn:messageEventDefinition id="MessageEventDefinition_0uxajxz" messageRef="Message_028qpkp" /> </bpmn:intermediateThrowEvent> <bpmn:userTask id="Activity_0rszoyy" name="HR审批" camunda:assignee="initializer"> <bpmn:incoming>Flow_18l7yxe</bpmn:incoming> <bpmn:outgoing>Flow_0kugaj6</bpmn:outgoing> </bpmn:userTask> </bpmn:process> <bpmn:message id="Message_028qpkp" name="Message_3vqgn79" /> <bpmndi:BPMNDiagram id="BPMNDiagram_1"> <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="SubProcessEvent_HR"> <bpmndi:BPMNEdge id="Flow_0kugaj6_di" bpmnElement="Flow_0kugaj6"> <di:waypoint x="340" y="120" /> <di:waypoint x="392" y="120" /> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge id="Flow_18l7yxe_di" bpmnElement="Flow_18l7yxe"> <di:waypoint x="188" y="120" /> <di:waypoint x="240" y="120" /> </bpmndi:BPMNEdge> <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1"> <dc:Bounds x="152" y="102" width="36" height="36" /> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Event_1px1l0r_di" bpmnElement="Event_0xszd8c"> <dc:Bounds x="392" y="102" width="36" height="36" /> <bpmndi:BPMNLabel> <dc:Bounds x="389" y="145" width="44" height="14" /> </bpmndi:BPMNLabel> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Activity_1sqn9r5_di" bpmnElement="Activity_0rszoyy"> <dc:Bounds x="240" y="80" width="100" height="80" /> </bpmndi:BPMNShape> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </bpmn:definitions>
|
参考
How to Use Business Keys? - Camunda
Call Activity | docs.camunda.org
EOF