SpringBoot整合Activiti7——任务监听器(七)

文章目录


一、任务监听器

任务监听器可以在任务创建、任务分配、任务完成、任务删除发生时触发,从而执行相应的逻辑。

事件类型

  1. create:创建事件
  2. assignment:指派事件
  3. complete:完成事件
  4. delete:删除事件

配置方式(选)

  1. class:直接配置class全名
  2. expression:spring bean容器对应名字与方法名
  3. delegateExpression:spring bean容器对应名字

代码实现

xml文件

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:activiti="http://activiti.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.activiti.org/processdef">
  <process id="task-listener" name="任务监听器" isExecutable="true">
    <documentation>测试任务监听器</documentation>
    <startEvent id="sid-30244641-2a1c-43e5-af5b-e77db43488bf" name="开始">
      <documentation>开始了</documentation>
    </startEvent>
    <userTask id="sid-9e62413f-e04f-4c81-8d0c-e73f17e125ec" name="节点1" activiti:assignee="${applyUserId}" activiti:candidateUsers="${candidateUsers}">
      <documentation>任务节点1</documentation>
      <extensionElements>
        <activiti:taskListener event="create" class="com.lmw.test.activiti.listener.task.MyTaskListener1"/>
        <activiti:taskListener event="assignment" expression="${myTaskListener2.notify(task)}"/>
        <activiti:taskListener event="complete" delegateExpression="${myTaskListener3}"/>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-1af5e647-b03c-4b12-807d-4171dfdf7ae9" sourceRef="sid-30244641-2a1c-43e5-af5b-e77db43488bf" targetRef="sid-9e62413f-e04f-4c81-8d0c-e73f17e125ec" name="顺序流1">
      <documentation>顺序流1了</documentation>
    </sequenceFlow>
    <userTask id="sid-d903cb09-56c2-4cfe-bd05-5ba0699539d0" name="节点2">
      <documentation>任务节点2</documentation>
    </userTask>
    <sequenceFlow id="sid-300ac02e-dc56-4988-bdd4-fd94a5bb71f7" sourceRef="sid-9e62413f-e04f-4c81-8d0c-e73f17e125ec" targetRef="sid-d903cb09-56c2-4cfe-bd05-5ba0699539d0" name="顺序流2">
      <documentation>顺序流2了</documentation>
    </sequenceFlow>
    <endEvent id="sid-ace3a923-023c-4226-875c-2a0a30cc1c50" name="结束">
      <documentation>结束了</documentation>
    </endEvent>
    <sequenceFlow id="sid-dbf73610-a8b4-4149-828e-4f5bc252c80d" sourceRef="sid-d903cb09-56c2-4cfe-bd05-5ba0699539d0" targetRef="sid-ace3a923-023c-4226-875c-2a0a30cc1c50" name="顺序流3">
      <documentation>顺序流3了</documentation>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_execution-listener">
    <bpmndi:BPMNPlane bpmnElement="task-listener" id="BPMNPlane_execution-listener">
      <bpmndi:BPMNShape id="shape-d4dd6424-1316-4c10-a8f9-f3c501cd4073" bpmnElement="sid-30244641-2a1c-43e5-af5b-e77db43488bf">
        <omgdc:Bounds x="-442.5" y="-6.75" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-bcd8743b-6857-42d1-bc71-bd3bb6eed795" bpmnElement="sid-9e62413f-e04f-4c81-8d0c-e73f17e125ec">
        <omgdc:Bounds x="-388.0" y="-31.75" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-d562b253-050f-4617-bbad-2e16950c15e4" bpmnElement="sid-1af5e647-b03c-4b12-807d-4171dfdf7ae9">
        <omgdi:waypoint x="-412.5" y="8.25"/>
        <omgdi:waypoint x="-388.0" y="8.25"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-cd7c00d6-f5f0-4afe-867c-6f576efc286d" bpmnElement="sid-d903cb09-56c2-4cfe-bd05-5ba0699539d0">
        <omgdc:Bounds x="-259.0" y="-31.75" width="100.0" height="80.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-44a5c4d2-2ab7-48f1-adc7-c7a7a099800c" bpmnElement="sid-300ac02e-dc56-4988-bdd4-fd94a5bb71f7">
        <omgdi:waypoint x="-288.0" y="8.25"/>
        <omgdi:waypoint x="-259.0" y="8.25"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-05cc9b19-8019-471d-b31c-bb41c42e3529" bpmnElement="sid-ace3a923-023c-4226-875c-2a0a30cc1c50">
        <omgdc:Bounds x="-123.0" y="-6.75" width="30.0" height="30.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-923723d0-33e3-4a62-bed9-bb16d4c9b175" bpmnElement="sid-dbf73610-a8b4-4149-828e-4f5bc252c80d">
        <omgdi:waypoint x="-159.0" y="8.25"/>
        <omgdi:waypoint x="-123.0" y="8.25"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

创建监听器

class方式
java 复制代码
public class MyTaskListener1 implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        System.out.println("========================MyTaskListener1========================");

        System.out.println("delegateTask.getProcessInstanceId() = " + delegateTask.getProcessInstanceId());
        System.out.println("delegateTask.getTaskDefinitionKey() = " + delegateTask.getTaskDefinitionKey());
        System.out.println("delegateTask.getEventName() = " + delegateTask.getEventName());
        System.out.println("delegateTask.getName() = " + delegateTask.getName());
        System.out.println("delegateTask.getAssignee() = " + delegateTask.getAssignee());
        System.out.println("delegateTask.getVariables() = " + delegateTask.getVariables());
    }
}
expression方式
java 复制代码
@Component
public class MyTaskListener2 {
    public void notify(DelegateTask delegateTask) {
        System.out.println("========================MyTaskListener1========================");

        System.out.println("delegateTask.getProcessInstanceId() = " + delegateTask.getProcessInstanceId());
        System.out.println("delegateTask.getTaskDefinitionKey() = " + delegateTask.getTaskDefinitionKey());
        System.out.println("delegateTask.getEventName() = " + delegateTask.getEventName());
        System.out.println("delegateTask.getName() = " + delegateTask.getName());
        System.out.println("delegateTask.getAssignee() = " + delegateTask.getAssignee());
        System.out.println("delegateTask.getVariables() = " + delegateTask.getVariables());
    }
}
delegateExpression
java 复制代码
@Component
public class MyTaskListener3 implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        System.out.println("========================MyTaskListener1========================");

        System.out.println("delegateTask.getProcessInstanceId() = " + delegateTask.getProcessInstanceId());
        System.out.println("delegateTask.getTaskDefinitionKey() = " + delegateTask.getTaskDefinitionKey());
        System.out.println("delegateTask.getEventName() = " + delegateTask.getEventName());
        System.out.println("delegateTask.getName() = " + delegateTask.getName());
        System.out.println("delegateTask.getAssignee() = " + delegateTask.getAssignee());
        System.out.println("delegateTask.getVariables() = " + delegateTask.getVariables());
    }
}

测试流程

java 复制代码
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestActivityTaskListener {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    private static final String PROCESS_INSTANCE_ID = "5b10eb17-3b4d-11ee-982e-18c04dcd4aee";

    @Test
    public void deployProcess() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("processes/task-listener.bpmn20.xml")
                .deploy();
        System.out.println("deploy = " + deploy);
    }

    @Test
    public void startProcess() {
        Map<String, Object> variables = new HashMap<>();
        variables.put("applyUserId", "user123456");
        variables.put("candidateUsers", CollectionUtil.newArrayList("zhangsan", "lisi", "wangwu"));

        String processDefinitionKey = "task-listener";
        String businessKey = processDefinitionKey + ":" + "100001";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
        System.out.println("processInstance = " + processInstance);

        // 输出当前任务列表
        this.printTaskList(processInstance.getId());
    }

    @Test
    public void completeTask() {
        // 查询任务
        Task task = taskService.createTaskQuery().processInstanceId(PROCESS_INSTANCE_ID).singleResult();
        taskService.complete(task.getId());
    }

    private void printTaskList(String processInstanceId) {
        // 输出当前任务列表
        taskService.createTaskQuery().processInstanceId(processInstanceId).orderByTaskCreateTime().asc().list().forEach(k -> {
            System.out.println("===================任务列表===================");
            System.out.println("任务ID = " + k.getId());
            System.out.println("任务名称 = " + k.getName());
            System.out.println("任务负责人 = " + k.getAssignee());
            System.out.println("任务创建时间 = " + k.getCreateTime());

            System.out.println("===================身份列表===================");
            // 输出用户身份关系列表
            taskService.getIdentityLinksForTask(k.getId()).forEach(link -> {
                System.out.println("link.getType() = " + link.getType());
                System.out.println("link.getUserId() = " + link.getUserId());
                System.out.println("link.getGroupId() = " + link.getGroupId());
                System.out.println("link.getTaskId() = " + link.getTaskId());
            });
        });
    }

}
部署流程

运行 deployProcess

启动流程

运行 startProcess,可以看到首先监听到任务节点1分配的事件,然后再是创建任务事件(因为有多个监听器顺序不一致)。

完成任务

运行 completeTask,首先完成的是节点1任务,可以看到监听器监听到任务完成的事件。

相关推荐
Redamancy_Xun2 分钟前
开源软件兼容性可信量化分析
java·开发语言·程序人生·网络安全·测试用例·可信计算技术
IDRSolutions_CN9 分钟前
(教程)用 Java 从 PDF 中提取嵌入的文件
java·经验分享·pdf·软件工程·团队开发
海波东12 分钟前
单例模式懒汉式、饿汉式(线程安全)
java·安全·单例模式
lwprain24 分钟前
解决tomcat双击startup.bat乱码的几种方法
java·tomcat
小汤猿人类1 小时前
nacos-gateway动态路由
java·前端·gateway
GraduationDesign1 小时前
基于SpringBoot的在线文档管理系统的设计与实现
java·spring boot·后端
TANGLONG2221 小时前
【初阶数据结构与算法】八大排序之非递归系列( 快排(使用栈或队列实现)、归并排序)
java·c语言·数据结构·c++·算法·蓝桥杯·排序算法
言之。1 小时前
【Java】面试题 并发安全 (1)
java·开发语言
m0_748234521 小时前
2025最新版Java面试八股文大全
java·开发语言·面试
van叶~1 小时前
仓颉语言实战——2.名字、作用域、变量、修饰符
android·java·javascript·仓颉