Quartz 的使用思路
- job - 任务 - 你要做什么事?
- Trigger - 触发器 - 你什么时候去做?
- Scheduler - 任务调度 - 你什么时候需要去做什么事?
1. 修改 pom 文件添加坐标
<!-- 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>
2. 创建 Job 类
/**
* 定义任务类
*/
public class MyJob implements Job {
/**
* 任务被触发时所执行的方法
*/
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("my job +++++"+new Date());
}
}
3. 编写 Quartz 的启动类
@Configuration
public class MyQuartz {
/**
* 创建 job 对象
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean (){
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
// 关联 job 类
jobDetailFactoryBean.setJobClass(MyJob.class);
return jobDetailFactoryBean;
}
/**
* 创建 Trigger 对象
*/
// 简单的 Trigger
@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
// 关联 JobDetailFactoryBean 对象
factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
// 设置重复时间间隔
factoryBean.setRepeatInterval(2000);
// 设置重复次数
factoryBean.setRepeatCount(5);
return factoryBean;
}
/**
* 创建 Scheduler 对象
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean){
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
// 关联 trigger
factoryBean.setTriggers(simpleTriggerFactoryBean.getObject());
return factoryBean;
}
}
4. 修改启动类
@SpringBootApplication
@EnableScheduling
public class App {
public static void main(String[] args){
SpringApplication.run(App.class,args);
}
}
5. 执行结果:
6. Job 类中注入对象
6.1 创建 service
@Service
public class UserService {
public void addUser(){
System.out.println("add user..."+new Date());
}
}
6.2 创建 job
public class MyJob implements Job {
/**
* 任务被触发时所执行的方法
*/
@Autowired
UserService userService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
userService.addUser();
}
}
6.3 编写一个 MyAdaptableJobFactory
我们要实现将 Service 层里的 userService 对象注入给 job,来完成具体的 service 层任务,则需要二者都在spring容器中。而 JobDetailFactoryBean.setJobClass(QuartzJob.class) 是通过 AdaptableJobFactory 反射直接初始化 job 对象的,不经过spring 容器。因此我们需要编写一个 MyAdaptableJobFactory 继承 AdaptableJobFactory 类,重写里面的create方法,完成将job对象也注入进spring容器中。
@Component
public class MyAdaptableJobFactory extends AdaptableJobFactory {
//AutowireCapableBeanFactory 可以将一个对象添加到SpringIOC容器中,并且完成该对象注入
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object object = super.createJobInstance(bundle);
//将obj对象添加Spring IOC容器中,并完成注入
this.autowireCapableBeanFactory.autowireBean(object);
return object;
}
}
6.3 修改 Quartz 的启动类
Spring的Java配置方式是通过@Configuration和@Bean这两个注解来实现
@Configuration可以作用在任意类上,表示该类是一个配置类,其实就相当于一个xml配置文件。
@Bean作用于方法上,其实就相当于xml配置文件中的bean,表示创建一个Bean,方法的返回值类型表示该Bean的类型,方法名表示该Bean的ID。
@Configuration
public class MyQuartz {
/**
* 创建 job 对象
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean (){
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
// 关联 job 类
jobDetailFactoryBean.setJobClass(MyJob.class);
return jobDetailFactoryBean;
}
/**
* 创建 Trigger 对象
* Cron trigger
*/
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
factoryBean.setCronExpression("0/2 * * * * ?");
return factoryBean;
}
/**
* 创建 Scheduler 对象
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory){
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
// 关联 trigger
factoryBean.setTriggers(cronTriggerFactoryBean.getObject());
factoryBean.setJobFactory(myAdaptableJobFactory);
return factoryBean;
}
}
6.4 执行结果: