一、单个定时任务
1、导入依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-quartzartifactId>
dependency>
2、创建定时任务:
/**
@DisallowConcurrentExecution:
不允许并发执行,等待上一个任务执行完成后再执行
*/
@DisallowConcurrentExecution
public class PopulationTask implements Job, Serializable {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("定时任务开始了");
}
}
3、配置定时任务交给spring来管理
(1)第一种:
/**
* 把定时任务job交给spring管理
*/
@Component
public class JobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
// 调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
// 进行注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
(2)第二种:
/**
* 把定时任务job交给spring管理
*/
@Component
public class SpringJobBean extends SpringBeanJobFactory {
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
//进行注入
autowireCapableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
注:以上随便哪一种都可以
4、配置Quartz
@Autowired
private JobFactory jobFactory;
// @Autowired
// private SpringJobBean springJobBean;
@Bean
public JobDetail jobDetail(){
return JobBuilder.newJob(PopulationTask.class).withIdentity("myJob").storeDurably().build();
}
@Bean
public Trigger trigger(){
return TriggerBuilder.newTrigger().forJob(jobDetail())
//触发器随便起名字
.withIdentity("myTrigger")
//设置多长时间执行一次
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
}
/**
* 将定时人交给spring管理
*/
@Bean
public Scheduler scheduler() throws SchedulerException {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.scheduleJob(jobDetail(),trigger());
//允许将在job进行依赖注入,不然对象注入不进去,把job交给spring容器管理
scheduler.setJobFactory(jobFactory);
scheduler.start();
return scheduler;
}
二、多个定时任务
1、导入依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-quartzartifactId>
dependency>
2、创建定时任务:
/**
@DisallowConcurrentExecution:
不允许并发执行,等待上一个任务执行完成后再执行
*/
@DisallowConcurrentExecution
public class Task1 implements Job, Serializable {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("定时任务1开始了");
}
}
/**
@DisallowConcurrentExecution:
不允许并发执行,等待上一个任务执行完成后再执行
*/
@DisallowConcurrentExecution
public class Task2 implements Job, Serializable {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("定时任务2开始了");
}
}
3、配置定时任务交给spring来管理
(1)第一种:
/**
* 把定时任务job交给spring管理
*/
@Component
public class JobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
// 调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
// 进行注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
(2)第二种:
/**
* 把定时任务job交给spring管理
*/
@Component
public class SpringJobBean extends SpringBeanJobFactory {
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
//进行注入
autowireCapableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
注:以上随便哪一种都可以
4、配置Quartz
@Autowired
private JobFactory jobFactory;
// @Autowired
// private SpringJobBean springJobBean;
/**
* 1、定时任务1任务类型
* @return
*/
@Bean
public JobDetail task1JobDetail(){
return JobBuilder.newJob(PopulationTask.class)
.withIdentity("task1Job","group1")
.storeDurably().build();
}
/**
* 1、定时任务1触发器
* @return
*/
@Bean
public Trigger task1Trigger(){
return TriggerBuilder.newTrigger().forJob(populationJobDetail())
.withIdentity("task2Trigger","group1")
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 22 * * ?"))
.build();
}
/**
* 2、定时任务2任务类型
* @return
*/
@Bean
public JobDetail task2JobDetail(){
return JobBuilder.newJob(PermantPopulationTask.class)
.withIdentity("task2JobDetail","group2")
.storeDurably().build();
}
/**
* 2、定时任务2触发器
* @return
*/
@Bean
public Trigger task2Trigger(){
return TriggerBuilder.newTrigger().forJob(permantPopulationJobDetail())
.withIdentity("task2Trigger","group2")
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 22 * * ?"))
.build();
}
/**
* 将定时人交给spring管理
* @return
*/
@Bean
public SchedulerFactoryBean scheduler() {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
//定时任务类型
schedulerFactory.setJobDetails(task1JobDetail(),task2JobDetail());
//触发器
schedulerFactory.setTriggers(task1Trigger(),task2Trigger());
//允许将在job进行依赖注入,不然对象注入不进去,把job交给spring容器管理
schedulerFactory.setJobFactory(jobFactory);
//schedulerFactory.setJobFactory(springJobBean);
return schedulerFactory;
}