【弄nèng - Activiti6】Activiti6入门篇(二十三)—— 监听器

文章目录

    • 1. 任务监听器
      • 1.1 简介
      • 1.2 流程设计
    • 2. 流程监听器
      • 2.1 简介
      • 2.2 流程设计
    • 3. 测试
      • 3.1 流程图
      • 3.2 测试类
      • 3.3 运行
        • 3.3.1 启动任务
    • 源码地址
    • 项目推荐

Activiti监听器分为任务监听器和流程监听器

*后台服务基于Springboot2 + Activiti6,整合文章请参考:https://blog.csdn.net/yy756127197/article/details/101211510 不需要流程设计器就排除3,4步骤 *

1. 任务监听器

1.1 简介

任务监听器实现TaskListener 。
监听器触发时机有4种,包括人物创建事件(create),指定任务代理人事件(assignment),任务完成事件(complete),删除事件(delete)。assignment是在任务创建阶段,优先级大于create。
监听器的设置方式有三种,包括class,expression,delegateExpression。
expression 需要实现Serializable.
delegateExpression 需要实现Serializable, TaskListener

1.2 流程设计

节点任务监听器设置
使用监听器添加任务处理人
【弄nèng - Activiti6】Activiti6入门篇(二十三)—— 监听器_第1张图片
【弄nèng - Activiti6】Activiti6入门篇(二十三)—— 监听器_第2张图片

任务监听器内容:

import cn.hutool.json.JSONUtil;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import java.util.Map;

/**
 *个人任务分配监听器
 */
public class UserTaskListener implements TaskListener {

	private static final long serialVersionUID = 1L;

	public void notify(DelegateTask delegateTask) {
		// 指定办理人
		delegateTask.setAssignee("zhangsan");
		// 指定候选人
//		delegateTask.addCandidateUser("张三");
//		delegateTask.addCandidateUser("李四");
//		delegateTask.addCandidateUser("王五");

		String eventName = delegateTask.getEventName();
		// 获取任务变量
		Map<String, Object> variableMap = delegateTask.getVariables();
		System.out.println(JSONUtil.toJsonStr(variableMap));

		if ("create".endsWith(eventName)) {
			System.out.println("create=========");
		} else if ("assignment".endsWith(eventName)) {
			System.out.println("assignment========");
		} else if ("complete".endsWith(eventName)) {
			System.out.println("complete===========");
		} else if ("delete".endsWith(eventName)) {
			System.out.println("delete=============");
		}
	}

}

2. 流程监听器

2.1 简介

流程监听器实现ExecutionListener。
监听器触发时机有4种,包括任务开始事件(start),任务结束事件(end),连线事件(take)
监听器的设置方式有三种,包括class,expression,delegateExpression。
expression 需要实现Serializable.
delegateExpression 需要实现Serializable, TaskListener

2.2 流程设计

节点流程监听器设置
【弄nèng - Activiti6】Activiti6入门篇(二十三)—— 监听器_第3张图片
【弄nèng - Activiti6】Activiti6入门篇(二十三)—— 监听器_第4张图片
连线流程监听器
【弄nèng - Activiti6】Activiti6入门篇(二十三)—— 监听器_第5张图片
流程监听器类

import cn.hutool.json.JSONUtil;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.Map;

/**
 * @author 司马缸砸缸了
 * @date 2019/9/16 10:47
 * @description 监听器
 */
@Component
public class UserExecutionListener implements Serializable, ExecutionListener {

    // ExecutionListener类的实现
    public void notify(DelegateExecution execution) {
        String eventName = execution.getEventName();
        //start
        if ("start".equals(eventName)) {
            System.out.println("start=========");
        } else if ("end".equals(eventName)) {
            System.out.println("end=========");
        } else if ("take".equals(eventName)) {
            //做一些业务处理,例如请假单拒绝后修改业务状态
            System.out.println("take=========BusinessKey:"+ execution.getProcessInstanceBusinessKey());
        }
    }
}

3. 测试

3.1 流程图

流程图
【弄nèng - Activiti6】Activiti6入门篇(二十三)—— 监听器_第6张图片

流程文件bpmn


<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="listener" name="listener" isExecutable="true">
    <documentation>listenerdocumentation>
    <startEvent id="startEvent1">startEvent>
    <userTask id="sid-93A78385-432A-4958-830F-C44CA2EC3A01" name="用户">
      <extensionElements>
        <activiti:executionListener event="start" class="com.it.cloud.activiti.listener.UserExecutionListener">activiti:executionListener>
        <activiti:executionListener event="end" class="com.it.cloud.activiti.listener.UserExecutionListener">activiti:executionListener>
        <activiti:taskListener event="create" class="com.it.cloud.activiti.listener.UserTaskListener">activiti:taskListener>
      extensionElements>
    userTask>
    <endEvent id="sid-C0126D8E-8D4A-4CFA-9DB6-FC7FE780F014">endEvent>
    <sequenceFlow id="sid-8114500F-583B-44EE-87B0-8714805778CF" sourceRef="sid-93A78385-432A-4958-830F-C44CA2EC3A01" targetRef="sid-C0126D8E-8D4A-4CFA-9DB6-FC7FE780F014">sequenceFlow>
    <sequenceFlow id="sid-FB699F1B-FC7E-460D-8B1D-E7A5A669A887" sourceRef="startEvent1" targetRef="sid-93A78385-432A-4958-830F-C44CA2EC3A01">
      <extensionElements>
        <activiti:executionListener event="take" class="com.it.cloud.activiti.listener.UserExecutionListener">activiti:executionListener>
      extensionElements>
    sequenceFlow>
  process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_listener">
    <bpmndi:BPMNPlane bpmnElement="listener" id="BPMNPlane_listener">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="105.0" y="160.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-93A78385-432A-4958-830F-C44CA2EC3A01" id="BPMNShape_sid-93A78385-432A-4958-830F-C44CA2EC3A01">
        <omgdc:Bounds height="80.0" width="100.0" x="225.0" y="135.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-C0126D8E-8D4A-4CFA-9DB6-FC7FE780F014" id="BPMNShape_sid-C0126D8E-8D4A-4CFA-9DB6-FC7FE780F014">
        <omgdc:Bounds height="28.0" width="28.0" x="390.0" y="161.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-8114500F-583B-44EE-87B0-8714805778CF" id="BPMNEdge_sid-8114500F-583B-44EE-87B0-8714805778CF">
        <omgdi:waypoint x="325.0" y="175.0">omgdi:waypoint>
        <omgdi:waypoint x="390.0" y="175.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-FB699F1B-FC7E-460D-8B1D-E7A5A669A887" id="BPMNEdge_sid-FB699F1B-FC7E-460D-8B1D-E7A5A669A887">
        <omgdi:waypoint x="135.0" y="175.0">omgdi:waypoint>
        <omgdi:waypoint x="225.0" y="175.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
definitions>

3.2 测试类

ListenerTest.java

import com.it.cloud.modules.activiti.service.IActReModelService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ListenerTest {

    @Autowired
    private IActReModelService actReModelService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment() // 创建部署
                .addClasspathResource("diagrams/listener.bpmn20.xml") // 加载流程资源文件
                .name("listener流程") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void start() {
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("listener"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 查看任务
     */
    @Test
    public void queryTask() {
        List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
                .taskAssignee("zhangsan") // 指定某个人
                .list();
        for (Task task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }
    }

    /**
     * 完成任务
     */
    @Test
    public void completeTask() {
        taskService.complete("85005");
    }

}

3.3 运行

运行deploy()

3.3.1 启动任务

运行start()

效果:
【弄nèng - Activiti6】Activiti6入门篇(二十三)—— 监听器_第7张图片

数据表ACT_RU_TASK
在这里插入图片描述


源码地址

IT-CLOUD-ACTIVITI6
开源项目,持续更新中,喜欢请 Star~

项目推荐

IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
开源项目,持续更新中,喜欢请 Star~

你可能感兴趣的:(Activiti6)