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

文章目录

  • 一、任务监听器
    • 事件类型
    • 配置方式(选)
    • 代码实现
      • xml文件
      • 创建监听器
        • class方式
        • expression方式
        • delegateExpression
      • 测试流程
        • 部署流程
        • 启动流程
        • 完成任务


一、任务监听器

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

事件类型

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

配置方式(选)

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

代码实现

SpringBoot整合Activiti7——任务监听器(七)_第1张图片

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: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>任务节点1documentation>
      <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>任务节点2documentation>
    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方式
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方式
@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
@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());
    }
}

测试流程

@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分配的事件,然后再是创建任务事件(因为有多个监听器顺序不一致)。
SpringBoot整合Activiti7——任务监听器(七)_第2张图片

完成任务

运行 completeTask,首先完成的是节点1任务,可以看到监听器监听到任务完成的事件。
SpringBoot整合Activiti7——任务监听器(七)_第3张图片

你可能感兴趣的:(Activiti,java,spring,boot,流程图)