查看: 248|回复: 0

[Java代码] Spring Boot集成Quartz注入Spring管理的类的方法

发表于 2018-5-4 08:00:02

在Spring Boot中使用Quartz时,在JOB中一般需要引用Spring管理的Bean,通过定义Job Factory实现自动注入。

Spring有自己的Schedule定时任务,在Spring boot中使用的时候,不能动态管理JOB,于是就使用Quartz来实现。

在Spring Boot中配置Quartz:

  1. import java.io.IOException;
  2. import java.util.Properties;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.config.PropertiesFactoryBean;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.core.io.ClassPathResource;
  8. import org.springframework.scheduling.annotation.EnableScheduling;
  9. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  10. @Configuration
  11. @EnableScheduling
  12. public class QuartzSchedule {
  13. @Autowired
  14. private MyJobFactory myJobFactory;
  15. @Bean
  16. public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
  17. SchedulerFactoryBean factory = new SchedulerFactoryBean();
  18. factory.setOverwriteExistingJobs(true);
  19. // 延时启动
  20. factory.setStartupDelay(20);
  21. // 加载quartz数据源配置
  22. factory.setQuartzProperties(quartzProperties());
  23. // 自定义Job Factory,用于Spring注入
  24. factory.setJobFactory(myJobFactory);
  25. return factory;
  26. }
  27. /**
  28. * 加载quartz数据源配置
  29. *
  30. * @return
  31. * @throws IOException
  32. */
  33. @Bean
  34. public Properties quartzProperties() throws IOException {
  35. PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
  36. propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
  37. propertiesFactoryBean.afterPropertiesSet();
  38. return propertiesFactoryBean.getObject();
  39. }
  40. }
复制代码

为了在JOB中使用Spring管理的Bean,需要重新定义一个Job Factory:

  1. @Component
  2. public class MyJobFactory extends AdaptableJobFactory {
  3. @Autowired
  4. private AutowireCapableBeanFactory capableBeanFactory;
  5. @Override
  6. protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
  7. // 调用父类的方法
  8. Object jobInstance = super.createJobInstance(bundle);
  9. // 进行注入
  10. capableBeanFactory.autowireBean(jobInstance);
  11. return jobInstance;
  12. }
  13. }
复制代码

然后在JOB中就可以使用Spring管理的Bean了

  1. public class MyJob implements Job, Serializable {
  2. private static final long serialVersionUID = 1L;
  3. private Logger logger = LoggerFactory.getLogger(this.getClass());
  4. @Autowired
  5. private SomeService someService;
  6. @Override
  7. public void execute(JobExecutionContext context) throws JobExecutionException {
  8. someService.doSomething();
  9. }
  10. }
复制代码

下面代码是创建JOB:

  1. JobDetail jobDetail = JobBuilder.newJob(((Job) Class.forName(job.getClazz()).newInstance()).getClass())
  2. .withIdentity(job.getJobName(), job.getJobGroup()).build();
  3. jobDetail.getJobDataMap().put("extdata", job.getExtData());
  4. // 表达式调度构建器
  5. CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression())
  6. .withMisfireHandlingInstructionDoNothing();
  7. // 构建一个trigger
  8. TriggerBuilder<CronTrigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerKey)
  9. .withSchedule(scheduleBuilder);
  10. if (job.getStartTime() != null) {
  11. triggerBuilder.startAt(job.getStartTime());
  12. }
  13. if (job.getEndTime() != null) {
  14. triggerBuilder.endAt(job.getEndTime());
  15. }
  16. CronTrigger trigger = triggerBuilder.build();
  17. scheduler.scheduleJob(jobDetail, trigger);// 注入到管理类
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报