欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-26 19:50:52
在spring boot中使用quartz时,在job中一般需要引用spring管理的bean,通过定义job factory实现自动注入。 spring有自己的sc...

在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
@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<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);// 注入到管理类 

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