spring boot整合quartz实现多个定时任务

时间:2023-03-09 23:31:46
spring boot整合quartz实现多个定时任务
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.****.net/liuchuanhong1/article/details/78543574

最近收到了很多封邮件,都是想知道spring boot整合quartz如何实现多个定时任务的,由于本人生产上并没有使用到多个定时任务,这里给个实现的思路。

1、新建两个定时任务,如下:

  1. public class ScheduledJob implements Job{
  2. @Override
  3. public void execute(JobExecutionContext context) throws JobExecutionException {
  4. System.out.println("schedule job1 is running…………………………………… ");
  5. }
  6. }

  1. public class ScheduledJob2 implements Job {
  2. @Override
  3. public void execute(JobExecutionContext context) throws JobExecutionException {
  4. System.out.println("schedule job2 is running ……………………………………………………");
  5. }
  6. }

2、配置以上两个任务

  1. @Component
  2. public class SchedulerAllJob {
  3. @Autowired
  4. private SchedulerFactoryBean schedulerFactoryBean;
  5. /*
  6. * 此处可以注入数据库操作,查询出所有的任务配置
  7. */
  8. /**
  9. * 该方法用来启动所有的定时任务
  10. * @throws SchedulerException
  11. */
  12. public void scheduleJobs() throws SchedulerException {
  13. Scheduler scheduler = schedulerFactoryBean.getScheduler();
  14. /**
  15. *
  16. */
  17. scheduleJob1(scheduler);
  18. scheduleJob2(scheduler);
  19. }
  20. /**
  21. * 配置Job1
  22. * 此处的任务可以配置可以放到properties或者是放到数据库中
  23. * 如果此时需要做到动态的定时任务,请参考:http://blog.****.net/liuchuanhong1/article/details/60873295
  24. * 博客中的ScheduleRefreshDatabase类
  25. * @param scheduler
  26. * @throws SchedulerException
  27. */
  28. private void scheduleJob1(Scheduler scheduler) throws SchedulerException{
  29. /*
  30. * 此处可以先通过任务名查询数据库,如果数据库中存在该任务,则按照ScheduleRefreshDatabase类中的方法,更新任务的配置以及触发器
  31. * 如果此时数据库中没有查询到该任务,则按照下面的步骤新建一个任务,并配置初始化的参数,并将配置存到数据库中
  32. */
  33. JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();
  34. // 每5s执行一次
  35. CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
  36. CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
  37. scheduler.scheduleJob(jobDetail,cronTrigger);
  38. }
  39. /**
  40. * 配置Job
  41. * @param scheduler
  42. * @throws SchedulerException
  43. */
  44. private void scheduleJob2(Scheduler scheduler) throws SchedulerException{
  45. JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job2", "group1").build();
  46. CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
  47. // 每10s执行一次
  48. CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1") .withSchedule(scheduleBuilder).build();
  49. scheduler.scheduleJob(jobDetail,cronTrigger);
  50. }
  51. }

3、启动两个任务

  1. @Configuration
  2. @EnableScheduling
  3. @Component
  4. public class SchedulerListener {
  5. @Autowired
  6. public SchedulerAllJob myScheduler;
  7. /**
  8. * 启动的时候执行该方法,或者是使用ApplicationListener,在启动的时候执行该方法
  9. * 具体使用见:http://blog.****.net/liuchuanhong1/article/details/77568187
  10. * @throws SchedulerException
  11. */
  12. @Scheduled(cron="0 08 18 ? * *")
  13. public void schedule() throws SchedulerException {
  14. myScheduler.scheduleJobs();
  15. }
  16. @Bean
  17. public SchedulerFactoryBean schedulerFactoryBean(){
  18. SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
  19. return schedulerFactoryBean;
  20. }
  21. }

4、测试结果如下

  1. schedule job1 is running……………………………………
  2. schedule job2 is running ……………………………………………………
  3. schedule job1 is running……………………………………
  4. schedule job1 is running……………………………………
  5. schedule job2 is running ……………………………………………………
  6. schedule job1 is running……………………………………
  7. schedule job1 is running……………………………………
  8. schedule job2 is running ……………………………………………………
  9. schedule job1 is running……………………………………