【Flowable】任务监听器(五)

前言

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

一、监听器

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

在这里插入图片描述
【Flowable】任务监听器(五)_第1张图片
如上图所示:

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

我们创建一下监听器:指定一下事件类型这里用create,其中org.flowable.TestTaskListener为自定义的一个java类
【Flowable】任务监听器(五)_第2张图片
这里也提供了更新之后的xml文件:


<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 {
    @Override
    public void notify(DelegateTask delegateTask) {
        System.err.println("============= 监听器执行 ================= ");

        if("create".equals(delegateTask.getEventName()) ){
            System.out.println("你的业务操作....");
        }
    }
}

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

public interface DelegateTask extends VariableScope {

    //任务ID
    String getId();

  	//任务名称
    String getName();

   	
    void setName(String name);

  	//描述
    String getDescription();


    void setDescription(String description);

   	//优先级
    int getPriority();

    
    void setPriority(int priority);

    //实例ID
    String getProcessInstanceId();


    //流程定义ID
    String getProcessDefinitionId();

    
    Date getCreateTime();

	//任务主键
    String getTaskDefinitionKey();
	
  	//是否终断
    boolean isSuspended();

    //租户ID
    String getTenantId();

	//表单key
    String getFormKey();

   
    void setFormKey(String formKey);

   	//事件
    String getEventName();
	//....... 
}

三、重新部署流程

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

   @Test
    public void testDeploy() {
        //1.获取ProcessEngine 对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RepositoryService
        RepositoryService 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去启动流程
在这里插入图片描述

四、启动流程

@Test
    public 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());
    }

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

你可能感兴趣的:(Flowable,java,flowable,监听器,TaskListener,消息通知)