当前位置: 首页 > news >正文

【Flowable】任务监听器(五)

前言

之前有需要使用到Flowable,鉴于网上的资料不是很多也不是很全也是捣鼓了半天,因此争取能在这里简单分享一下经验,帮助有需要的朋友,也非常欢迎大家指出不足的地方。

一、监听器

在Flowable中,我们可以使用监听器来完成很多流程业务,比如指定任务处理人、任务通知提醒等操作,在这里我们来简单尝试一下监听器的使用,我们在上篇文章中使用的请假流程中来创建监听器。

在这里插入图片描述
在这里插入图片描述
如上图所示:

  • 事件
    • create:任务创建的时候触发。
    • assignment:在分配处理人的时候触发。
    • complete:处理完成之后触发。
    • all:所有的行为都会触发

我们创建一下监听器:指定一下事件类型这里用create,其中org.flowable.TestTaskListener为自定义的一个java类
在这里插入图片描述
这里也提供了更新之后的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef" exporter="Flowable Open Source Modeler" exporterVersion="6.7.2"><process id="MyHoliday" name="MyHoliday" isExecutable="true"><startEvent id="startEvent1" name="开始" flowable:formFieldValidation="true"></startEvent><userTask id="sid-320C4AED-D9F4-4C53-B2B6-D4ACCBA155F5" name="直线审批" flowable:formFieldValidation="true"><extensionElements><flowable:taskListener event="create" class="org.flowable.TestTaskListener"></flowable:taskListener></extensionElements></userTask><sequenceFlow id="sid-422764E6-61F4-454A-BF08-9CF973E1E67D" sourceRef="startEvent1" targetRef="sid-320C4AED-D9F4-4C53-B2B6-D4ACCBA155F5"></sequenceFlow><userTask id="sid-873E297E-9E6B-4F4E-AF6A-2E085F1806B1" name="部门审批" flowable:formFieldValidation="true"><extensionElements><flowable:taskListener event="create" class="org.flowable.TestTaskListener"></flowable:taskListener></extensionElements></userTask><sequenceFlow id="sid-A0869337-10D3-43BC-90C1-036C9719308E" sourceRef="sid-320C4AED-D9F4-4C53-B2B6-D4ACCBA155F5" targetRef="sid-873E297E-9E6B-4F4E-AF6A-2E085F1806B1"></sequenceFlow><endEvent id="sid-CAB64925-F653-4167-8E12-ED956B723D2E" name="结束"></endEvent><sequenceFlow id="sid-91D96B3A-3408-4504-A23D-8F0C2AFE19A7" sourceRef="sid-873E297E-9E6B-4F4E-AF6A-2E085F1806B1" targetRef="sid-CAB64925-F653-4167-8E12-ED956B723D2E"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_MyHoliday"><bpmndi:BPMNPlane bpmnElement="MyHoliday" id="BPMNPlane_MyHoliday"><bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1"><omgdc:Bounds height="30.0" width="30.0" x="89.99999731779107" y="149.99999552965178"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="sid-320C4AED-D9F4-4C53-B2B6-D4ACCBA155F5" id="BPMNShape_sid-320C4AED-D9F4-4C53-B2B6-D4ACCBA155F5"><omgdc:Bounds height="80.0" width="100.00000000000003" x="164.99999731779107" y="124.99999552965178"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="sid-873E297E-9E6B-4F4E-AF6A-2E085F1806B1" id="BPMNShape_sid-873E297E-9E6B-4F4E-AF6A-2E085F1806B1"><omgdc:Bounds height="80.00000000000001" width="100.0" x="299.99999105930357" y="119.99999642372141"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="sid-CAB64925-F653-4167-8E12-ED956B723D2E" id="BPMNShape_sid-CAB64925-F653-4167-8E12-ED956B723D2E"><omgdc:Bounds height="28.0" width="28.0" x="464.9999861419205" y="145.9999920725826"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="sid-91D96B3A-3408-4504-A23D-8F0C2AFE19A7" id="BPMNEdge_sid-91D96B3A-3408-4504-A23D-8F0C2AFE19A7" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.00000000000001" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0"><omgdi:waypoint x="399.94999034668683" y="159.99999473723344"></omgdi:waypoint><omgdi:waypoint x="464.99998600932435" y="159.99999254311274"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="sid-A0869337-10D3-43BC-90C1-036C9719308E" id="BPMNEdge_sid-A0869337-10D3-43BC-90C1-036C9719308E" flowable:sourceDockerX="50.000000000000014" flowable:sourceDockerY="40.0" flowable:targetDockerX="50.0" flowable:targetDockerY="40.00000000000001"><omgdi:waypoint x="264.9499973177909" y="163.1481439230865"></omgdi:waypoint><omgdi:waypoint x="299.9999910593035" y="161.8499961786801"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="sid-422764E6-61F4-454A-BF08-9CF973E1E67D" id="BPMNEdge_sid-422764E6-61F4-454A-BF08-9CF973E1E67D" flowable:sourceDockerX="15.0" flowable:sourceDockerY="15.0" flowable:targetDockerX="50.000000000000014" flowable:targetDockerY="40.0"><omgdi:waypoint x="119.94999580774865" y="164.99999552965178"></omgdi:waypoint><omgdi:waypoint x="164.9999973177828" y="164.99999552965178"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>

将此xml文件放置项目的资源目录下

二、创建监听类

package org.flowable;import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;/*** Author:hyy* Date:2023/9/15* Description:*/
public class TestTaskListener implements TaskListener {@Overridepublic void notify(DelegateTask delegateTask) {System.err.println("============= 监听器执行 ================= ");if("create".equals(delegateTask.getEventName()) ){System.out.println("你的业务操作....");}}
}

创建TestTaskListener实现TaskListener,我们发现这个方法的关键信息都是从DelegateTask中获取,name我们来看一下这个类

public interface DelegateTask extends VariableScope {//任务IDString getId();//任务名称String getName();void setName(String name);//描述String getDescription();void setDescription(String description);//优先级int getPriority();void setPriority(int priority);//实例IDString getProcessInstanceId();//流程定义IDString getProcessDefinitionId();Date getCreateTime();//任务主键String getTaskDefinitionKey();//是否终断boolean isSuspended();//租户IDString getTenantId();//表单keyString getFormKey();void setFormKey(String formKey);//事件String getEventName();//....... 
}

三、重新部署流程

在上一篇文章示例中,我们在重新部署一下这个流程

   @Testpublic void testDeploy() {//1.获取ProcessEngine 对象ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();//2.获取RepositoryServiceRepositoryService repositoryService = processEngine.getRepositoryService();//3.完成流程部署操作Deployment deploy = repositoryService.createDeployment().addClasspathResource("MyHoliday.bpmn20.xml") //关联要部署的流程名称.name("请假流程").deploy(); //部署流程System.out.println("id: " + deploy.getId()); System.out.println("name: " + deploy.getName());System.out.println("key: " + deploy.getKey());}

这里去ACT_RE_PROCDEF中找到部署ID为代码打印的ID数据我这里是35001,拿到他的ID_字段MyHoliday:2:35004去启动流程
在这里插入图片描述

四、启动流程

@Testpublic void startTest() {ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();Map<String, Object> variables = new HashMap<>();variables.put("firstAssigne", "张三");variables.put("deptAssigne", "李四");ProcessInstance processInstance = runtimeService.startProcessInstanceById("MyHoliday:2:35004", variables);System.out.println("processInstance.getProcessInstanceId() = " + processInstance.getProcessInstanceId());}

发现控制台打印如下日志,表示监听器执行完成
在这里插入图片描述

http://www.lryc.cn/news/167462.html

相关文章:

  • spring-kafka中ContainerProperties.AckMode详解
  • 【rpc】Dubbo和Zookeeper结合使用,它们的作用与联系(通俗易懂,一文理解)
  • ChatGPT的未来
  • Pytorch模型转ONNX部署
  • k8s优雅停服
  • 面试题五:computed的使用
  • 完美的分布式监控系统 Prometheus与优雅的开源可视化平台 Grafana
  • 黑马JVM总结(九)
  • 如何使用 RunwayML 进行创意 AI 创作
  • 【css】能被4整除 css :class,判断一个数能否被另外一个数整除,余数
  • ChatGPT与日本首相交流核废水事件-精准Prompt...
  • 关于 firefox 不能访问 http 的解决
  • 68、Spring Data JPA 的 方法名关键字查询
  • Brother CNC联网数采集和远程控制
  • Jenkins 编译 Maven 项目提示错误 version 17
  • 数据结构——排序算法——堆排序
  • 【Spring事务底层实现原理】
  • docker快速安装redis,mysql,minio,nacos等常用软件【持续更新】
  • SCRUM产品负责人(CSPO)认证培训课程
  • python连接mysql数据库的练习
  • 扩散模型在图像生成中的应用:从真实样例到逼真图像的奇妙转变
  • Windows 打包 Docker 提示环境错误: no DOCKER_HOST environment variable
  • 2023.9.8 基于传输层协议 UDP 和 TCP 编写网络通信程序
  • 单例模式,适用于对象唯一的情景(设计模式与开发实践 P4)
  • C语言实现三子棋游戏(详解)
  • javaee之黑马乐优商城3
  • Pytorch intermediate(二) ResNet
  • 【2023集创赛】加速科技杯作品:高光响应的二硫化铼光电探测器
  • 编写postcss插件,全局css文件px转vw
  • 精品SpringCloud的B2C模式在线学习网微服务分布式