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

时间:2022-06-07 04:44:20

在spring boot中使用quartz时,在job中一般需要引用spring管理的bean,通过定义job factory实现自动注入。

spring有自己的schedule定时任务,在spring boot中使用的时候,不能动态管理job,于是就使用quartz来实现。

在spring boot中配置quartz:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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了

?
1
2
3
4
5
6
7
8
9
10
11
12
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);// 注入到管理类

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

原文链接:https://www.cnblogs.com/softidea/p/6073495.html