国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Spring Boot集成Quartz注入Spring管理的類的方法

2024-07-14 08:40:15
字體:
來源:轉載
供稿:網友

在Spring Boot中使用Quartz時,在JOB中一般需要引用Spring管理的Bean,通過定義Job Factory實現自動注入。

Spring有自己的Schedule定時任務,在Spring boot中使用的時候,不能動態管理JOB,于是就使用Quartz來實現。

在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@EnableSchedulingpublic class QuartzSchedule {  @Autowired  private MyJobFactory myJobFactory;  @Bean  public SchedulerFactoryBean schedulerFactoryBean() throws IOException {    SchedulerFactoryBean factory = new SchedulerFactoryBean();    factory.setOverwriteExistingJobs(true);    // 延時啟動    factory.setStartupDelay(20);    // 加載quartz數據源配置    factory.setQuartzProperties(quartzProperties());    // 自定義Job Factory,用于Spring注入    factory.setJobFactory(myJobFactory);    return factory;  }  /**   * 加載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();  }}

為了在JOB中使用Spring管理的Bean,需要重新定義一個Job Factory:

 @Componentpublic 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();  }} 

下面代碼是創建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);// 注入到管理類 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永寿县| 久治县| 合山市| 临颍县| 漳平市| 南澳县| 昆明市| 威海市| 镇坪县| 常山县| 体育| 高州市| 客服| 金塔县| 西丰县| 凌源市| 托克逊县| 都江堰市| 合水县| 项城市| 新野县| 大丰市| 阜宁县| 贵州省| 海城市| 巴青县| 静海县| 喀什市| 云梦县| 黄骅市| 巫山县| 库伦旗| 庐江县| 崇州市| 辰溪县| 南江县| 白城市| 宜春市| 张北县| 阜城县| 和林格尔县|