最近收到了很多封郵件,都是想知道spring boot整合quartz如何實現(xiàn)多個定時任務(wù)的,由于本人生產(chǎn)上并沒有使用到多個定時任務(wù),這里給個實現(xiàn)的思路。
成都創(chuàng)新互聯(lián)主營江源網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā)公司,江源h5微信小程序開發(fā)搭建,江源網(wǎng)站營銷推廣歡迎江源等地區(qū)企業(yè)咨詢
1、新建兩個定時任務(wù),如下:
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、配置以上兩個任務(wù)
@Component public class SchedulerAllJob { @Autowired private SchedulerFactoryBean schedulerFactoryBean; /* * 此處可以注入數(shù)據(jù)庫操作,查詢出所有的任務(wù)配置 */ /** * 該方法用來啟動所有的定時任務(wù) * @throws SchedulerException */ public void scheduleJobs() throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); /** * */ scheduleJob1(scheduler); scheduleJob2(scheduler); } /** * 配置Job1 * 此處的任務(wù)可以配置可以放到properties或者是放到數(shù)據(jù)庫中 * 如果此時需要做到動態(tài)的定時任務(wù),請參考:http://blog.csdn.net/liuchuanhong1/article/details/60873295 * 博客中的ScheduleRefreshDatabase類 * @param scheduler * @throws SchedulerException */ private void scheduleJob1(Scheduler scheduler) throws SchedulerException{ /* * 此處可以先通過任務(wù)名查詢數(shù)據(jù)庫,如果數(shù)據(jù)庫中存在該任務(wù),則按照ScheduleRefreshDatabase類中的方法,更新任務(wù)的配置以及觸發(fā)器 * 如果此時數(shù)據(jù)庫中沒有查詢到該任務(wù),則按照下面的步驟新建一個任務(wù),并配置初始化的參數(shù),并將配置存到數(shù)據(jù)庫中 */ JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build(); // 每5s執(zhí)行一次 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執(zhí)行一次 CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1") .withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail,cronTrigger); } }
3、啟動兩個任務(wù)
@Configuration @EnableScheduling @Component public class SchedulerListener { @Autowired public SchedulerAllJob myScheduler; /** * 啟動的時候執(zhí)行該方法,或者是使用ApplicationListener,在啟動的時候執(zhí)行該方法 * 具體使用見:http://blog.csdn.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、測試結(jié)果如下
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……………………………………
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。