版權聲明:本文為博主原創文章,轉載請注明出處。 https://blog.csdn.net/liuchuanhong1/article/details/78543574
最近收到了很多封郵件,都是想知道spring boot整合quartz如何實現多個定時任務的,由於本人生產上並沒有使用到多個定時任務,這里給個實現的思路。
1、新建兩個定時任務,如下:
-
public class ScheduledJob implements Job{
-
-
public void execute(JobExecutionContext context) throws JobExecutionException {
-
System.out.println( "schedule job1 is running…………………………………… ");
-
}
-
}
-
public class ScheduledJob2 implements Job {
-
-
-
public void execute(JobExecutionContext context) throws JobExecutionException {
-
System.out.println( "schedule job2 is running ……………………………………………………");
-
}
-
}
2、配置以上兩個任務
-
-
public class SchedulerAllJob {
-
-
private SchedulerFactoryBean schedulerFactoryBean;
-
-
/*
-
* 此處可以注入數據庫操作,查詢出所有的任務配置
-
*/
-
-
/**
-
* 該方法用來啟動所有的定時任務
-
* @throws SchedulerException
-
*/
-
public void scheduleJobs() throws SchedulerException {
-
Scheduler scheduler = schedulerFactoryBean.getScheduler();
-
-
/**
-
*
-
*/
-
scheduleJob1(scheduler);
-
scheduleJob2(scheduler);
-
}
-
-
/**
-
* 配置Job1
-
* 此處的任務可以配置可以放到properties或者是放到數據庫中
-
* 如果此時需要做到動態的定時任務,請參考:http://blog.csdn.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、啟動兩個任務
-
-
-
-
public class SchedulerListener {
-
-
-
public SchedulerAllJob myScheduler;
-
-
/**
-
* 啟動的時候執行該方法,或者是使用ApplicationListener,在啟動的時候執行該方法
-
* 具體使用見:http://blog.csdn.net/liuchuanhong1/article/details/77568187
-
* @throws SchedulerException
-
*/
-
-
public void schedule() throws SchedulerException {
-
myScheduler.scheduleJobs();
-
}
-
-
-
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……………………………………
-
