首页 > 编程语言 >如何使用 JavaScript 在 Camunda 外部任务中使用瞬态变量

如何使用 JavaScript 在 Camunda 外部任务中使用瞬态变量

时间:2022-09-20 11:12:38浏览次数:89  
标签:www http 变量 bpmn JavaScript 瞬态 Camunda

你怎么做 使用 JavaScript 的 Camunda 外部任务中的瞬态变量

在本文中,让我们看看如何在使用 java 脚本实现 Camunda 外部任务的同时使用瞬态变量。

先决条件:

1. 卡蒙达 7.17

2. Eclipse 与 Maven(自 嵌入式 Camunda 引擎 使用,任何IDE)

3.Java

4. Visual Studio 代码(仅用于编写和测试 Javascript 代码;任何其他方式也可以)

让我们开始

按照链接获得 嵌入式 Camunda 引擎 如果没有提前完成申请。让我们修改 process.bpmn 以具有服务任务的外部实现,如下所示。另一个脚本任务用于打印由 Javascript 外部工作者设置的瞬态变量的值。

以下代码片段可以使用/保存为 bpmn 以实现上述实现。

<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:xsi=”http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda=”http://camunda.org/schema/1.0/bpmn" id=”Definitions_0fr9mxs” targetNamespace=”http://bpmn.io/schema/bpmn" exporter=”Camunda Modeler”

<bpmn:process id=”camunda-new-features-js-externalclient” isExecutable=”true”>

<bpmn:startEvent id=”StartEvent_1" name=”Start”>

bpmn:outgoingFlow_1ql0hfr</bpmn:outgoing>

</bpmn:startEvent>

<bpmn:sequenceFlow id=”Flow_1ql0hfr” sourceRef=”StartEvent_1" targetRef=”Activity_08xjz19" />

<bpmn:endEvent id=”Event_0ptn8u4" name=”End”>

bpmn:extensionElements

<camunda:executionListener event=”end”>

<camunda:script scriptFormat=”JavaScript”>print(“进程执行。”);</camunda:script>

</camunda:executionListener>

</bpmn:extensionElements>

bpmn:incomingFlow_0beer3a</bpmn:incoming>

</bpmn:endEvent>

<bpmn:sequenceFlow id=”Flow_0adr27u” sourceRef=”Activity_08xjz19" targetRef=”Activity_0zpmh2y” />

<bpmn:serviceTask id=”Activity_08xjz19" name=”Call External Javascript Client” camunda:type=”external” camunda:topic=”javascript-client”>

bpmn:incomingFlow_1ql0hfr</bpmn:incoming>

bpmn:outgoingFlow_0adr27u</bpmn:outgoing>

</bpmn:serviceTask>

<bpmn:sequenceFlow id=”Flow_0beer3a” sourceRef=”Activity_0zpmh2y” targetRef=”Event_0ptn8u4" />

<bpmn:scriptTask id=”Activity_0zpmh2y” name=”Print Transient Variable” scriptFormat=”JavaScript”>

bpmn:incomingFlow_0adr27u</bpmn:incoming>

bpmn:outgoingFlow_0beer3a</bpmn:outgoing>

bpmn:scriptprint(“瞬态变量值:”+execution.getVariable(“age”))</bpmn:script>

</bpmn:scriptTask>

</bpmn:process>

<bpmndi:BPMNDiagram id=”BPMNDiagram_1">

<bpmndi:BPMNPlane id=”BPMNPlane_1" bpmnElement=”camunda-new-features-js-externalclient”>

<bpmndi:BPMNEdge id=”Flow_0adr27u_di” bpmnElement=”Flow_0adr27u”>

<di:waypoint x=”400" y=”117" />

<di:waypoint x=”450" y=”117" />

</bpmndi:BPMNEdge>

<bpmndi:BPMNEdge id=”Flow_1ql0hfr_di” bpmnElement=”Flow_1ql0hfr”>

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

<di:waypoint x=”300" y=”117" />

</bpmndi:BPMNEdge>

<bpmndi:BPMNEdge id=”Flow_0beer3a_di” bpmnElement=”Flow_0beer3a”>

<di:waypoint x=”550" y=”117" />

<di:waypoint x=”622" y=”117" />

</bpmndi:BPMNEdge>

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

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

bpmndi:BPMNLabel

<dc:Bounds x=”185" y=”142" width=”25" height=”14" />

</bpmndi:BPMNLabel>

</bpmndi:BPMNShape>

<bpmndi:BPMNShape id=”Activity_14doiiu_di” bpmnElement=”Activity_08xjz19">

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

<bpmndi:BPMNLabel />

</bpmndi:BPMNShape>

<bpmndi:BPMNShape id=”Event_0ptn8u4_di” bpmnElement=”Event_0ptn8u4">

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

bpmndi:BPMNLabel

<dc:Bounds x=”630" y=”142" width=”20" height=”14" />

</bpmndi:BPMNLabel>

</bpmndi:BPMNShape>

<bpmndi:BPMNShape id=”Activity_1grvs1w_di” bpmnElement=”Activity_0zpmh2y”>

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

<bpmndi:BPMNLabel />

</bpmndi:BPMNShape>

</bpmndi:BPMNPlane>

</bpmndi:BPMNDiagram>

</bpmn:definitions>

构建并运行应用程序。

接下来,让我们构建一个 Javascript 工作代码,它可以为上述流程模型设置瞬态变量。

使用下面的代码片段执行相同的操作并保存为“.js”文件,此处的瞬态变量年龄设置为 50。

const {客户端,记录器} = 要求(“camunda-external-task-client-js”);

const {变量} = require(“camunda-external-task-client-js”);

const client = new Client ({baseUrl: “http://localhost:8082/engine-rest”, 使用: logger});

client.subscribe(“javascript-client”, async function({ task, taskService }) {

常量变量=新变量();

variables.setTransient(“年龄”, “50”); //设置进程的瞬态变量

等待 taskService.complete(任务,变量);

});

执行脚本。要获得运行 Javascript 片段的帮助,请遵循 视频教程 由 Camunda 团队提供。

接下来,使用 Camunda cockpit 启动流程实例,结果将在 eclipse/IDE 控制台上打印为瞬态变量的值为 50(在 Javascript 代码中设置)。

让我们尝试验证此过程变量是否保存在 Camunda DB 中。打开 H2 DB(用于此部署)。

选择 * FROM ACT_HI_VARINST

瞬态变量“age”不保存在数据库中,而在过程中可以访问它,并且可以在过程建模中的各种用例中使用。

希望有帮助。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明

本文链接:https://www.qanswer.top/38036/08552011

标签:www,http,变量,bpmn,JavaScript,瞬态,Camunda
From: https://www.cnblogs.com/amboke/p/16710367.html

相关文章

  • JavaScript 类型转换
    强制类型转换        -指将一个数据类型强制转换为其他的数据类型        -类型转换主要指,将其他的数据类型,转换为      ......
  • javascript中的运算符
     重点看带星号的1、===严格相等运算符,用作逻辑判断,为什么不用==呢? 第二条1=='1',解释器会将'1'转化称数字1然后再比较,结果就是true而第三条就是从类型和数值上比较......
  • arcgis for javascript api做缓冲区分析
    需求内容:对某个arcgisserver发布的服务进行缓冲区分析,并且下载缓冲区的结果要素图层思路: 通过arcgis服务的find操作拿到所有的要素对要素进行缓冲区分析下载缓......
  • JavaScript 函数定义
    https://m.runoob.com/js/js-function-definition.html JavaScript使用关键字function定义函数。函数可以通过声明定义,也可以是一个表达式。函数声明在之前的教......
  • JavaScript合并多个数组
    工作中经常会对数组进行合并,稍微总结一下常用的方法:concatJavaScript原生自带的函数,用法如下:letarr1=[3,5,7];letarr2=[4,78,79];letarr3=[];arr3=......
  • javascript中的对象和json对象区别
    json对象和javacript对象长得很像,但有本质不同  1、本质区别:(a)、json对象本质上是字符串,他是客户端和服务器之间传递数据的一种格式,他的属性只是样子货,不能执行。(b)......
  • JavaScript高级复习——js面向对象
    面向对象优点:易维护、易复用、易扩展。适合多人合作。缺点:性能比面向过程低。面向过程优点:性能比面向对象高,适合跟硬件联系很紧密的东西,例如单片机。缺点:没有面向对象......
  • javascript的类的继承
     先看下java的继承,java的继承是发生在类之间,他们的对象之间没有继承关系,子类的对象通过子类创建对象,同时对象中不存储函数只有数据,函数需找到类定义以后读取里面的函数,类......
  • JavaScript break 和 continue 语句
    break语句用于跳出循环。continue用于跳过循环中的一个迭代。break语句break语句可用于跳出循环。break语句跳出循环后,会继续执行该循环之后的代码continue语句......
  • JavaScript for 循环
    循环可以将代码块执行指定的次数。for循环:如果向多次运行相同代码,且每次的值都是不同,那就使用循环不同类型的循环JavaScript支持不同类型的循环:for -循环代码块一......