使用Quartz2.3.2+spring boot实现数据库持久化存储多个定时任务
时间: 2024-03-21 09:42:03 浏览: 218
Quartz+Spring Boot实现动态管理定时任务
5星 · 资源好评率100%
好的,我了解了你的问题。要使用Quartz2.3.2和Spring Boot实现数据库持久化存储多个定时任务,你可以按照以下步骤操作:
1. 在Spring Boot项目中添加Quartz2.3.2的依赖包:
```xml
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
```
2. 创建一个继承了`org.quartz.impl.jdbcjobstore.PostgreSQLDelegate`的类,用于实现PostgreSQL数据库的存储。
```java
public class PostgreSQLDelegateCustom extends PostgreSQLDelegate {
private String tablePrefix = "";
public PostgreSQLDelegateCustom() {
super();
}
@Override
public void setInstanceName(String schedName) {
super.setInstanceName(schedName);
this.tablePrefix = schedName + ".";
}
@Override
public String getSelectNextTriggerToAcquireSql(int maxCount) {
return "SELECT * FROM " + tablePrefix + "QRTZ_TRIGGERS WHERE " + "TRIGGER_STATE = ? AND NEXT_FIRE_TIME <= ? " + "AND MISFIRE_INSTR IN (" + MisfireInstruction.IGNORE_MISFIRE_POLICY + "," + MisfireInstruction.SMART_POLICY + ") ORDER BY NEXT_FIRE_TIME ASC";
}
@Override
public String getSelectNextMisfiredTriggersInStateSql(Connection conn, String state, long ts) {
return "SELECT * FROM " + tablePrefix + "QRTZ_TRIGGERS WHERE " + "TRIGGER_STATE = ? AND NEXT_FIRE_TIME <= ? " + "AND MISFIRE_INSTR NOT IN (" + MisfireInstruction.IGNORE_MISFIRE_POLICY + "," + MisfireInstruction.SMART_POLICY + ") ORDER BY NEXT_FIRE_TIME ASC";
}
@Override
public String getSelectPausedTriggerGroupsSql() {
return "SELECT TRIGGER_GROUP FROM " + tablePrefix + "QRTZ_PAUSED_TRIGGER_GRPS";
}
@Override
public String getSelectPausedTriggerGroupsLikeSql() {
return "SELECT TRIGGER_GROUP FROM " + tablePrefix + "QRTZ_PAUSED_TRIGGER_GRPS WHERE TRIGGER_GROUP LIKE ?";
}
@Override
public String getSelectFiredTriggersSql(int limit) {
return "SELECT * FROM " + tablePrefix + "QRTZ_FIRED_TRIGGERS ORDER BY FIRE_TIME ASC LIMIT " + limit;
}
@Override
public String getSelectFiredTriggerSql() {
return "SELECT * FROM " + tablePrefix + "QRTZ_FIRED_TRIGGERS WHERE ENTRY_ID = ?";
}
@Override
public String getInsertPausedTriggerGroupSql() {
return "INSERT INTO " + tablePrefix + "QRTZ_PAUSED_TRIGGER_GRPS (TRIGGER_GROUP) VALUES (?)";
}
@Override
public String getDeletePausedTriggerGroupSql() {
return "DELETE FROM " + tablePrefix + "QRTZ_PAUSED_TRIGGER_GRPS WHERE TRIGGER_GROUP = ?";
}
@Override
public String getDeleteAllPausedTriggerGroupsSql() {
return "DELETE FROM " + tablePrefix + "QRTZ_PAUSED_TRIGGER_GRPS";
}
@Override
public String getInsertFiredTriggerSql() {
return "INSERT INTO " + tablePrefix + "QRTZ_FIRED_TRIGGERS (ENTRY_ID, " + "TRIGGER_NAME, TRIGGER_GROUP, " + "IS_VOLATILE, INSTANCE_NAME, " + "FIRED_TIME, SCHED_TIME, " + "PRIORITY, STATE, JOB_NAME, " + "JOB_GROUP, IS_STATEFUL, " + "REQUESTS_RECOVERY, " + "MISFIRE_INSTR) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
}
@Override
public String getUpdateFiredTriggerSql() {
return "UPDATE " + tablePrefix + "QRTZ_FIRED_TRIGGERS SET " + "FIRED_TIME = ?, SCHED_TIME = ?, STATE = ? " + "WHERE ENTRY_ID = ?";
}
@Override
public String getDeleteFiredTriggerSql() {
return "DELETE FROM " + tablePrefix + "QRTZ_FIRED_TRIGGERS WHERE ENTRY_ID = ?";
}
@Override
public String getDeleteFiredTriggersSql(String instanceId) {
return "DELETE FROM " + tablePrefix + "QRTZ_FIRED_TRIGGERS WHERE INSTANCE_NAME = ?";
}
@Override
public String getSelectInstancesFiredTriggerCountSql() {
return "SELECT COUNT(*) FROM " + tablePrefix + "QRTZ_FIRED_TRIGGERS WHERE INSTANCE_NAME = ?";
}
}
```
3. 在Spring Boot配置文件中配置Quartz的相关参数:
```yaml
spring:
quartz:
job-store-type: jdbc
properties:
org:
quartz:
scheduler:
instanceId: AUTO
instanceName: my-scheduler
jobStore:
driverDelegateClass: com.example.PostgreSQLDelegateCustom
useProperties: false
isClustered: false
dataSource: myDataSource
tablePrefix: QRTZ_
threadPool:
threadCount: 10
plugin:
shutdownhook:
class: org.quartz.plugins.management.ShutdownHookPlugin
cleanShutdown: true
jobHistory:
class: org.quartz.plugins.history.LoggingJobHistoryPlugin
jobHistoryLogLevel: INFO
```
4. 创建一个继承了`org.quartz.Job`的类,用于定义任务的具体逻辑:
```java
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// 任务逻辑代码
}
}
```
5. 在Spring Boot的任意一个类中添加以下代码,用于配置和启动Quartz Scheduler:
```java
@Configuration
public class QuartzConfig {
@Bean
public JobDetail myJobDetail() {
return JobBuilder.newJob(MyJob.class)
.withIdentity("myJob")
.storeDurably()
.build();
}
@Bean
public Trigger myTrigger() {
return TriggerBuilder.newTrigger()
.forJob(myJobDetail())
.withIdentity("myTrigger")
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setDataSource(dataSource);
schedulerFactoryBean.setJobDetails(myJobDetail());
schedulerFactoryBean.setTriggers(myTrigger());
return schedulerFactoryBean;
}
}
```
6. 最后,你需要在PostgreSQL数据库中创建Quartz所需的表结构。你可以使用Quartz提供的`tables_postgres.sql`文件进行创建。
至此,你已经成功实现了使用Quartz2.3.2和Spring Boot实现数据库持久化存储多个定时任务。
阅读全文