工作流之监听器介绍及配置实现

目录

类型

执行监听器

用途

配置

自定义实现

任务监听器

配置

自定义实现

事件监听器(全局监听器)

用途

配置

自定义实现

运行总结

流程图

运行日志

分析结果


类型

  • 流程监听器
    • 执行监听器
    • 事件监听器(全局监听器)
  • 连线监听器
    • 执行监听器
  • 环节监听器
    • 执行监听器
    • 任务监听器(只有用户活动类型有)

执行监听器

用途

  • 动态分配节点处理人
  • 通过前一个节点设置的变量,在运行到下一个节点时设置对应的处理人;
  • 当流程运行到某个节点时,发送邮件或短信给待办用户;
  • 任务流转到当前的节点的时候,需要监控当前任务节点的一些信息或者其他的业务信息,统计流程处理时长,是否超时等;
  • 业务层面数据处理。

配置

执行监听器则监听流程的所有节点和连线。主要有start、end、take事件。其中节点有start、end两种事件,而连线则有take事件

工作流之监听器介绍及配置实现_第1张图片

自定义实现

自定义执行监听器需要实现ExecutionListener接口,并且实现notify方法。

@Slf4j
public class MyExecutionListener implements ExecutionListener{


    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        log.info("============executionListener start============");
        String eventName = delegateExecution.getEventName();
        String currentActivitiId = delegateExecution.getCurrentActivityId();
        log.info("事件名称:{}", eventName);
        log.info("ActivitiId:{}", currentActivitiId);
        log.info("============executionListener  end============");
    }
}

任务监听器

配置

用于监听任务,任务监听器生命周期对应四个事件,分别是assignment、create、complete、delete。

当流程引擎触发这四种事件类型时,对应的任务监听器会捕获其事件类型,再按照监听器的处理逻辑进行处理。

工作流之监听器介绍及配置实现_第2张图片

自定义实现

自定义任务监听器需要实现TaskListener接口,并且实现notify方法。

@Slf4j
public class MyTaskListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("============TaskListener start============");
        String taskDefinitionKey = delegateTask.getTaskDefinitionKey();
        String eventName = delegateTask.getEventName();
        log.info("事件名称:{}", eventName);
        log.info("taskDefinitionKey:{}", taskDefinitionKey);
        log.info("============TaskListener end============");
    }
}

事件监听器(全局监听器)

用途

监控该流程的启动和结束。

 

配置

可配置各类事件

工作流之监听器介绍及配置实现_第3张图片

自定义实现

  • 在activiti中已经有BaseEntityEventListener类来定义流程相关的事件。可以继承BaseEntityEventListener,重写对应方法onCreate等来自定义事件。
@Override
  public final void onEvent(ActivitiEvent event) {
    if (isValidEvent(event)) {
      // Check if this event
      if (event.getType() == ActivitiEventType.ENTITY_CREATED) {
        onCreate(event);
      } else if (event.getType() == ActivitiEventType.ENTITY_INITIALIZED) {
        onInitialized(event);
      } else if (event.getType() == ActivitiEventType.ENTITY_DELETED) {
        onDelete(event);
      } else if (event.getType() == ActivitiEventType.ENTITY_UPDATED) {
        onUpdate(event);
      } else {
        // Entity-specific event
        onEntityEvent(event);
      }
    }
  }
  • 如上述事件不满足,则可自定义任务监听器来实现ActivitiEventListener接口,并且实现onEvent方法来自定义事件。
@Slf4j
public class MyActivitiEventListener implements ActivitiEventListener {

    //当时间被执行时调用该方法
    @Override
    public void onEvent(ActivitiEvent activitiEvent) {
        log.info("============ActivitiEventListener start============");
        log.info("事件类型:{}", activitiEvent.getType());
        log.info("ExecutionId:{}", activitiEvent.getExecutionId());
        log.info("ProcessInstanceId:{}", activitiEvent.getProcessInstanceId());
        log.info("============ActivitiEventListener  end============");
    }

    //这个是onEvent执行失败之后进行的处理
    // 如果是true则返回onEvent执行失败的异常
    // 如果是false则忽略这个异常
    @Override
    public boolean isFailOnException() {
        return false;
    }
}

运行总结

流程图

其中1配置了全局事件监听器,如上图

2配置执行监听器,事件为take

3配置执行监听器,事件为start、end

3配置任务监听器,事件为assignment、create、complete、delete

工作流之监听器介绍及配置实现_第4张图片

运行日志

[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.391:[ INFO] [214320] [] [XNIO-2 task-2:2431126] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:19] ============ActivitiEventListener start============
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.481:[ INFO] [214320] [] [XNIO-2 task-2:2431216] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:20] 事件类型:ACTIVITY_STARTED
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.481:[ INFO] [214320] [] [XNIO-2 task-2:2431216] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:21] ExecutionId:10021
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.481:[ INFO] [214320] [] [XNIO-2 task-2:2431216] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:22] ProcessInstanceId:10021
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.482:[ INFO] [214320] [] [XNIO-2 task-2:2431217] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:23] ============ActivitiEventListener  end============


[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.486:[ INFO] [214320] [] [XNIO-2 task-2:2431221] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:18] ============executionListener start============
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.486:[ INFO] [214320] [] [XNIO-2 task-2:2431221] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:21] 事件名称:take
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.487:[ INFO] [214320] [] [XNIO-2 task-2:2431222] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:22] ActivitiId:sid-397FFB45-7110-4874-9E72-F4EBA8F284C8
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.487:[ INFO] [214320] [] [XNIO-2 task-2:2431222] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:23] ============executionListener  end============


[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.489:[ INFO] [214320] [] [XNIO-2 task-2:2431224] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:18] ============executionListener start============
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.490:[ INFO] [214320] [] [XNIO-2 task-2:2431225] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:21] 事件名称:start
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.490:[ INFO] [214320] [] [XNIO-2 task-2:2431225] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:22] ActivitiId:task_1
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.491:[ INFO] [214320] [] [XNIO-2 task-2:2431226] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:23] ============executionListener  end============


[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.491:[ INFO] [214320] [] [XNIO-2 task-2:2431226] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:19] ============ActivitiEventListener start============
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.491:[ INFO] [214320] [] [XNIO-2 task-2:2431226] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:20] 事件类型:ACTIVITY_STARTED
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.492:[ INFO] [214320] [] [XNIO-2 task-2:2431227] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:21] ExecutionId:10021
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.492:[ INFO] [214320] [] [XNIO-2 task-2:2431227] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:22] ProcessInstanceId:10021
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.492:[ INFO] [214320] [] [XNIO-2 task-2:2431227] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:23] ============ActivitiEventListener  end============


[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.525:[ INFO] [214320] [] [XNIO-2 task-2:2431260] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:16] ============TaskListener start============
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.526:[ INFO] [214320] [] [XNIO-2 task-2:2431261] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:19] 事件名称:assignment
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.526:[ INFO] [214320] [] [XNIO-2 task-2:2431261] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:20] taskDefinitionKey:task_1
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.526:[ INFO] [214320] [] [XNIO-2 task-2:2431261] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:21] ============TaskListener end============

[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.527:[ INFO] [214320] [] [XNIO-2 task-2:2431262] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:16] ============TaskListener start============
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.527:[ INFO] [214320] [] [XNIO-2 task-2:2431262] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:19] 事件名称:create
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.527:[ INFO] [214320] [] [XNIO-2 task-2:2431262] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:20] taskDefinitionKey:task_1
[cp3-activiti-server:8780:0000:] 2021-01-21 15:32:17.528:[ INFO] [214320] [] [XNIO-2 task-2:2431263] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:21] ============TaskListener end============


[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.897:[ INFO] [214320] [] [XNIO-2 task-2:2797632] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:16] ============TaskListener start============
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.898:[ INFO] [214320] [] [XNIO-2 task-2:2797633] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:19] 事件名称:complete
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.898:[ INFO] [214320] [] [XNIO-2 task-2:2797633] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:20] taskDefinitionKey:task_1
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.898:[ INFO] [214320] [] [XNIO-2 task-2:2797633] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:21] ============TaskListener end============


[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.898:[ INFO] [214320] [] [XNIO-2 task-2:2797633] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:16] ============TaskListener start============
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.899:[ INFO] [214320] [] [XNIO-2 task-2:2797634] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:19] 事件名称:delete
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.899:[ INFO] [214320] [] [XNIO-2 task-2:2797634] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:20] taskDefinitionKey:task_1
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.899:[ INFO] [214320] [] [XNIO-2 task-2:2797634] [c.c.c.a.controller.flow.listener.MyTaskListener.notify:21] ============TaskListener end============


[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.914:[ INFO] [214320] [] [XNIO-2 task-2:2797649] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:18] ============executionListener start============
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.915:[ INFO] [214320] [] [XNIO-2 task-2:2797650] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:21] 事件名称:end
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.915:[ INFO] [214320] [] [XNIO-2 task-2:2797650] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:22] ActivitiId:task_1
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.915:[ INFO] [214320] [] [XNIO-2 task-2:2797650] [c.c.c.a.c.flow.listener.MyExecutionListener.notify:23] ============executionListener  end============


[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.919:[ INFO] [214320] [] [XNIO-2 task-2:2797654] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:19] ============ActivitiEventListener start============
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.919:[ INFO] [214320] [] [XNIO-2 task-2:2797654] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:20] 事件类型:ACTIVITY_STARTED
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.920:[ INFO] [214320] [] [XNIO-2 task-2:2797655] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:21] ExecutionId:10021
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.920:[ INFO] [214320] [] [XNIO-2 task-2:2797655] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:22] ProcessInstanceId:10021
[cp3-activiti-server:8780:MDAwMA==:] 2021-01-21 15:38:23.921:[ INFO] [214320] [] [XNIO-2 task-2:2797656] [c.c.c.a.c.flow.listener.MyActivitiEventListener.onEvent:23] ============ActivitiEventListener  end============

分析结果

  • 全局事件监听器,全个环节都会触发,连线不会触发。触发时间由type决定。
  • 从连线到task1环节的触发顺序:

工作流之监听器介绍及配置实现_第5张图片

 

你可能感兴趣的:(工作流,activiti,workflow)