网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringBoot集成Quartz注入Spring管理的类的方法

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

成都创新互联主要从事成都网站制作、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务和政,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

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
@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数据源配置
    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:

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

下面代码是创建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 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);// 注入到管理类 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网站栏目:SpringBoot集成Quartz注入Spring管理的类的方法
当前路径:http://bjjierui.cn/article/jeeedc.html

其他资讯