Spring Boot集成Quartz注入Spring管理的類的方法
在Spring Boot中使用Quartz時(shí),在JOB中一般需要引用Spring管理的Bean,通過定義Job Factory實(shí)現(xiàn)自動注入。
Spring有自己的Schedule定時(shí)任務(wù),在Spring boot中使用的時(shí)候,不能動態(tài)管理JOB,于是就使用Quartz來實(shí)現(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);
// 延時(shí)啟動
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 {
// 調(diào)用父類的方法
Object jobInstance = super.createJobInstance(bundle);
// 進(jìn)行注入
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());
// 表達(dá)式調(diào)度構(gòu)建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression())
.withMisfireHandlingInstructionDoNothing();
// 構(gòu)建一個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);// 注入到管理類
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 淺談SpringBoot集成Quartz動態(tài)定時(shí)任務(wù)
- SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例
- SpringBoot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)的方法
- Spring集成Quartz的簡單配置的方法
- 詳解Quartz 與 Spring框架集成的三種方式
- SpringBoot集成quartz實(shí)現(xiàn)定時(shí)任務(wù)詳解
- SpringBoot2.6.3集成quartz的方式
- Quartz與Spring集成的兩種方法示例
- springBoot項(xiàng)目集成quartz開發(fā)定時(shí)任務(wù)案例及注意事項(xiàng)
- 可視化定時(shí)任務(wù)quartz集成解析全過程
相關(guān)文章
java多線程之并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore
這篇文章主要為大家介紹了java并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12
教你一步解決java.io.FileNotFoundException:找不到文件異常
這篇文章主要給大家介紹了關(guān)于如何一步解決java.io.FileNotFoundException:找不到文件異常的相關(guān)資料,文中通過圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Mabatis錯誤提示Parameter index out of range的處理方法
這篇文章主要介紹了Mabatis錯誤提示Parameter index out of range 的處理方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
微信支付java版V3驗(yàn)證數(shù)據(jù)合法性(Deom)
這篇文章主要介紹了微信支付java版V3驗(yàn)證數(shù)據(jù)合法性(Deom)的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境
本文主要介紹了Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
如何使用SpEL表達(dá)式實(shí)現(xiàn)動態(tài)分表查詢
這篇文章主要介紹了如何使用SpEL表達(dá)式實(shí)現(xiàn)動態(tài)分表查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

