camunda如何使用script脚本节点

在Camunda中,脚本任务是一个自动活动,当流程执行到脚本任务时,相关的脚本自动执行。camunda支持大多是兼容JSR-223的脚本引擎实现,比如Groovy, JavaScript, JRuby and Jython。

本文重点描述基于JavaScript 的脚本引擎配置Camunda 脚本服务,完成基于脚本的自动计算,并把计算结果返回给流程变量。

一、设计流程图


以下时脚本节点的关键配置项:


 

脚本计算结果返回值的设置有2种方式:

一是通过result Variable属性设置。通过将流程变量名指定为脚本任务定义的camunda:resultVariable属性的文本值,可以将脚本任务的返回值分配给先前存在的流程变量或新流程变量。特定流程变量的任何现有值都将被脚本执行的结果值覆盖。当没有指定结果变量名时,脚本结果值将被忽略。

二是通过execution.setVariable("变量名","变量值");的方式设置,可以在脚本里设置多个变量,更灵活。

以下是完整的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_0ppniex" 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_0a6gw7u" name="贷款申请流程Script" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:outgoing>Flow_0kzdck2</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="Flow_0kzdck2" sourceRef="StartEvent_1" targetRef="Activity_1l6o8wm" />
    <bpmn:scriptTask id="Activity_1l6o8wm" name="计算贷款额度" scriptFormat="JavaScript">
      <bpmn:incoming>Flow_0kzdck2</bpmn:incoming>
      <bpmn:outgoing>Flow_0h8bikl</bpmn:outgoing>
      <bpmn:script>var yearWages = execution.getVariable("yearWages");
var houseAssets = execution.getVariable("houseAssets");
execution.setVariable("loanLimit", (yearWages+houseAssets)*0.8);</bpmn:script>
    </bpmn:scriptTask>
    <bpmn:sequenceFlow id="Flow_0h8bikl" sourceRef="Activity_1l6o8wm" targetRef="Activity_1wjgiji" />
    <bpmn:userTask id="Activity_1wjgiji" name="确认贷款额度" camunda:assignee="demo">
      <bpmn:incoming>Flow_0h8bikl</bpmn:incoming>
      <bpmn:outgoing>Flow_03h3srs</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:sequenceFlow id="Flow_03h3srs" sourceRef="Activity_1wjgiji" targetRef="Event_0myx83u" />
    <bpmn:endEvent id="Event_0myx83u">
      <bpmn:incoming>Flow_03h3srs</bpmn:incoming>
    </bpmn:endEvent>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0a6gw7u">
      <bpmndi:BPMNEdge id="Flow_03h3srs_di" bpmnElement="Flow_03h3srs">
        <di:waypoint x="600" y="117" />
        <di:waypoint x="692" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0h8bikl_di" bpmnElement="Flow_0h8bikl">
        <di:waypoint x="390" y="117" />
        <di:waypoint x="500" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0kzdck2_di" bpmnElement="Flow_0kzdck2">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="290" 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="Event_0myx83u_di" bpmnElement="Event_0myx83u">
        <dc:Bounds x="692" y="99" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_187in45_di" bpmnElement="Activity_1wjgiji">
        <dc:Bounds x="500" y="77" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_0w973ww_di" bpmnElement="Activity_1l6o8wm">
        <dc:Bounds x="290" y="77" width="100" height="80" />
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

二、发起流程测试

登录:http://localhost:8080/camunda/app/admin/default/#/login

1、发起流程,输入流程变量,后面的脚本节点计算需要这两个流程变量


 

2、提交流程后,查看流程图,Script脚本节点已经成功执行了


 

3、查看表单中的流程变量,Script脚本节点计算后的返回值已经成功写入了。


 

更多参考:

https://docs.camunda.org/manual/7.15/reference/bpmn20/tasks/script-task/

https://github.com/camunda/camunda-bpm-examples/tree/7.15/scripttask

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