描述

业务任务通常是用来调用业务系统,camunda中可以调用Java代码或者rest api调用

业务任务分类

在camunda中,业务任务实现方式有5种

  • Java Class
  • Expression
  • Delegate expression
  • External
  • Connector

需求

假设用户预约电器公司上门维修家电,然后师傅上门维修,完成后公司回访客户对师傅服务打分,师傅查询自己的评分。

流程设计

预约装修

image.png

package com.example.workflow.servicetask;

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

/**
 * @author Helen
 * @version 1.0
 * @createTime 2023/3/28
 * @Description 预约维修
 */
public class ReserveReRepairService implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) throws Exception {
        System.out.println("进入到预约维修家电修理任务");
        String currentActivityName = execution.getCurrentActivityName();

        // 预约的具体调用
        String processDefinitionId = execution.getProcessDefinitionId();
        System.out.println("当前活动名称:" + currentActivityName + "流程定义id:" + processDefinitionId);
    }
}

师傅上门维修

使用Delegate expression实现业务任务,使用EL表达式接收实现任务的Bean,不用带方法名,默认调用execute方法。实现任务的Bean需要implements JavaDeletegate接口。

image.png

 
package com.example.workflow.servicetask;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;

/**
 * @author Helen
 * @version 1.0
 * @createTime 2023/3/28
 * @Description
 */
@Service("doRepair")
public class DoingRepairService implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        System.out.println("进入到预约家电维修任务");
        String currentActivityName = execution.getCurrentActivityName();
        // 预约的具体调用
        String processDefinitionId = execution.getProcessDefinitionId();
        System.out.println("当前活动名称:" + currentActivityName + "流程定义id:" + processDefinitionId);
    }
}


公司电话回访

使用Expression实现业务任务,使用EL表达式接收bean.callMethod(参数)的方式执行,也可以是一行普通的表达式,比如$(a==b?a:b},并且将方法执行结果存入Result Variable;

相比Delegate expression,此种方式实现任务的Bean无须实现implements JavaDeletegate接口,可以是任意方法,任意参数,需要用到流程执行的参数,可以直接传入execution.

调用TelCallService中doCall方法中模拟给师傅打了10分。return 10会自动存入流程中的 score变量


package com.example.workflow.servicetask;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Service;

/**
 * @author Helen
 * @version 1.0
 * @createTime 2023/3/28
 * @Description 公司电话回访
 */
@Service("telCall")
public class TelCallService {

    private long doCall(DelegateExecution execution) {
        System.out.println("开始电话回访");
        String repairManName = String.valueOf("repairManName");
        System.out.println("您对" + repairManName + "的服务打几分");
        return 10;
    }

    public void getScore(DelegateExecution execution) {
        System.out.println("查询评分");
        String repairManName = String.valueOf("repairManName");
        int score = (int) execution.getVariable("score");
        System.out.println("顾客对" + repairManName + "的评分:" + score);
    }
}

完整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:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_16w2t7e" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.9.0" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.18.0">
  <bpmn:process id="Process_service_task" name="service_task" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:outgoing>Flow_0au84kw</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:serviceTask id="Activity_1ggy9f6" name="预约维修家电" camunda:class="com.example.workflow.servicetask.ReserveReRepairService">
      <bpmn:incoming>Flow_0au84kw</bpmn:incoming>
      <bpmn:outgoing>Flow_177h56b</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="Flow_0au84kw" sourceRef="StartEvent_1" targetRef="Activity_1ggy9f6" />
    <bpmn:serviceTask id="Activity_0tn158x" name="上门修理" camunda:delegateExpression="${doRepair}">
      <bpmn:incoming>Flow_177h56b</bpmn:incoming>
      <bpmn:outgoing>Flow_06b7lrq</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="Flow_177h56b" sourceRef="Activity_1ggy9f6" targetRef="Activity_0tn158x" />
    <bpmn:serviceTask id="Activity_1mq0h11" name="公司电话回访" camunda:expression="${telCall.doCall(execution)}" camunda:resultVariable="score">
      <bpmn:incoming>Flow_06b7lrq</bpmn:incoming>
      <bpmn:outgoing>Flow_1twqlgx</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="Flow_06b7lrq" sourceRef="Activity_0tn158x" targetRef="Activity_1mq0h11" />
    <bpmn:serviceTask id="Activity_144un60" name="查看评分" camunda:expression="${telCall.getScore(execution)}">
      <bpmn:incoming>Flow_1twqlgx</bpmn:incoming>
      <bpmn:outgoing>Flow_17bz79o</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="Flow_1twqlgx" sourceRef="Activity_1mq0h11" targetRef="Activity_144un60" />
    <bpmn:endEvent id="Event_0cse7cg">
      <bpmn:incoming>Flow_17bz79o</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="Flow_17bz79o" sourceRef="Activity_144un60" targetRef="Event_0cse7cg" />
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_service_task">
      <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_1ggy9f6_di" bpmnElement="Activity_1ggy9f6">
        <dc:Bounds x="270" y="77" width="100" height="80" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_0tn158x_di" bpmnElement="Activity_0tn158x">
        <dc:Bounds x="430" y="77" width="100" height="80" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_1mq0h11_di" bpmnElement="Activity_1mq0h11">
        <dc:Bounds x="590" y="77" width="100" height="80" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_144un60_di" bpmnElement="Activity_144un60">
        <dc:Bounds x="750" y="77" width="100" height="80" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_0cse7cg_di" bpmnElement="Event_0cse7cg">
        <dc:Bounds x="912" y="99" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="Flow_0au84kw_di" bpmnElement="Flow_0au84kw">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="270" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_177h56b_di" bpmnElement="Flow_177h56b">
        <di:waypoint x="370" y="117" />
        <di:waypoint x="430" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_06b7lrq_di" bpmnElement="Flow_06b7lrq">
        <di:waypoint x="530" y="117" />
        <di:waypoint x="590" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_1twqlgx_di" bpmnElement="Flow_1twqlgx">
        <di:waypoint x="690" y="117" />
        <di:waypoint x="750" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_17bz79o_di" bpmnElement="Flow_17bz79o">
        <di:waypoint x="850" y="117" />
        <di:waypoint x="912" y="117" />
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>


image.png

标签:

作者:迷失的小鹿308
链接:https://juejin.cn/post/7217013944393039928
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐