SpringBoot整合Quartz定时任务框架

Quartz的使用思路

job - 任务 - 你要做什么事?
Trigger - 触发器 - 你什么时候去做?
Scheduler - 任务调度 - 你什么时候需要去做什么事?

添加 Quartz 的坐标
1
2
3
4
5
6
<!-- Quartz 坐标 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
创建 Job 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 定义任务类
*
*
*/
public class QuartzDemo implements Job {
/**
* 任务被触发时所执行的方法
*/
public void execute(JobExecutionContext arg0) throws
JobExecutionException {
System.out.println("Execute...."+new Date());
}
}
写测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class QuartzMain {
public static void main(String[] args) throws Exception {
// 1.创建 Job 对象:你要做什么事?
JobDetail job = JobBuilder.newJob(QuartzDemo.class).build();
/**
* 简单的 trigger 触发时间:通过 Quartz 提供一个方法来完成简单的重复
调用 cron
* Trigger:按照 Cron 的表达式来给定触发的时间
*/
// 2.创建 Trigger 对象:在什么时间做?
/*Trigger trigger =
TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSe
condlyForever())
.build();*/
Trigger trigger =
TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedu
le("0/2 * * * * ?"))
.build();
// 3.创建 Scheduler 对象:在什么时间做什么事?
Scheduler scheduler =
StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(job, trigger);
//启动
scheduler.start();
}
}
```
#### SpringBoot整合Quartz定时框架
##### 修改pom文件添加坐标
``` java
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Quartz 坐标 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加 Scheduled 坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- Sprng tx 坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
编写 Quartz 的启动类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Quartz 配置类
*
*
*/
@Configuration
public class QuartzConfig {
/**
* 1.创建 Job 对象
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean(){
JobDetailFactoryBean factory = new JobDetailFactoryBean();
//关联我们自己的 Job 类
factory.setJobClass(QuartzDemo.class);
return factory;
}
/**
* 2.创建 Trigger 对象
* 简单的 Trigger
*/
/*@Bean
public SimpleTriggerFactoryBean
simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
SimpleTriggerFactoryBean factory = new
SimpleTriggerFactoryBean();
//关联 JobDetail 对象
factory.setJobDetail(jobDetailFactoryBean.getObject());
//该参数表示一个执行的毫秒数
factory.setRepeatInterval(2000);
//重复次数
factory.setRepeatCount(5);
return factory;
}*/
/**
* Cron Trigger
*/
@Bean
public CronTriggerFactoryBean
cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
factory.setJobDetail(jobDetailFactoryBean.getObject());
//设置触发时间
factory.setCronExpression("0/2 * * * * ?");
return factory;
}
/**
* 3.创建 Scheduler 对象
*/
@Bean
public SchedulerFactoryBean
schedulerFactoryBean(CronTriggerFactoryBean
cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory){
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联 trigger
factory.setTriggers(cronTriggerFactoryBean.getObject());
factory.setJobFactory(myAdaptableJobFactory);
return factory;
}
}
添加MyAdaptableJobFactory解决Spring注入问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Component("myAdaptableJobFactory")
public class MyAdaptableJobFactory extends AdaptableJobFactory {
//AutowireCapableBeanFactory 可以将一个对象添加到 SpringIOC 容器中,
并且完成该对象注入
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
/**
* 该方法需要将实例化的任务对象手动的添加到 springIOC 容器中并且完成对
象的注入
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
Object obj = super.createJobInstance(bundle);
//将 obj 对象添加 Spring IOC 容器中,并完成注入
this.autowireCapableBeanFactory.autowireBean(obj);
return obj;
}
}