springboot整合quartz
网上示例挺多的,主要注意以下几点吧:
1、quartz的主要核心就是由Scheduler类根据Triegger的任务时刻执行Job任务
2、springboot整合quartz主要核心就是能够让quartz能够在spring的管理范围内依赖注入,要不然直接写quartz多好,省时省力
一、基础配置
这一步主要是用来配置quartz框架的job工厂交由spring负责,可以依赖注入spring容器中的相关bean
先看一下基本的配置类情况吧
红框中的两个类主要就是为了将quartz框架中生成Job类实例交由spring的工厂类去生成,同时将所有quartz的配置使用原生的quartz.properties文件进行
两个类可以当做固定写法套用即可
QuartzConfiguration.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.io.IOException; import java.util.Properties; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.spi.JobFactory; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.quartz.SchedulerFactoryBean; /** * 配置相关的scheduler属性 */ @Configuration public class QuartzConfiguration { @Bean public JobFactory jobFactory(ApplicationContext applicationContext) { AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory(); jobFactory.setApplicationContext(applicationContext); return jobFactory; } //SchedulerFactoryBean @Bean public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory) throws IOException{ SchedulerFactoryBean factory = new SchedulerFactoryBean(); //把bean交给spring管理,这样Job就可以使用spring产生的bean factory.setJobFactory(jobFactory); factory.setQuartzProperties(quartzProperties()); return factory; } @Bean public Scheduler scheduler(SchedulerFactoryBean jobFactory) throws IOException{ Scheduler scheduler = jobFactory.getObject(); return scheduler; } /** * quartz的属性配置 * @return * @throws IOException */ @Bean public Properties quartzProperties() throws IOException { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject(); } }
AutowiringSpringBeanJobFactory.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import org.quartz.spi.TriggerFiredBundle; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.scheduling.quartz.SpringBeanJobFactory; /** * 用来保证job被spring容器托管,能够依赖注入 */ public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware { private transient AutowireCapableBeanFactory beanFactory; @Override public void setApplicationContext(final ApplicationContext context) { beanFactory = context.getAutowireCapableBeanFactory(); } @Override protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception { final Object job = super.createJobInstance(bundle); beanFactory.autowireBean(job); return job; } }
二、任务配置
接下来就是进行相关的job配置了,实现org.quartz.Job,依赖注入要用的相关属性或者实例bean,进行任务执行
最后就是进行任务初始化调度执行了,我这边用JobConfiguration类实现了,你们也可以使用其他方法或者将多个任务分开放在不同的类里面。
1 import java.util.Calendar; 2 3 import javax.annotation.PostConstruct; 4 5 import org.quartz.CronScheduleBuilder; 6 import org.quartz.CronTrigger; 7 import org.quartz.JobBuilder; 8 import org.quartz.JobDetail; 9 import org.quartz.Scheduler; 10 import org.quartz.SchedulerException; 11 import org.quartz.TriggerBuilder; 12 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.beans.factory.annotation.Value; 14 import org.springframework.stereotype.Service; 15 16 @Service 17 public class JobConfiguration { 18 19 @Autowired 20 private Scheduler scheduler; 21 22 @Value("${spring.screenshoot.cron}") 23 private String pictureShotCron; 24 @Value("${spring.dailydelete.cron}") 25 private String pictureDailyDeleteCron; 26 27 @PostConstruct 28 public void init() { 29 pictureShotTask(); 30 pictureDailyDeleteTask(); 31 } 32 33 public void pictureShotTask() { 34 //构建jobdetail 35 JobDetail jobDetail = JobBuilder.newJob(PictureCatchTask.class).withIdentity("pictureShotJob","pictureGroup").build(); 36 37 //构建trigger 38 //构建cron调度器 39 CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(pictureShotCron).withMisfireHandlingInstructionDoNothing(); 40 //构建cron触发器 41 //从整分钟开始 42 Calendar ca = Calendar.getInstance(); 43 ca.set(Calendar.MINUTE, 0); 44 ca.set(Calendar.SECOND, 0); 45 System.out.println(ca.getTime()); 46 CronTrigger trigger = TriggerBuilder.newTrigger() 47 .withIdentity("pictureShotTrigger","pictureGroup") 48 .startAt(ca.getTime()) 49 .withSchedule(cronSchedule).build(); 50 //组合运行 51 try { 52 scheduler.scheduleJob(jobDetail, trigger); 53 } catch (SchedulerException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } 57 } 58 59 public void pictureDailyDeleteTask() { 60 //构建jobdetail 61 JobDetail jobDetail = JobBuilder.newJob(PictureDailyDeleteTask.class).withIdentity("pictureDailyDeleteTask","pictureGroup").build(); 62 63 //构建trigger 64 //构建cron调度器 65 CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(pictureDailyDeleteCron).withMisfireHandlingInstructionDoNothing(); 66 //构建cron触发器 67 //从整分钟开始 68 Calendar ca = Calendar.getInstance(); 69 ca.set(Calendar.MINUTE, 0); 70 ca.set(Calendar.SECOND, 0); 71 System.out.println(ca.getTime()); 72 CronTrigger trigger = TriggerBuilder.newTrigger() 73 .withIdentity("pictureDailyDeleteTrigger","pictureGroup") 74 .startAt(ca.getTime()) 75 .withSchedule(cronSchedule).build(); 76 //组合运行 77 try { 78 scheduler.scheduleJob(jobDetail, trigger); 79 } catch (SchedulerException e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } 83 } 84 85 86 }
最后全权交给相父处理即可啊!
三、参考链接
附上一个主要的参考链接:https://www.jianshu.com/p/a99b80021be6