Axon Framework:Spring Boot 2.2.2中QuartzEventScheduler配置及Job实现选型
配置Axon Framework的QuartzEventScheduler(持久化任务)
嗨,我完全懂你现在的困惑——Axon的QuartzEventScheduler确实依赖Quartz生态,但你完全不需要自己实现Quartz的Job接口,Axon已经为你封装好了适配的Job类,下面一步步给你讲清楚:
1. 核心Job实现:Axon自带的AxonJob
Axon Framework为QuartzEventScheduler专门提供了org.axonframework.eventhandling.scheduling.quartz.AxonJob,这个类已经封装了Quartz调度触发与Axon事件发布的衔接逻辑,你直接用它就够了,不需要自定义任何Job实现。
2. Spring Boot 2.2.2中的配置步骤
2.1 添加依赖
确保你的项目依赖中包含Axon的Quartz扩展和Spring Boot的Quartz starter,注意版本要和你的Spring Boot 2.2.2适配:
Maven(pom.xml):
<dependency> <groupId>org.axonframework</groupId> <artifactId>axon-quartz</artifactId> <version>4.4.1</version> <!-- 此版本适配Spring Boot 2.2.x系列 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> <version>2.2.2.RELEASE</version> </dependency>
Gradle(build.gradle):
implementation 'org.axonframework:axon-quartz:4.4.1' implementation 'org.springframework.boot:spring-boot-starter-quartz:2.2.2.RELEASE'
2.2 配置QuartzEventScheduler Bean
在你的Spring配置类中,创建QuartzEventScheduler实例,关联Axon的EventBus和Spring管理的Quartz Scheduler:
import org.axonframework.eventhandling.EventBus; import org.axonframework.eventhandling.scheduling.quartz.QuartzEventScheduler; import org.quartz.Scheduler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AxonQuartzConfig { @Bean public QuartzEventScheduler quartzEventScheduler(Scheduler quartzScheduler, EventBus eventBus) { QuartzEventScheduler scheduler = new QuartzEventScheduler(); scheduler.setScheduler(quartzScheduler); scheduler.setEventBus(eventBus); // 可选:自定义调度任务的组标识,默认值为"AxonEvents" scheduler.setGroupIdentifier("MyAppScheduledEvents"); return scheduler; } }
2.3 配置Quartz持久化(实现任务不丢失的关键)
要让任务在JVM重启后不丢失,必须配置Quartz用数据库存储任务元数据。Spring Boot的Quartz starter会自动读取配置,你只需要在application.properties中添加:
# 指定Quartz使用JDBC存储任务 spring.quartz.job-store-type=jdbc # 首次启动时自动创建Quartz所需的数据库表 spring.quartz.jdbc.initialize-schema=always # Quartz核心配置 spring.quartz.properties.org.quartz.scheduler.instanceName=AxonEventScheduler spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO spring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate spring.quartz.properties.org.quartz.jobStore.tablePrefix=QRTZ_ spring.quartz.properties.org.quartz.jobStore.isClustered=false # 集群环境可改为true spring.quartz.properties.org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool spring.quartz.properties.org.quartz.threadPool.threadCount=5
3. 使用QuartzEventScheduler调度事件
配置完成后,你可以通过注入Axon的EventScheduler抽象接口(QuartzEventScheduler是它的实现)来调度事件:
import org.axonframework.eventhandling.scheduling.EventScheduler; import org.springframework.stereotype.Service; import java.time.Duration; @Service public class ScheduledEventService { private final EventScheduler eventScheduler; public ScheduledEventService(EventScheduler eventScheduler) { this.eventScheduler = eventScheduler; } public void scheduleDelayedEvent() { // 调度5分钟后发布MyDelayedEvent事件 eventScheduler.schedule(Duration.ofMinutes(5), new MyDelayedEvent("user-123")); } } // 自定义的调度事件类 public class MyDelayedEvent { private final String userId; public MyDelayedEvent(String userId) { this.userId = userId; } public String getUserId() { return userId; } }
4. 重要提示
- 无需自定义Quartz Job:
AxonJob已经帮你处理了从Quartz触发到Axon事件发布的所有逻辑,你只需要关注业务事件本身。 - 版本兼容性:确保Axon版本和Spring Boot版本匹配,Axon 4.4.x和Spring Boot 2.2.x是适配的。
- 持久化验证:启动应用后,数据库会生成以
QRTZ_为前缀的表,调度任务后可以查看QRTZ_JOB_DETAILS和QRTZ_TRIGGERS表,确认任务已持久化。
内容的提问来源于stack exchange,提问作者Kenneth Clark
相关产品推荐
相关产品推荐

