camunda7动态添加监听器

在Camunda7中,主要有两种类型的监听器:

  1. Execution Listener(执行监听器)
    • 用于监听流程实例的生命周期事件。
    • 事件类型包括:
      • start:当流程实例或活动启动时触发。
      • end:当流程实例或活动结束时触发。
  2. Task Listener(任务监听器)
    • 用于监听用户任务的生命周期事件。
    • 事件类型包括:
      • create:当任务创建时触发。
      • assignment:当任务被分配时触发。
      • complete:当任务完成时触发。
      • delete:当任务被删除时触发。

这两种监听器可以在BPMN模型中配置,也可以通过Java API动态添加。这里介绍动态添加的方法,旨在每个任务节点动态添加监听器,不需要在bpmn文件中添加。

开发步骤:

  1. 添加两个全局监听GlobalExecutionListener.javaGlobalTaskListener.java:

    GlobalExecutionListener.java用于监听执行事件

    import org.camunda.bpm.engine.delegate.DelegateExecution;
    import org.camunda.bpm.engine.delegate.ExecutionListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class GlobalExecutionListener implements ExecutionListener {
         
        @Override
        public void notify(DelegateExecution execution) {
         
            // 处理执行事件
            String eventName = execution.getEventName();
            //System.out.println("Execution Event: " + eventName);
            //System.out.println("Process Instance ID: " + execution.getProcessInstanceId());
            if ("end".equals(eventName)) {
         
                Object abnormalId = execution.getVariable("abnormalId");
            }
        }
    }
    

    GlobalTaskListener.java用于监听任务事件:

    import org.camunda.bpm

你可能感兴趣的:(java)