​ Camunda中的Service Task(服务任务)用于调用服务。在Camunda中,可以通过调用本地Java代码、外部工作项、web服务形式实现的逻辑来完成的。

本文重点描述如何使用web服务实现Camunda服务调用,即如何使用camunda Connector连接器调用HTTP服务,本地Java代码和外部工作项后续文章中再详细介绍。

Camunda Connect提供HTTP和SOAP HTTP连接器。本示例演示了使用http连接器从Camunda BPM中的服务任务调用REST服务。

一、添加项目依赖

给项目中添加camunda  connect 和 camunda spin 包依赖,重新启动camunda Platform。

<dependency>
    <groupId>org.camunda.bpm</groupId>
    <artifactId>camunda-engine-plugin-connect</artifactId>
    <version>7.15.0</version>
</dependency>

<dependency>
    <groupId>org.camunda.bpm</groupId>
    <artifactId>camunda-engine-plugin-spin</artifactId>
    <version>7.15.0</version>
</dependency>
<dependency>
    <groupId>org.camunda.spin</groupId>
    <artifactId>camunda-spin-dataformat-json-jackson</artifactId>
    <version>1.10.1</version>
</dependency>
<dependency>
    <groupId>org.camunda.spin</groupId>
    <artifactId>camunda-spin-core</artifactId>
    <version>1.10.1</version>
</dependency>
<dependency>
    <groupId>org.camunda.spin</groupId>
    <artifactId>camunda-spin-dataformat-all</artifactId>
    <version>1.10.1</version>
</dependency>

二、设计流程图

​编辑

以下时服务节点的关键配置项:

HTTP连接器可用于创建新请求、设置HTTP方法、URL、内容类型和有效负载。

The HTTP connector can be used to create a new request, set a HTTP method, URL, content type and payload.

Parameter

Description

method

Sets the HTTP method of the request

url

Sets the URL of the request

headers

Contains a map of the configured HTTP headers of the request

payload

Sets the payload of the request

​编辑

​编辑

​编辑

​编辑

返回值包含状态码、响应头和响应体。

A response contains the status code, response headers and body.

Parameter

Description

statusCode

Contains the status code of the response

headers

Contains a map with the HTTP headers of the response

response

Contains the response body

​编辑

以下是完整的BPMN模型文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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_1qvw7a2" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.8.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
  3. <bpmn:process id="Process_0a6gw7u" name="贷款申请流程HTTP" isExecutable="true">
  4. <bpmn:startEvent id="StartEvent_1">
  5. <bpmn:outgoing>Flow_0kzdck2</bpmn:outgoing>
  6. </bpmn:startEvent>
  7. <bpmn:sequenceFlow id="Flow_0kzdck2" sourceRef="StartEvent_1" targetRef="Activity_0bvh293" />
  8. <bpmn:sequenceFlow id="Flow_07gaddc" sourceRef="Activity_0bvh293" targetRef="Activity_0kimj7k" />
  9. <bpmn:serviceTask id="Activity_0bvh293" name="贷款额度计算">
  10. <bpmn:extensionElements>
  11. <camunda:connector>
  12. <camunda:inputOutput>
  13. <camunda:inputParameter name="headers">
  14. <camunda:map>
  15. <camunda:entry key="Content-Type">text/plain</camunda:entry>
  16. <camunda:entry key="Accept">application/json</camunda:entry>
  17. </camunda:map>
  18. </camunda:inputParameter>
  19. <camunda:inputParameter name="method">POST</camunda:inputParameter>
  20. <camunda:inputParameter name="url">http://127.0.0.1:8888/userLoan/computeLoan?yearWages=${yearWages}&amp;houseAssets=${houseAssets}</camunda:inputParameter>
  21. <camunda:outputParameter name="creditRating">
  22. <camunda:script scriptFormat="JavaScript">var response = connector.getVariable("response");
  23. var user = S(response);
  24. var creditRating = user.prop("creditRating").stringValue();
  25. creditRating;</camunda:script>
  26. </camunda:outputParameter>
  27. <camunda:outputParameter name="loanLimit">
  28. <camunda:script scriptFormat="JavaScript">var response = connector.getVariable("response");
  29. var user = S(response);
  30. var loanLimit = user.prop("loanLimit").numberValue();
  31. loanLimit;</camunda:script>
  32. </camunda:outputParameter>
  33. <camunda:outputParameter name="loanUser">${response}</camunda:outputParameter>
  34. </camunda:inputOutput>
  35. <camunda:connectorId>http-connector</camunda:connectorId>
  36. </camunda:connector>
  37. </bpmn:extensionElements>
  38. <bpmn:incoming>Flow_0kzdck2</bpmn:incoming>
  39. <bpmn:outgoing>Flow_07gaddc</bpmn:outgoing>
  40. </bpmn:serviceTask>
  41. <bpmn:userTask id="Activity_0kimj7k" name="确认贷款额度" camunda:assignee="demo">
  42. <bpmn:incoming>Flow_07gaddc</bpmn:incoming>
  43. <bpmn:outgoing>Flow_09mofy3</bpmn:outgoing>
  44. </bpmn:userTask>
  45. <bpmn:sequenceFlow id="Flow_09mofy3" sourceRef="Activity_0kimj7k" targetRef="Event_0myx83u" />
  46. <bpmn:endEvent id="Event_0myx83u">
  47. <bpmn:incoming>Flow_09mofy3</bpmn:incoming>
  48. </bpmn:endEvent>
  49. </bpmn:process>
  50. <bpmndi:BPMNDiagram id="BPMNDiagram_1">
  51. <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0a6gw7u">
  52. <bpmndi:BPMNEdge id="Flow_09mofy3_di" bpmnElement="Flow_09mofy3">
  53. <di:waypoint x="570" y="117" />
  54. <di:waypoint x="662" y="117" />
  55. </bpmndi:BPMNEdge>
  56. <bpmndi:BPMNEdge id="Flow_07gaddc_di" bpmnElement="Flow_07gaddc">
  57. <di:waypoint x="370" y="117" />
  58. <di:waypoint x="470" y="117" />
  59. </bpmndi:BPMNEdge>
  60. <bpmndi:BPMNEdge id="Flow_0kzdck2_di" bpmnElement="Flow_0kzdck2">
  61. <di:waypoint x="215" y="117" />
  62. <di:waypoint x="270" y="117" />
  63. </bpmndi:BPMNEdge>
  64. <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
  65. <dc:Bounds x="179" y="99" width="36" height="36" />
  66. </bpmndi:BPMNShape>
  67. <bpmndi:BPMNShape id="Activity_0rfgq1k_di" bpmnElement="Activity_0bvh293">
  68. <dc:Bounds x="270" y="77" width="100" height="80" />
  69. </bpmndi:BPMNShape>
  70. <bpmndi:BPMNShape id="Activity_16ovae8_di" bpmnElement="Activity_0kimj7k">
  71. <dc:Bounds x="470" y="77" width="100" height="80" />
  72. </bpmndi:BPMNShape>
  73. <bpmndi:BPMNShape id="Event_0myx83u_di" bpmnElement="Event_0myx83u">
  74. <dc:Bounds x="662" y="99" width="36" height="36" />
  75. </bpmndi:BPMNShape>
  76. </bpmndi:BPMNPlane>
  77. </bpmndi:BPMNDiagram>
  78. </bpmn:definitions>

三、开发HTTP服务

1. 新建springboot工程,开发User类

  1. import java.io.Serializable;
  2. public class User implements Serializable {
  3. private String userName; //姓名
  4. private int userAge; //年龄
  5. private double yearWages; //年薪
  6. private double houseAssets; //房产
  7. private String creditRating; //信用等级
  8. private double loanLimit; //贷款额度
  9. private String isTransferAccount; //是否已放贷
  10. public String getUserName() {
  11. return userName;
  12. }
  13. public void setUserName(String userName) {
  14. this.userName = userName;
  15. }
  16. public int getUserAge() {
  17. return userAge;
  18. }
  19. public void setUserAge(int userAge) {
  20. this.userAge = userAge;
  21. }
  22. public double getYearWages() {
  23. return yearWages;
  24. }
  25. public void setYearWages(double yearWages) {
  26. this.yearWages = yearWages;
  27. }
  28. public double getHouseAssets() {
  29. return houseAssets;
  30. }
  31. public void setHouseAssets(double houseAssets) {
  32. this.houseAssets = houseAssets;
  33. }
  34. public String getCreditRating() {
  35. return creditRating;
  36. }
  37. public void setCreditRating(String creditRating) {
  38. this.creditRating = creditRating;
  39. }
  40. public double getLoanLimit() {
  41. return loanLimit;
  42. }
  43. public void setLoanLimit(double loanLimit) {
  44. this.loanLimit = loanLimit;
  45. }
  46. public String getIsTransferAccount() {
  47. return isTransferAccount;
  48. }
  49. public void setIsTransferAccount(String isTransferAccount) {
  50. this.isTransferAccount = isTransferAccount;
  51. }
  52. }

2. 开发REST服务类

  1. import org.springframework.web.bind.annotation.GetMapping;
  2. import org.springframework.web.bind.annotation.PostMapping;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. @RequestMapping("/userLoan")
  7. public class UserLoanController {
  8. @PostMapping("/computeLoan")
  9. public User computeLoan(double yearWages,double houseAssets){
  10. User user=new User();
  11. user.setYearWages(yearWages);
  12. user.setHouseAssets(houseAssets);
  13. double sum = yearWages + houseAssets;
  14. if(sum<=0){
  15. user.setCreditRating("C");
  16. user.setLoanLimit(0);
  17. }else if(sum>0 && sum <=100){
  18. user.setCreditRating("B");
  19. user.setLoanLimit(sum*0.8);
  20. }else if(sum>100){
  21. user.setCreditRating("A");
  22. user.setLoanLimit(sum*1.2);
  23. }
  24. return user;
  25. }
  26. }

四、发起流程测试

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

1、发起流程,输入流程变量,后面的服务节点需要这两个流程变量

​编辑

2、提交流程后,查看流程图,HTTP服务节点已经成功执行了

​编辑

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

​编辑

更多参考:

Camunda Connector Reference | docs.camunda.org

camunda-bpm-examples/servicetask at 7.15 · camunda/camunda-bpm-examples · GitHub

云程 | 云BPM,云程BPM,低代码平台,低代码开发平台,开源流程引擎,Camunda,flowable,业务流程管理,activiti,智能表单,电子表单,可视化开发,零代码开发,基础平台,流程PaaS,流程SaaS

camunda如何调用HTTP REST(Service Task)服务节点的更多相关文章

  1. 翻译-使用Spring调用SOAP Web Service

    原文链接: http://spring.io/guides/gs/consuming-web-service/ 调用SOAP web service 本指南将指导你使用Spring调用一个基于SOAP ...

  2. C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  3. WebService学习总结(四)——调用第三方提供的webService服务

    http://www.cnblogs.com/xdp-gacl/p/4260627.html 互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他 ...

  4. java程序调用xfire发布的webService服务(二)

    在上一篇的调用xfire发布的webService服务中,我只是从服务端返回了一个字符串给客户端,却没有测试从客户端传递数据给服务端.而实际应用中一般是不太可能只出现这样的应用场景的,因此我便更进一步 ...

  5. WebService学习--(四)调用第三方提供的webService服务

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...

  6. C#作为客户端调用gsoap生成的C++服务端

    近日在学习C++,偶然遇到网友想用C#调用gsoap生成的C++服务的问题,遂决定研究一下,网上搜索了很久,大多数是C++调用C#的应用.... 经过本人的不断努力,终于找到一种解决问题的方法,总结如 ...

  7. WebService学习总结——调用第三方提供的webService服务

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据. 气象中心的管理系统将收 ...

  8. 延迟调用或多次调用第三方的Web API服务

    当我们调用第三方的Web API服务的时候,不一定每次都是成功的.这时候,我们可能会再多尝试几次,也有可能延迟一段时间再去尝试调用服务. Task的静态方法Delay允许我们延迟执行某个Task,此方 ...

  9. 在Android中调用KSOAP2库访问webservice服务出现的服务端传入参数为null的问题解决

    ksoap2-android-3.0.0-jar 第三方库来调用.net 写的Web Service 如果没有参数,那么调用一切顺利,但是如果服务是带参数的,那么服务端接收的参数都是nul.      ...

随机推荐

  1. Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa(解决的两种方式)

    异常问题: 下班之前升级了一下Git的版本,结果第二天过来拉取远程最新代码的时候就提示了下面的异常问题: Unable to negotiate with xx.xxx.xxxx port 22: n ...

  2. js知识梳理3:创建对象的模式探究

    写在前面 注:这个系列是本人对js知识的一些梳理,其中不少内容来自书籍:Javascript高级程序设计第三版和JavaScript权威指南第六版,感谢它们的作者和译者.有发现什么问题的,欢迎留言指出 ...

  3. MFC---简介、编码、结构和消息响应

    MFC简介 MFC是微软基础类库的简称,是微软公司实现的一个c++类库,主要封装了大部分的windows API函数 在MFC中,可以直接调用 windows API,同时需要引用对应的头文件或库文件 ...

  4. LevelDB 学习笔记1:布隆过滤器

    LevelDB 学习笔记1:布隆过滤器 底层是位数组,初始都是 0 插入时,用 k 个哈希函数对插入的数字做哈希,并用位数组长度取余,将对应位置 1 查找时,做同样的哈希操作,查看这些位的值 如果所有 ...

  5. Git上传本地仓库文件到Gitee(Github同理)

    前言:本来想把最近的代码更新到Github上,但是校园网打不开,于是决定暂时先更新到Gitee中,Github中的操作也同理. 1. 创建云仓库: 就是在Gitee/Github上创建仓库,这里不演示 ...

  6. 今天写了一个可以测试并发数和运行次数的压力测试代码。(Java)

    今天写了一个可以测试并发数和运行次数的压力测试代码 介绍一下为什么会写这么一个工具. 介绍一个这个工具怎么用的. 背景 最近在开发CoapServer端,以及模拟设备侧发送数据调用开发好的CoapSe ...

  7. 被人DDoS攻击了,分析一下原理和防护

    一.行业现象 1.1 为什么要攻击? 常见的,一个是同行恶意竞争,一个是敲诈勒索. 无论是传统行业的线下门店,还是互联网行业的门户网站.APP产品,都存在着竞争关系,争相获得更多客源,究其目的,无非是 ...

  8. JMeter如何设置中文

    打开Option => Choose Language => Chinese

  9. 为何PostgreSQL即将超越SQL Server?

    DB-Engines 2021年10月份统计,PostgreSQL当月上升10.30点,总分是597.27:SQLServer当月大幅下降16.32,总分是954.29 .按照这样的速度,2年之内,P ...

  10. Unity制作一个小星球

    制作过程 在场景中新建一个球体(Planet)和一个胶囊(Player),适当缩放并添加材质,这里胶囊会被视为玩家 然后将摄像机设为胶囊(Player)的子物体 自行调整合适的摄像机视角 新建脚本Gr ...