camunda如何调用HTTP(Service Task)服务节点

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模型文件:

<?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_1qvw7a2" 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="贷款申请流程HTTP" 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_0bvh293" />
    <bpmn:sequenceFlow id="Flow_07gaddc" sourceRef="Activity_0bvh293" targetRef="Activity_0kimj7k" />
    <bpmn:serviceTask id="Activity_0bvh293" name="贷款额度计算">
      <bpmn:extensionElements>
        <camunda:connector>
          <camunda:inputOutput>
            <camunda:inputParameter name="headers">
              <camunda:map>
                <camunda:entry key="Content-Type">text/plain</camunda:entry>
                <camunda:entry key="Accept">application/json</camunda:entry>
              </camunda:map>
            </camunda:inputParameter>
            <camunda:inputParameter name="method">POST</camunda:inputParameter>
            <camunda:inputParameter name="url">http://127.0.0.1:8888/userLoan/computeLoan?yearWages=${yearWages}&amp;houseAssets=${houseAssets}</camunda:inputParameter>
            <camunda:outputParameter name="creditRating">
              <camunda:script scriptFormat="JavaScript">var response = connector.getVariable("response");
var user = S(response);
var creditRating = user.prop("creditRating").stringValue();
creditRating;</camunda:script>
            </camunda:outputParameter>
            <camunda:outputParameter name="loanLimit">
              <camunda:script scriptFormat="JavaScript">var response = connector.getVariable("response");
var user = S(response);
var loanLimit = user.prop("loanLimit").numberValue();
loanLimit;</camunda:script>
            </camunda:outputParameter>
            <camunda:outputParameter name="loanUser">${response}</camunda:outputParameter>
          </camunda:inputOutput>
          <camunda:connectorId>http-connector</camunda:connectorId>
        </camunda:connector>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_0kzdck2</bpmn:incoming>
      <bpmn:outgoing>Flow_07gaddc</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:userTask id="Activity_0kimj7k" name="确认贷款额度" camunda:assignee="demo">
      <bpmn:incoming>Flow_07gaddc</bpmn:incoming>
      <bpmn:outgoing>Flow_09mofy3</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:sequenceFlow id="Flow_09mofy3" sourceRef="Activity_0kimj7k" targetRef="Event_0myx83u" />
    <bpmn:endEvent id="Event_0myx83u">
      <bpmn:incoming>Flow_09mofy3</bpmn:incoming>
    </bpmn:endEvent>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0a6gw7u">
      <bpmndi:BPMNEdge id="Flow_09mofy3_di" bpmnElement="Flow_09mofy3">
        <di:waypoint x="570" y="117" />
        <di:waypoint x="662" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_07gaddc_di" bpmnElement="Flow_07gaddc">
        <di:waypoint x="370" y="117" />
        <di:waypoint x="470" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0kzdck2_di" bpmnElement="Flow_0kzdck2">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="270" 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_0rfgq1k_di" bpmnElement="Activity_0bvh293">
        <dc:Bounds x="270" y="77" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_16ovae8_di" bpmnElement="Activity_0kimj7k">
        <dc:Bounds x="470" y="77" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_0myx83u_di" bpmnElement="Event_0myx83u">
        <dc:Bounds x="662" y="99" width="36" height="36" />
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

三、开发HTTP服务

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

import java.io.Serializable;

public class User implements Serializable {

    private String userName; //姓名

    private int userAge; //年龄

    private double yearWages; //年薪

    private double houseAssets; //房产

    private String creditRating; //信用等级

    private double loanLimit; //贷款额度

    private String isTransferAccount;  //是否已放贷

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public double getYearWages() {
        return yearWages;
    }

    public void setYearWages(double yearWages) {
        this.yearWages = yearWages;
    }

    public double getHouseAssets() {
        return houseAssets;
    }

    public void setHouseAssets(double houseAssets) {
        this.houseAssets = houseAssets;
    }

    public String getCreditRating() {
        return creditRating;
    }

    public void setCreditRating(String creditRating) {
        this.creditRating = creditRating;
    }

    public double getLoanLimit() {
        return loanLimit;
    }

    public void setLoanLimit(double loanLimit) {
        this.loanLimit = loanLimit;
    }

    public String getIsTransferAccount() {
        return isTransferAccount;
    }

    public void setIsTransferAccount(String isTransferAccount) {
        this.isTransferAccount = isTransferAccount;
    }
}

2. 开发REST服务类

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/userLoan")
public class UserLoanController {

    @PostMapping("/computeLoan")
    public User computeLoan(double yearWages,double houseAssets){
        User user=new User();
        user.setYearWages(yearWages);
        user.setHouseAssets(houseAssets);
        double sum = yearWages + houseAssets;
        if(sum<=0){
            user.setCreditRating("C");
            user.setLoanLimit(0);
        }else  if(sum>0 && sum <=100){
            user.setCreditRating("B");
            user.setLoanLimit(sum*0.8);
        }else  if(sum>100){
            user.setCreditRating("A");
            user.setLoanLimit(sum*1.2);
        }
        return user;
    }
}

四、发起流程测试

登录: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

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Camunda是一种用于管理和执行业务流程的开源工作流引擎。在Camunda中,Service Task是一种任务类型,用于执行具体的逻辑操作。而Java是一种编程语言,可以在Camunda中使用来开发和定制Service Task。 使用CamundaService Task Java功能,可以通过编写Java代码来执行各种业务逻辑。通过创建一个Java Delegate类,我们可以定义任务的执行过程。在Java Delegate类中,我们可以访问Camunda提供的API,对流程中的数据进行操作,并与外部系统进行交互。 要使用Camunda Service Task Java功能,需要按照以下步骤进行: 1. 创建一个Java类,实现org.camunda.bpm.engine.delegate.JavaDelegate接口。 2. 在Java类中,实现execute方法,该方法定义了任务的执行逻辑。 3. 在execute方法中,可以使用Camunda提供的API与流程引擎进行交互,例如获取流程变量、发送消息或启动子流程等。 4. 在Camunda中,将Service Task节点与Java Delegate类进行关联,以便在执行过程中调用指定的Java代码。 使用Camunda Service Task Java功能的一个示例是将某些业务逻辑封装在一个自定义的Java类中,然后在流程执行过程中调用该类以执行特定的任务。这样可以在执行流程时,根据具体的业务需求,定制和扩展业务逻辑。 总之,CamundaService Task Java功能可以通过编写Java代码来实现定制的业务逻辑,并在流程执行过程中执行特定的任务。通过与Camunda的API进行交互,可以实现与外部系统的集成,进一步增强和扩展业务流程的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大龄码农有梦想

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值