版权声明:本文为博主原创文章,转载请注明出处。 https://blog.****.net/liuchuanhong1/article/details/78543574
最近收到了很多封邮件,都是想知道spring boot整合quartz如何实现多个定时任务的,由于本人生产上并没有使用到多个定时任务,这里给个实现的思路。
1、新建两个定时任务,如下:
-
public class ScheduledJob implements Job{
-
@Override
-
public void execute(JobExecutionContext context) throws JobExecutionException {
-
System.out.println("schedule job1 is running…………………………………… ");
-
}
-
}
-
public class ScheduledJob2 implements Job {
-
@Override
-
public void execute(JobExecutionContext context) throws JobExecutionException {
-
System.out.println("schedule job2 is running ……………………………………………………");
-
}
-
}
2、配置以上两个任务
-
@Component
-
public class SchedulerAllJob {
-
@Autowired
-
private SchedulerFactoryBean schedulerFactoryBean;
-
/*
-
* 此处可以注入数据库操作,查询出所有的任务配置
-
*/
-
/**
-
* 该方法用来启动所有的定时任务
-
* @throws SchedulerException
-
*/
-
public void scheduleJobs() throws SchedulerException {
-
Scheduler scheduler = schedulerFactoryBean.getScheduler();
-
/**
-
*
-
*/
-
scheduleJob1(scheduler);
-
scheduleJob2(scheduler);
-
}
-
/**
-
* 配置Job1
-
* 此处的任务可以配置可以放到properties或者是放到数据库中
-
* 如果此时需要做到动态的定时任务,请参考:http://blog.****.net/liuchuanhong1/article/details/60873295
-
* 博客中的ScheduleRefreshDatabase类
-
* @param scheduler
-
* @throws SchedulerException
-
*/
-
private void scheduleJob1(Scheduler scheduler) throws SchedulerException{
-
/*
-
* 此处可以先通过任务名查询数据库,如果数据库中存在该任务,则按照ScheduleRefreshDatabase类中的方法,更新任务的配置以及触发器
-
* 如果此时数据库中没有查询到该任务,则按照下面的步骤新建一个任务,并配置初始化的参数,并将配置存到数据库中
-
*/
-
JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();
-
// 每5s执行一次
-
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
-
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
-
scheduler.scheduleJob(jobDetail,cronTrigger);
-
}
-
/**
-
* 配置Job
-
* @param scheduler
-
* @throws SchedulerException
-
*/
-
private void scheduleJob2(Scheduler scheduler) throws SchedulerException{
-
JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job2", "group1").build();
-
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
-
// 每10s执行一次
-
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1") .withSchedule(scheduleBuilder).build();
-
scheduler.scheduleJob(jobDetail,cronTrigger);
-
}
-
}
3、启动两个任务
-
@Configuration
-
@EnableScheduling
-
@Component
-
public class SchedulerListener {
-
@Autowired
-
public SchedulerAllJob myScheduler;
-
/**
-
* 启动的时候执行该方法,或者是使用ApplicationListener,在启动的时候执行该方法
-
* 具体使用见:http://blog.****.net/liuchuanhong1/article/details/77568187
-
* @throws SchedulerException
-
*/
-
@Scheduled(cron="0 08 18 ? * *")
-
public void schedule() throws SchedulerException {
-
myScheduler.scheduleJobs();
-
}
-
@Bean
-
public SchedulerFactoryBean schedulerFactoryBean(){
-
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
-
return schedulerFactoryBean;
-
}
-
}
4、测试结果如下
-
schedule job1 is running……………………………………
-
schedule job2 is running ……………………………………………………
-
schedule job1 is running……………………………………
-
schedule job1 is running……………………………………
-
schedule job2 is running ……………………………………………………
-
schedule job1 is running……………………………………
-
schedule job1 is running……………………………………
-
schedule job2 is running ……………………………………………………
-
schedule job1 is running……………………………………