在Spring Boot中使用Quartz時,在JOB中一般需要引用Spring管理的Bean,通過定義Job Factory實現(xiàn)自動注入。
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站制作、做網(wǎng)站、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務和政,十余年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220
Spring有自己的Schedule定時任務,在Spring boot中使用的時候,不能動態(tài)管理JOB,于是就使用Quartz來實現(xiàn)。
在Spring Boot中配置Quartz:
import java.io.IOException; import java.util.Properties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.quartz.SchedulerFactoryBean; @Configuration @EnableScheduling public class QuartzSchedule { @Autowired private MyJobFactory myJobFactory; @Bean public SchedulerFactoryBean schedulerFactoryBean() throws IOException { SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setOverwriteExistingJobs(true); // 延時啟動 factory.setStartupDelay(20); // 加載quartz數(shù)據(jù)源配置 factory.setQuartzProperties(quartzProperties()); // 自定義Job Factory,用于Spring注入 factory.setJobFactory(myJobFactory); return factory; } /** * 加載quartz數(shù)據(jù)源配置 * * @return * @throws IOException */ @Bean public Properties quartzProperties() throws IOException { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject(); } }
為了在JOB中使用Spring管理的Bean,需要重新定義一個Job Factory:
@Component public class MyJobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { // 調用父類的方法 Object jobInstance = super.createJobInstance(bundle); // 進行注入 capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }
然后在JOB中就可以使用Spring管理的Bean了
public class MyJob implements Job, Serializable { private static final long serialVersionUID = 1L; private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SomeService someService; @Override public void execute(JobExecutionContext context) throws JobExecutionException { someService.doSomething(); } }
下面代碼是創(chuàng)建JOB:
JobDetail jobDetail = JobBuilder.newJob(((Job) Class.forName(job.getClazz()).newInstance()).getClass()) .withIdentity(job.getJobName(), job.getJobGroup()).build(); jobDetail.getJobDataMap().put("extdata", job.getExtData()); // 表達式調度構建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()) .withMisfireHandlingInstructionDoNothing(); // 構建一個trigger TriggerBuilder<CronTrigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(scheduleBuilder); if (job.getStartTime() != null) { triggerBuilder.startAt(job.getStartTime()); } if (job.getEndTime() != null) { triggerBuilder.endAt(job.getEndTime()); } CronTrigger trigger = triggerBuilder.build(); scheduler.scheduleJob(jobDetail, trigger);// 注入到管理類
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文題目:SpringBoot集成Quartz注入Spring管理的類的方法
文章URL:http://www.rwnh.cn/article12/jeeedc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、外貿網(wǎng)站建設、軟件開發(fā)、網(wǎng)站設計公司、自適應網(wǎng)站、微信小程序
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)