camunda调用Java类(JavaDelegate)实现业务自动化

2022/6/14 1:22:41

本文主要是介绍camunda调用Java类(JavaDelegate)实现业务自动化,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

​ 在Camunda中,有四种方法来声明如何调用Java逻辑:

  1. 指定实现JavaDelegate或ActivityBehavior的类
  2. 计算解析为委托对象的表达式
  3. 调用方法表达式
  4. 求值表达式的值

本文重点描述基于JavaDelegate配置Camunda 的自动化服务节点,在JavaDelegate实现类里完成逻辑计算,并把计算结果返回给流程变量。

一、设计流程图

 

​编辑

 

要指定在进程执行期间调用的类,camunda:class属性需要提供完全限定的类名。要实现可以在流程执行期间调用的类,这个类需要实现org.camunda.bpm.engine.delegate.JavaDelegate接口,并在execute方法中提供所需的逻辑。当流程执行到达这个特定的步骤时,它将执行该方法中定义的逻辑,并以默认的BPMN 2.0方式保留活动。

作为示例,让我们创建一个Java类,实现里模拟业务逻辑。这个类需要实现org.camunda.bpm.engine.delegate.JavaDelegate接口,它需要我们实现execute(DelegateExecution)方法。引擎将调用这个操作,并且需要包含业务逻辑。流程实例信息(如流程变量和其他信息)可以通过DelegateExecution接口访问和操作。

JavaDelegate示例实现类:

package com.example.demo1;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

/**
 * 贷款计算类
 */
public class LoanDelegate  implements JavaDelegate {

    @Override
    public void execute(DelegateExecution delegateExecution) throws Exception {
        Double yearWages = (Double) delegateExecution.getVariable("yearWages");
        Double houseAssets = (Double) delegateExecution.getVariable("houseAssets");
        double sum = yearWages + houseAssets;
        if(sum<=0){
            delegateExecution.setVariable("creditRating","C");
            delegateExecution.setVariable("loanLimit",0);
        }else  if(sum>0 && sum <=100){
            delegateExecution.setVariable("creditRating","B");
            delegateExecution.setVariable("loanLimit",sum*0.8);
        }else  if(sum>100){
            delegateExecution.setVariable("creditRating","A");
            delegateExecution.setVariable("loanLimit",sum*1.2);
        }
    }
}

以下是完整的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_1wr7a1v" 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="贷款申请流程Java" 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: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:serviceTask id="Activity_1l6o8wm" name="计算贷款额度" camunda:class="com.example.demo1.LoanDelegate">
      <bpmn:incoming>Flow_0kzdck2</bpmn:incoming>
      <bpmn:outgoing>Flow_0h8bikl</bpmn:outgoing>
    </bpmn:serviceTask>
  </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="Activity_187in45_di" bpmnElement="Activity_1wjgiji">
        <dc:Bounds x="500" y="77" width="100" height="80" />
      </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_05149db_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、发起流程,输入流程变量,后面的Java代理节点计算需要这两个流程变量

​编辑

 

2、提交流程后,查看流程图,Java代理节点已经成功执行了

​编辑

 

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

​编辑

 

 

更多参考:

Service Task | docs.camunda.org

 



这篇关于camunda调用Java类(JavaDelegate)实现业务自动化的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程