目录
1. 使用 ProcessEngineConfiguration 注册
2. 使用 @Configuration 和 ApplicationListener 注册
引言
Flowable 是一个开源的工作流引擎,广泛用于业务流程管理和自动化任务处理。在 Flowable 中,监听器(Listeners)是一种强大的工具,用于在特定事件发生时执行自定义逻辑。本文将详细介绍如何在 Flowable 中注册全局的 TASK_CREATED 事件监听器的几种方式,特别是通过 @Configuration 和 ApplicationListener 的方式。
什么是 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 框架,可以通过 @Configuration 和 ApplicationListener 注册全局的 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
});
}
}
详细解释
-
定义事件监听器类:
FlowTestListener实现了FlowableEventListener接口,重写了onEvent方法来处理TASK_CREATED事件。
-
定义配置类:
GlobalEventListenerConfig类使用了@Configuration注解,表示这是一个配置类。- 使用了
@AllArgsConstructor注解来自动生成全参构造函数,以便通过构造函数注入依赖。 - 实现了
ApplicationListener<ContextRefreshedEvent>接口,重写了onApplicationEvent方法。 - 在
onApplicationEvent方法中,通过RuntimeService注册FlowTestListener监听器,并指定了要监听的事件类型TASK_CREATED。
运行示例
为了验证配置是否生效,可以在应用启动后创建一个任务并观察控制台输出。
-
启动应用:
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); } } -
创建任务:
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 事件监听器的几种方式,特别重点介绍了使用 @Configuration 和 ApplicationListener 的方式。这种方式不仅灵活,而且能够在 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 事件监听器。
4111

被折叠的 条评论
为什么被折叠?



