SpringBoot 根据数据库动态Quartz定时
时间: 2023-11-20 12:59:44 浏览: 173
在Spring Boot项目中使用Quartz实现动态定时任务,需要完成以下步骤:
1. 引入Quartz依赖
在`pom.xml`文件中添加Quartz依赖:
```xml
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
```
2. 配置Quartz
创建`QuartzConfig`类,配置Quartz的参数和JobDetail:
```java
@Configuration
public class QuartzConfig {
// 配置Scheduler
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(jobFactory());
schedulerFactoryBean.setQuartzProperties(quartzProperties());
return schedulerFactoryBean;
}
// 配置JobDetail
@Bean
public JobDetail jobDetail() {
return JobBuilder.newJob(MyJob.class)
.withIdentity("myJob")
.storeDurably()
.build();
}
// 配置JobFactory
@Bean
public JobFactory jobFactory() {
return new SpringBeanJobFactory();
}
// 配置Quartz参数
@Bean
public Properties quartzProperties() {
Properties properties = new Properties();
properties.setProperty("org.quartz.scheduler.instanceName", "MyScheduler");
properties.setProperty("org.quartz.scheduler.instanceId", "AUTO");
properties.setProperty("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
properties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
properties.setProperty("org.quartz.jobStore.dataSource", "myDS");
properties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
properties.setProperty("org.quartz.dataSource.myDS.driver", "com.mysql.jdbc.Driver");
properties.setProperty("org.quartz.dataSource.myDS.URL", "jdbc:mysql://localhost:3306/mydb");
properties.setProperty("org.quartz.dataSource.myDS.user", "root");
properties.setProperty("org.quartz.dataSource.myDS.password", "root");
properties.setProperty("org.quartz.dataSource.myDS.maxConnections", "10");
return properties;
}
}
```
其中,`MyJob`是自定义的Job类,需要实现`org.quartz.Job`接口。
3. 创建Job和Trigger
创建`Job`和`Trigger`的实例,并将`Job`和`Trigger`注册到`Scheduler`中:
```java
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
@Autowired
private JobDetail jobDetail;
@PostConstruct
public void init() throws SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
// 创建Trigger
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger")
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) // 每5秒执行一次
.build();
// 将Job和Trigger注册到Scheduler中
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
```
4. 编写Job逻辑
创建自定义的`Job`类,实现`org.quartz.Job`接口:
```java
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// 编写Job逻辑
}
}
```
在`execute`方法中编写具体的定时任务逻辑。
5. 动态修改定时任务
Quartz支持动态修改定时任务的执行时间,可以使用以下代码实现:
```java
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
@PutMapping("/modifyTrigger")
public void modifyTrigger() throws SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
// 获取Trigger
TriggerKey triggerKey = TriggerKey.triggerKey("myTrigger");
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (trigger == null) {
return;
}
// 修改Trigger的执行时间
String cronExpression = "0/10 * * * * ?"; // 每10秒执行一次
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
// 重新注册Trigger
scheduler.rescheduleJob(triggerKey, trigger);
}
```
在上面的代码中,使用`getTrigger`方法获取`Trigger`对象,然后使用`getTriggerBuilder`方法修改`Trigger`的执行时间,最后使用`rescheduleJob`方法重新注册`Trigger`对象即可。
以上就是在Spring Boot项目中使用Quartz实现动态定时任务的步骤。
阅读全文