springboot quartz动态定时任务
时间: 2023-09-15 10:15:26 浏览: 171
Spring Boot集成Quartz实现动态定时任务的步骤如下:
1. 添加依赖
在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
```
2. 配置Quartz
在application.properties中添加Quartz相关配置:
```properties
# 配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 配置Quartz
spring.quartz.job-store-type=jdbc
spring.quartz.jdbc.initialize-schema=always
```
这里我们使用了MySQL数据库作为Quartz的存储介质,因此需要配置数据源。
3. 创建Job
创建一个Job类,实现org.quartz.Job接口,实现execute方法,此方法为定时任务的具体执行逻辑。
```java
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("定时任务执行了!");
}
}
```
4. 创建Trigger
创建一个Trigger类,用于配置定时任务的触发规则,这里我们使用CronTrigger,可以根据Cron表达式来设置任务的触发时间。
```java
public class MyTrigger {
public static Trigger buildTrigger(String cron) {
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
return TriggerBuilder.newTrigger()
.withSchedule(scheduleBuilder)
.build();
}
}
```
5. 创建Scheduler
创建一个Scheduler类,用于创建和启动定时任务。
```java
@Configuration
public class QuartzConfig {
@Autowired
private DataSource dataSource;
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setDataSource(dataSource);
schedulerFactoryBean.setOverwriteExistingJobs(true);
schedulerFactoryBean.setAutoStartup(true);
schedulerFactoryBean.setQuartzProperties(quartzProperties());
return schedulerFactoryBean;
}
@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/quartz?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC");
properties.setProperty("org.quartz.dataSource.myDS.user", "root");
properties.setProperty("org.quartz.dataSource.myDS.password", "root");
properties.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
properties.setProperty("org.quartz.threadPool.threadCount", "10");
properties.setProperty("org.quartz.threadPool.threadPriority", "5");
return properties;
}
@Bean
public Scheduler scheduler() {
return schedulerFactoryBean().getScheduler();
}
}
```
在这里我们使用了@Configuration注解将Quartz的配置类配置为一个Spring Bean,在其中创建了SchedulerFactoryBean和Scheduler两个Bean,并且在schedulerFactoryBean()方法中设置了Quartz的相关属性,比如数据源、是否覆盖已有的Job等。
6. 动态添加Job
最后,我们可以在需要的时候动态地添加Job:
```java
@Autowired
private Scheduler scheduler;
public void addJob(String jobName, String jobGroup, String triggerName, String triggerGroup, String cron) throws SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
.withIdentity(jobName, jobGroup)
.build();
Trigger trigger = MyTrigger.buildTrigger(cron);
trigger = trigger.getTriggerBuilder()
.withIdentity(triggerName, triggerGroup)
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
```
其中,jobName、jobGroup、triggerName、triggerGroup分别为Job和Trigger的名称和分组,cron为触发规则。
至此,我们就可以动态地添加和启动定时任务了。
阅读全文