Flowable 注册全局事件监听器的几种方式

目录

引言

什么是 TASK_CREATED 事件监听器?

注册全局 TASK_CREATED 事件监听器的方式

1. 使用 ProcessEngineConfiguration 注册

2. 使用 @Configuration 和 ApplicationListener 注册

详细解释

运行示例

结论

示例代码

1. 定义事件监听器类

2. 定义配置类

3. 启动应用

4. 创建任务


引言

Flowable 是一个开源的工作流引擎,广泛用于业务流程管理和自动化任务处理。在 Flowable 中,监听器(Listeners)是一种强大的工具,用于在特定事件发生时执行自定义逻辑。本文将详细介绍如何在 Flowable 中注册全局的 TASK_CREATED 事件监听器的几种方式,特别是通过 @ConfigurationApplicationListener 的方式。

什么是 TASK_CREATED 事件监听器?

TASK_CREATED 事件监听器是在任务创建时触发的监听器。通过注册 TASK_CREATED 事件监听器,你可以在任务创建后立即执行一些自定义操作,例如发送通知、记录日志等。

注册全局 TASK_CREATED 事件监听器的方式
1. 使用 ProcessEngineConfiguration 注册

在 Flowable 的配置中,可以通过 ProcessEngineConfiguration 注册全局的 TASK_CREATED 事件监听器。这种方式适用于在应用启动时全局配置监听器。

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.delegate.event.FlowableEventListener;
import org.flowable.engine.delegate.event.FlowableEvent;
import org.flowable.engine.delegate.event.FlowableTaskCreatedEvent;

public class TaskCreatedListener implements FlowableEventListener {

    @Override
    public void onEvent(FlowableEvent event) {
        if (event instanceof FlowableTaskCreatedEvent) {
            FlowableTaskCreatedEvent taskCreatedEvent = (FlowableTaskCreatedEvent) event;
            System.out.println("Task created: " + taskCreatedEvent.getTask().getName());
            // 执行自定义逻辑
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    public static void main(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();

        // 注册 TASK_CREATED 事件监听器
        processEngineConfiguration.getEventDispatcher().addEventListener(new TaskCreatedListener());
    }
}
2. 使用 @Configuration 和 ApplicationListener 注册

如果你使用 Spring 框架,可以通过 @ConfigurationApplicationListener 注册全局的 TASK_CREATED 事件监听器。这种方式更加灵活,可以在 Spring 容器启动时自动注册监听器。

首先,定义一个事件监听器类:

import org.flowable.engine.delegate.event.FlowableEvent;
import org.flowable.engine.delegate.event.FlowableTaskCreatedEvent;
import org.flowable.engine.delegate.event.FlowableEventListener;

public class FlowTestListener implements FlowableEventListener {

    @Override
    public void onEvent(FlowableEvent event) {
        if (event instanceof FlowableTaskCreatedEvent) {
            FlowableTaskCreatedEvent taskCreatedEvent = (FlowableTaskCreatedEvent) event;
            System.out.println("Task created: " + taskCreatedEvent.getTask().getName());
            // 执行自定义逻辑
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }
}

然后,定义一个配置类来注册事件监听器:

import lombok.AllArgsConstructor;
import org.flowable.engine.delegate.event.FlowableEngineEventType;
import org.flowable.engine.impl.event.logger.EventLogger;
import org.flowable.engine.runtime.RuntimeService;
import org.flowable.spring.event.ApplicationEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Configuration
@AllArgsConstructor
public class GlobalEventListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private FlowTestListener flowTestListener;

    @Autowired
    private RuntimeService runtimeService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 注册 TASK_CREATED 事件监听器
        runtimeService.addEventListener(flowTestListener, new FlowableEngineEventType[] {
                FlowableEngineEventType.TASK_CREATED
        });
    }
}
详细解释
  1. 定义事件监听器类

    • FlowTestListener 实现了 FlowableEventListener 接口,重写了 onEvent 方法来处理 TASK_CREATED 事件。
  2. 定义配置类

    • GlobalEventListenerConfig 类使用了 @Configuration 注解,表示这是一个配置类。
    • 使用了 @AllArgsConstructor 注解来自动生成全参构造函数,以便通过构造函数注入依赖。
    • 实现了 ApplicationListener<ContextRefreshedEvent> 接口,重写了 onApplicationEvent 方法。
    • 在 onApplicationEvent 方法中,通过 RuntimeService 注册 FlowTestListener 监听器,并指定了要监听的事件类型 TASK_CREATED
运行示例

为了验证配置是否生效,可以在应用启动后创建一个任务并观察控制台输出。

  1. 启动应用

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class FlowableDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FlowableDemoApplication.class, args);
        }
    }
  2. 创建任务

    import org.flowable.engine.RuntimeService;
    import org.flowable.engine.TaskService;
    import org.flowable.engine.runtime.ProcessInstance;
    import org.flowable.task.api.Task;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    @Component
    public class TaskCreator implements CommandLineRunner {
    
        @Autowired
        private RuntimeService runtimeService;
    
        @Autowired
        private TaskService taskService;
    
        @Override
        public void run(String... args) throws Exception {
            // 启动流程实例
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
    
            // 创建任务
            Task task = taskService.newTask();
            task.setName("My Task");
            task.setAssignee("John Doe");
            taskService.saveTask(task);
    
            System.out.println("Task created with ID: " + task.getId());
        }
    }
结论

通过本文,我们详细介绍了在 Flowable 中注册全局 TASK_CREATED 事件监听器的几种方式,特别重点介绍了使用 @ConfigurationApplicationListener 的方式。这种方式不仅灵活,而且能够在 Spring 容器启动时自动注册监听器,非常适合在现代 Spring 应用中使用。希望本文对您在使用 Flowable 进行工作流管理时有所帮助。

如果您有任何问题或建议,请随时留言交流。祝您编程愉快!


示例代码

为了方便您理解和测试,以下是完整的示例代码:

1. 定义事件监听器类
import org.flowable.engine.delegate.event.FlowableEvent;
import org.flowable.engine.delegate.event.FlowableTaskCreatedEvent;
import org.flowable.engine.delegate.event.FlowableEventListener;

public class FlowTestListener implements FlowableEventListener {

    @Override
    public void onEvent(FlowableEvent event) {
        if (event instanceof FlowableTaskCreatedEvent) {
            FlowableTaskCreatedEvent taskCreatedEvent = (FlowableTaskCreatedEvent) event;
            System.out.println("Task created: " + taskCreatedEvent.getTask().getName());
            // 执行自定义逻辑
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }
}
2. 定义配置类
import lombok.AllArgsConstructor;
import org.flowable.engine.delegate.event.FlowableEngineEventType;
import org.flowable.engine.runtime.RuntimeService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Configuration
@AllArgsConstructor
public class GlobalEventListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final FlowTestListener flowTestListener;
    private final RuntimeService runtimeService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 注册 TASK_CREATED 事件监听器
        runtimeService.addEventListener(flowTestListener, new FlowableEngineEventType[] {
                FlowableEngineEventType.TASK_CREATED
        });
    }
}
3. 启动应用
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FlowableDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(FlowableDemoApplication.class, args);
    }
}
4. 创建任务
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TaskCreator implements CommandLineRunner {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Override
    public void run(String... args) throws Exception {
        // 启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");

        // 创建任务
        Task task = taskService.newTask();
        task.setName("My Task");
        task.setAssignee("John Doe");
        taskService.saveTask(task);

        System.out.println("Task created with ID: " + task.getId());
    }
}

希望这些示例代码能够帮助您更好地理解和实现 Flowable 中的全局 TASK_CREATED 事件监听器。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值