当前位置: 首页> 健康> 养生 > Camunda如何通过外部任务与其他系统自动交互

Camunda如何通过外部任务与其他系统自动交互

时间:2025/7/14 3:36:49来源:https://blog.csdn.net/trayvontang/article/details/140412746 浏览次数:0次

文章目录

  • 简介
  • 流程图
  • 外部系统
    • pom.xml
    • logback.xml
    • 监听类
  • 启动流程实例
  • 常见问题
    • Public Key Retrieval is not allowed的解决方法
    • java.lang.reflect.InaccessibleObjectException
  • 流程图xml

简介

前面我们已经介绍了Camunda的基本操作、任务、表:

  1. Camunda组件与服务与基本操作
  2. Camunda ScriptTask SendTask ReceiveTask操作
  3. Camunda中强大的监听服务
  4. Camunda最基本、最常用的表梳理及如何通过服务定位到相关表

这一篇我们介绍一下Camunda的外部任务。

很多时候,任务并不是业务老师来完成,而是外部系统来完成。

这种情况,如何交互呢?

提供接口吗?

Camunda提供了更好的方式,外部服务:只需要在流程图中创建一个服务节点,配置为External就可以。

然后,其他系统可以监听指定的topic,流程到了,就可以获取到通知,完成之后,还可以自动完成任务。

流程图

流程图
设置为type为External,并指定topic。

具体流程图xml我放在最后,需要自取。

外部系统

首先,我们模拟一个外部系统监听任务。

pom.xml

最主要是camunda-external-task-client

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>vip.meet</groupId><artifactId>camunda-out-task</artifactId><version>1.0.0</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.camunda.bpm</groupId><artifactId>camunda-external-task-client</artifactId><version>7.15.0</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.5.6</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.4.0-b180830.0359</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.34</version></dependency></dependencies><build><plugins></plugins></build></project>

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration><property name="log.path" value="D:/logs/camunda-out-task" /><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{50} - %msg%n</pattern></encoder></appender><!--控制框架输出日志--><logger name="org.slf4j" level="INFO"/><logger name="org.springframework" level="INFO"/><root><appender-ref ref="STDOUT" level="INFO" /></root>
</configuration>

监听类

package vip.meet;import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.client.ExternalTaskClient;@Slf4j
public class UpWorker {public static void main(String[] args) {ExternalTaskClient client = ExternalTaskClient.create()// 依据实际情况填写程序的IP和端口.baseUrl("http://localhost:8088/engine-rest").asyncResponseTimeout(10000).lockDuration(10000).workerId("监听流程任务").build();client// 要处理的主题.subscribe("起床了")// 流程实例启动设置的业务key
//                .businessKey("out-task-key")
//                 流程定义时设置的业务key.processDefinitionKeyIn("pi-out-001")
//                .processDefinitionId("pi-out-001:2:ae842ab3-4179-11ef-b67d-ac74b10c44ef").lockDuration(30000)// 处理任务逻辑.handler((externalTask, externalTaskService) -> {String item = externalTask.getVariable("item");Integer money = externalTask.getVariable("money");log.info("获取任务参数:item:{},money:{}", money, item);// 完成任务externalTaskService.complete(externalTask);}).open();}
}

其中:subscribe是要订阅的主题,还可以设置businessKey、processDefinitionKey、processDefinitionId等参数来监听指定流程。

处理逻辑可以通过ExternalTask获取相关流程参数,ExternalTaskService来完成任务。

有几个参数需要说明一下:

  1. lockDuration不是轮询时间,只有一个默认的退火轮询策略,就是如果没有任务,轮询时间间隔会越来越长,直到接受到任务,重新开始计算时间
  2. businessKey:是业务流程启动时指定的实例业务key,多个实例可以使用相同的key
  3. processDefinitionKey:是业务流程定义时,指定的key,多个流程可以使用相同key,只是版本号不同
  4. processDefinitionId:是流程定义Camunda生成的id,这个每次都会变,不建议使用这个来过滤

流程定义key启动key
实例id

启动流程实例

不知道怎么搭建环境的可以先看一下下面这2篇:

  1. Camunda组件与服务与基本操作
  2. Camunda最基本、最常用的表梳理及如何通过服务定位到相关表

可以直接请求 post url:http://localhost:8088/engine-rest/process-definition/key/{流程id}/start

注意版本:启动实例接口文档

{"money": {"value": 1024,"type": "integer"},"item": {"value": "你干嘛"}
}

Camunda启动流程
Camunda指定启动参数
查看任务如果流程中没有设置执行人,这里可以指派。
完成任务
然后我们可以在外部系统查看有没有监听到:
Camunda外部任务监听我们可以看到外部系统已经接收到任务了,在回去看,可以发现,任务已经完成,流程结束。

常见问题

Public Key Retrieval is not allowed的解决方法

数据库url链接中添加allowPublicKeyRetrieval=true参数。

java.lang.reflect.InaccessibleObjectException

Cause: java.lang.reflect.InaccessibleObjectException: Unable to make public int java.util.XXXX.size() accessible: module java.base does not “opens java.util” to unnamed module @xxxx

MyBatis Ognl反射出错,设置vm参数:–add-opens java.base/java.util=ALL-UNNAMED

idea设置vm参数

流程图xml

<?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_1avcqjw" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.19.0" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0"><bpmn:process id="pi-out-001" name="外部任务流程" isExecutable="true" camunda:historyTimeToLive="180"><bpmn:startEvent id="StartEvent_1"><bpmn:outgoing>Flow_1giqjon</bpmn:outgoing></bpmn:startEvent><bpmn:sequenceFlow id="Flow_1giqjon" sourceRef="StartEvent_1" targetRef="Activity_1lwbfpu" /><bpmn:userTask id="Activity_1lwbfpu" name="前置任务" camunda:assignee="demo"><bpmn:incoming>Flow_1giqjon</bpmn:incoming><bpmn:outgoing>Flow_1c1kuaz</bpmn:outgoing></bpmn:userTask><bpmn:sequenceFlow id="Flow_1c1kuaz" sourceRef="Activity_1lwbfpu" targetRef="Activity_0oezzr5" /><bpmn:serviceTask id="Activity_0oezzr5" name="外部任务" camunda:type="external" camunda:topic="起床了"><bpmn:incoming>Flow_1c1kuaz</bpmn:incoming><bpmn:outgoing>Flow_0yftk9w</bpmn:outgoing></bpmn:serviceTask><bpmn:endEvent id="Event_17b6xzm"><bpmn:incoming>Flow_0yftk9w</bpmn:incoming></bpmn:endEvent><bpmn:sequenceFlow id="Flow_0yftk9w" sourceRef="Activity_0oezzr5" targetRef="Event_17b6xzm" /></bpmn:process><bpmndi:BPMNDiagram id="BPMNDiagram_1"><bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="pi-out-001"><bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1"><dc:Bounds x="179" y="102" width="36" height="36" /></bpmndi:BPMNShape><bpmndi:BPMNShape id="Activity_05oodpp_di" bpmnElement="Activity_1lwbfpu"><dc:Bounds x="310" y="80" width="100" height="80" /><bpmndi:BPMNLabel /></bpmndi:BPMNShape><bpmndi:BPMNShape id="Activity_1oc0pm3_di" bpmnElement="Activity_0oezzr5"><dc:Bounds x="510" y="80" width="100" height="80" /><bpmndi:BPMNLabel /></bpmndi:BPMNShape><bpmndi:BPMNShape id="Event_17b6xzm_di" bpmnElement="Event_17b6xzm"><dc:Bounds x="712" y="102" width="36" height="36" /></bpmndi:BPMNShape><bpmndi:BPMNEdge id="Flow_1giqjon_di" bpmnElement="Flow_1giqjon"><di:waypoint x="215" y="120" /><di:waypoint x="310" y="120" /></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="Flow_1c1kuaz_di" bpmnElement="Flow_1c1kuaz"><di:waypoint x="410" y="120" /><di:waypoint x="510" y="120" /></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="Flow_0yftk9w_di" bpmnElement="Flow_0yftk9w"><di:waypoint x="610" y="120" /><di:waypoint x="712" y="120" /></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</bpmn:definitions>
关键字:Camunda如何通过外部任务与其他系统自动交互

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: