Camunda中强大的监听服务

2024-01-30 20:36
文章标签 服务 监听 强大 camunda

本文主要是介绍Camunda中强大的监听服务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 简介
  • 创建工程
  • JavaDelegate
  • TaskListener
  • ExecutionListener
  • 部署发起流程
    • CustomExecutionListener开始节点
    • CustomExecutionListener
    • CustomJavaDelegate
    • CustomExecutionListener
    • CustomTaskListener用户节点
  • Expression
  • Delegate Expression
  • 流程图

简介

Camunda预览了很多接口,以便于我们扩展,其中最重要的莫过于各种监听接口,本文就将接受三个最终常用的接口:

  1. ExecutionListener
  2. JavaDelegate
  3. TaskListener

并介绍下面3个ListenerType的区别:

  1. Java Class
  2. Expression
  3. Delegate expression

创建工程

我们还是使用SpringBoot来创建工程,可以通过下面链接生成一个集成Camunda的SpringBoot工程

SpringBoot Camunda工程生成

也可以直接拷贝下面的pom自己创建:

<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>com.example.workflow</groupId><artifactId>my-project</artifactId><version>1.0.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>3.1.1</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.camunda.bpm</groupId><artifactId>camunda-bom</artifactId><version>7.20.0</version><scope>import</scope><type>pom</type></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.camunda.bpm.springboot</groupId><artifactId>camunda-bpm-spring-boot-starter-rest</artifactId></dependency><dependency><groupId>org.camunda.bpm.springboot</groupId><artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId></dependency><dependency><groupId>org.camunda.bpm</groupId><artifactId>camunda-engine-plugin-spin</artifactId></dependency><dependency><groupId>org.camunda.spin</groupId><artifactId>camunda-spin-dataformat-all</artifactId></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>3.1.1</version></plugin></plugins></build></project>

如果不想使用默认的H2数据库,可以参考下面修改配置文件:application.yaml

#spring.datasource.url: jdbc:h2:file:./camunda-h2-databasecamunda.bpm.admin-user:id: demopassword: demo#camunda.bpm.database:
#  schema-update: drop-createspring.datasource:url: jdbc:mysql://127.0.0.1:3306/clearn?characterEncoding=UTF-8&useUnicode=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghaidriver-class-name: com.mysql.cj.jdbc.Driverusername: timpassword: pd

两点需要注意:

  1. 需要添加相关数据库驱动lib
  2. camunda.bpm.database.schema-update好像不生效,需要手动创建一下Camunda相关表

自己测试,如果怕麻烦,直接使用默认的H2数据库即可。

然后就可以直接启动了:

@SpringBootApplication
public class Application {public static void main(String... args) {SpringApplication.run(Application.class, args);}}

JavaDelegate

对于Camunda的服务节点,我们可以绑定一个Java类,这个Java类只需要实现org.camunda.bpm.engine.delegate.JavaDelegate接口即可。

Type选Java Class:
服务节点

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;public class CustomJavaDelegate implements JavaDelegate {@Overridepublic void execute(DelegateExecution delegateExecution) throws Exception {System.out.println(CustomJavaDelegate.class.getName() + "--start");System.out.println("getProcessInstanceId:" + delegateExecution.getProcessInstanceId());System.out.println("getActivityInstanceId:" + delegateExecution.getActivityInstanceId());System.out.println("getCurrentActivityId:" + delegateExecution.getCurrentActivityId());System.out.println("getCurrentActivityName:" + delegateExecution.getCurrentActivityName());System.out.println("getProcessBusinessKey:" + delegateExecution.getProcessBusinessKey());System.out.println("getProcessDefinitionId:" + delegateExecution.getProcessDefinitionId());System.out.println("getBusinessKey:" + delegateExecution.getBusinessKey());System.out.println("getEventName:" + delegateExecution.getEventName());Double paramA = (Double) delegateExecution.getVariable("paramA");String paramB = (String) delegateExecution.getVariable("paramB");System.out.println(paramA);System.out.println(paramB);delegateExecution.setVariable("CustomJavaDelegateP1","p1");delegateExecution.setVariable("CustomJavaDelegateP2",500);System.out.println(CustomJavaDelegate.class.getName() + "--end");}
}

TaskListener

对于用户节点,我们可以配置TaskListener,来监听用户节点的一些事件,如:

  1. create
  2. assignment
  3. delete
  4. complete
  5. update
  6. timeout

user task

import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;public class CustomTaskListener implements TaskListener {@Overridepublic void notify(DelegateTask delegateTask) {System.out.println(CustomTaskListener.class.getName() + "--start");System.out.println("processInstanceId:" + delegateTask.getProcessInstanceId());System.out.println("getAssignee:" + delegateTask.getAssignee());System.out.println("getId:" + delegateTask.getId());System.out.println("getName:" + delegateTask.getName());System.out.println("getEventName:" + delegateTask.getEventName());System.out.println("getPriority:" + delegateTask.getPriority());System.out.println("getCaseDefinitionId:" + delegateTask.getCaseDefinitionId());System.out.println("getCaseExecutionId:" + delegateTask.getCaseExecutionId());System.out.println("getDueDate:" + delegateTask.getDueDate());System.out.println("getTaskDefinitionKey:" + delegateTask.getTaskDefinitionKey());System.out.println("getOwner:" + delegateTask.getOwner());System.out.println("getDescription:" + delegateTask.getDescription());System.out.println(CustomTaskListener.class.getName() + "--end");}
}

ExecutionListener

基本所有节点都可以配置Execution listener:

执行listener

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;public class CustomExecutionListener implements ExecutionListener {@Overridepublic void notify(DelegateExecution delegateExecution) throws Exception {System.out.println(CustomExecutionListener.class.getName() + "--start");System.out.println("getProcessInstanceId:" + delegateExecution.getProcessInstanceId());System.out.println("getProcessDefinitionId:" + delegateExecution.getProcessDefinitionId());System.out.println("getActivityInstanceId:" + delegateExecution.getActivityInstanceId());System.out.println("getCurrentActivityId:" + delegateExecution.getCurrentActivityId());System.out.println("getCurrentActivityName:" + delegateExecution.getCurrentActivityName());System.out.println("getProcessBusinessKey:" + delegateExecution.getProcessBusinessKey());System.out.println("getBusinessKey:" + delegateExecution.getBusinessKey());// 获取参数delegateExecution.getVariable("paramA");// 设置参数delegateExecution.setVariable("paramZ","paramZValue");// 可以获取ProcessEngine、RuntimeService来执行更多的操作RuntimeService runtimeService = delegateExecution.getProcessEngine().getRuntimeService();System.out.println(runtimeService);System.out.println(CustomExecutionListener.class.getName() + "--end");}
}

部署发起流程

部署工作流

部署成功

发起工作流

CustomExecutionListener开始节点

com.example.workflow.delegate.CustomExecutionListener--start
getProcessInstanceId:646f4f07-bf4c-11ee-a70e-00ffe7687986
getProcessDefinitionId:Process_1dnzxeh:3:830657fb-bf4a-11ee-b1f6-00ffe7687986
getActivityInstanceId:null
getCurrentActivityId:StartEvent_1
getCurrentActivityName:null
getProcessBusinessKey:listener-30
getBusinessKey:listener-30
org.camunda.bpm.engine.impl.RuntimeServiceImpl@1d380352
com.example.workflow.delegate.CustomExecutionListener--end

CustomExecutionListener

com.example.workflow.delegate.CustomExecutionListener--start
getProcessInstanceId:646f4f07-bf4c-11ee-a70e-00ffe7687986
getProcessDefinitionId:Process_1dnzxeh:3:830657fb-bf4a-11ee-b1f6-00ffe7687986
getActivityInstanceId:StartEvent_1:64747f30-bf4c-11ee-a70e-00ffe7687986
getCurrentActivityId:StartEvent_1
getCurrentActivityName:null
getProcessBusinessKey:listener-30
getBusinessKey:listener-30
org.camunda.bpm.engine.impl.RuntimeServiceImpl@1d380352
com.example.workflow.delegate.CustomExecutionListener--end

CustomJavaDelegate

com.example.workflow.delegate.CustomJavaDelegate--start
getProcessInstanceId:646f4f07-bf4c-11ee-a70e-00ffe7687986
getActivityInstanceId:Activity_1claer7:6475dec2-bf4c-11ee-a70e-00ffe7687986
getCurrentActivityId:Activity_1claer7
getCurrentActivityName:bzService
getProcessBusinessKey:listener-30
getProcessDefinitionId:Process_1dnzxeh:3:830657fb-bf4a-11ee-b1f6-00ffe7687986
getBusinessKey:listener-30
getEventName:null
3.14
haha
com.example.workflow.delegate.CustomJavaDelegate--end

CustomExecutionListener

com.example.workflow.delegate.CustomExecutionListener--start
getProcessInstanceId:646f4f07-bf4c-11ee-a70e-00ffe7687986
getProcessDefinitionId:Process_1dnzxeh:3:830657fb-bf4a-11ee-b1f6-00ffe7687986
getActivityInstanceId:Activity_1xjraoy:6476c927-bf4c-11ee-a70e-00ffe7687986
getCurrentActivityId:Activity_1xjraoy
getCurrentActivityName:userTask
getProcessBusinessKey:listener-30
getBusinessKey:listener-30
org.camunda.bpm.engine.impl.RuntimeServiceImpl@1d380352
com.example.workflow.delegate.CustomExecutionListener--end

CustomTaskListener用户节点

com.example.workflow.delegate.CustomTaskListener--start
processInstanceId:646f4f07-bf4c-11ee-a70e-00ffe7687986
getAssignee:null
getId:64771749-bf4c-11ee-a70e-00ffe7687986
getName:userTask
getEventName:create
getPriority:50
getCaseDefinitionId:null
getCaseExecutionId:null
getDueDate:null
getTaskDefinitionKey:Activity_1xjraoy
getOwner:null
getDescription:null
com.example.workflow.delegate.CustomTaskListener--end

Expression

Expression的优势在于不用继承特别的类,就可以调用相关方法。

注意:需要将类注入容器

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;/*** #{expressionDelegateExecution.exe(execution)}*/
@Component("expressionDelegateExecution")
public class ExpressionDelegateExecution {public void exe(DelegateExecution execution){String processInstanceId = execution.getProcessInstanceId();System.out.println("ExpressionDelegateExecution:" + processInstanceId);}
}

表达式配置

Delegate Expression

Delegate Expression可以看做是加强版的Expression。

它最大的一个特点是对于实现了:

  1. ExecutionListener
  2. JavaDelegate
  3. TaskListener

上面的类,Delegate Expression可以直接配置bean名称就可以,Camunda会自动调用其方法。

还是需要注入容器才行。

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;/*** #{delegateExpressionComponent}*/
@Component("delegateExpressionComponent")
public class DelegateExpressionComponent implements JavaDelegate {@Overridepublic void execute(DelegateExecution execution) throws Exception {System.out.println("DelegateExpressionComponent:" + execution.getProcessInstanceId());}
}

delegate表达式配置

流程图

<?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_13pugtj" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.19.0" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.20.0"><bpmn:process id="Process_1dnzxeh" name="service" isExecutable="true" camunda:historyTimeToLive="180"><bpmn:extensionElements><camunda:executionListener class="com.example.workflow.delegate.CustomExecutionListener" event="end" /><camunda:executionListener class="com.example.workflow.delegate.CustomExecutionListener" event="start" /></bpmn:extensionElements><bpmn:startEvent id="StartEvent_1"><bpmn:extensionElements><camunda:executionListener class="com.example.workflow.delegate.CustomExecutionListener" event="start" /></bpmn:extensionElements><bpmn:outgoing>Flow_0eji7wy</bpmn:outgoing></bpmn:startEvent><bpmn:serviceTask id="Activity_1claer7" name="bzService" camunda:class="com.example.workflow.delegate.CustomJavaDelegate"><bpmn:incoming>Flow_0eji7wy</bpmn:incoming><bpmn:outgoing>Flow_0c190eu</bpmn:outgoing></bpmn:serviceTask><bpmn:sequenceFlow id="Flow_0eji7wy" sourceRef="StartEvent_1" targetRef="Activity_1claer7" /><bpmn:sequenceFlow id="Flow_0c190eu" sourceRef="Activity_1claer7" targetRef="Activity_1xjraoy" /><bpmn:userTask id="Activity_1xjraoy" name="userTask"><bpmn:extensionElements><camunda:taskListener class="com.example.workflow.delegate.CustomTaskListener" event="create" id="Lid255" /><camunda:executionListener class="com.example.workflow.delegate.CustomExecutionListener" event="start" /></bpmn:extensionElements><bpmn:incoming>Flow_0c190eu</bpmn:incoming><bpmn:outgoing>Flow_0383lfn</bpmn:outgoing></bpmn:userTask><bpmn:endEvent id="Event_0aws3kb"><bpmn:extensionElements><camunda:executionListener class="com.example.workflow.delegate.CustomExecutionListener" event="start" /></bpmn:extensionElements><bpmn:incoming>Flow_0383lfn</bpmn:incoming></bpmn:endEvent><bpmn:sequenceFlow id="Flow_0383lfn" sourceRef="Activity_1xjraoy" targetRef="Event_0aws3kb" /></bpmn:process><bpmndi:BPMNDiagram id="BPMNDiagram_1"><bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1dnzxeh"><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_04xt5m2_di" bpmnElement="Activity_1claer7"><dc:Bounds x="280" y="77" width="100" height="80" /><bpmndi:BPMNLabel /></bpmndi:BPMNShape><bpmndi:BPMNShape id="Activity_1frv2ve_di" bpmnElement="Activity_1xjraoy"><dc:Bounds x="450" y="77" width="100" height="80" /><bpmndi:BPMNLabel /></bpmndi:BPMNShape><bpmndi:BPMNShape id="Event_0aws3kb_di" bpmnElement="Event_0aws3kb"><dc:Bounds x="602" y="99" width="36" height="36" /></bpmndi:BPMNShape><bpmndi:BPMNEdge id="Flow_0eji7wy_di" bpmnElement="Flow_0eji7wy"><di:waypoint x="215" y="117" /><di:waypoint x="280" y="117" /></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="Flow_0c190eu_di" bpmnElement="Flow_0c190eu"><di:waypoint x="380" y="117" /><di:waypoint x="450" y="117" /></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="Flow_0383lfn_di" bpmnElement="Flow_0383lfn"><di:waypoint x="550" y="117" /><di:waypoint x="602" y="117" /></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</bpmn:definitions>

这篇关于Camunda中强大的监听服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/661473

相关文章

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Linux上设置Ollama服务配置(常用环境变量)

《Linux上设置Ollama服务配置(常用环境变量)》本文主要介绍了Linux上设置Ollama服务配置(常用环境变量),Ollama提供了多种环境变量供配置,如调试模式、模型目录等,下面就来介绍一... 目录在 linux 上设置环境变量配置 OllamPOgxSRJfa手动安装安装特定版本查看日志在

SpringCloud之LoadBalancer负载均衡服务调用过程

《SpringCloud之LoadBalancer负载均衡服务调用过程》:本文主要介绍SpringCloud之LoadBalancer负载均衡服务调用过程,具有很好的参考价值,希望对大家有所帮助,... 目录前言一、LoadBalancer是什么?二、使用步骤1、启动consul2、客户端加入依赖3、以服务

Flutter监听当前页面可见与隐藏状态的代码详解

《Flutter监听当前页面可见与隐藏状态的代码详解》文章介绍了如何在Flutter中使用路由观察者来监听应用进入前台或后台状态以及页面的显示和隐藏,并通过代码示例讲解的非常详细,需要的朋友可以参考下... flutter 可以监听 app 进入前台还是后台状态,也可以监听当http://www.cppcn

Nginx配置系统服务&设置环境变量方式

《Nginx配置系统服务&设置环境变量方式》本文介绍了如何将Nginx配置为系统服务并设置环境变量,以便更方便地对Nginx进行操作,通过配置系统服务,可以使用系统命令来启动、停止或重新加载Nginx... 目录1.Nginx操作问题2.配置系统服android务3.设置环境变量总结1.Nginx操作问题

spring @EventListener 事件与监听的示例详解

《spring@EventListener事件与监听的示例详解》本文介绍了自定义Spring事件和监听器的方法,包括如何发布事件、监听事件以及如何处理异步事件,通过示例代码和日志,展示了事件的顺序... 目录1、自定义Application Event2、自定义监听3、测试4、源代码5、其他5.1 顺序执行