本文介绍了Quartz Job与Spring的集成。
[列表]
[*]Quartz Job官网: [ URL ] http://quartz-scheduler.org [/URL ]
[*]Spring官网:“URL”https://spring.io [/URL ]
[ * ] [ b ] schedulerfactorybeanjavadoc:[/b ] [ URL ] http://docs.spring.io/spring -框架/docs/current/javavadoc
[/list]
此次整合主要由Spring提供
org.spring framework.scheduling.quartz.schedulerfactorybean。 使用此类可以创建或管理quartz计划程序,包括jobdeeler注册
有了这个类,Retire可以降级名为org.quartz.ee.servlet.quartzinitializerlistener的监听器。
[b][color=blue]注:此类与Quartz 2.1.4或更高版本、Spring 4.1或更高版本兼容。 [/color][/b]
[b]1.示例(使用注释) ()/b ) )。
quartz_jobs.xml :略
quartz.properties :略
importorg.quartz.SPI.triggerfiredbundle;
importorg.spring framework.beans.factory.annotation.auto wired;
importorg.spring framework.beans.factory.config.autowirecapablebeanfactory;
importorg.spring framework.scheduling.quartz.adaptablejobfactory;
importorg.spring framework.stereotype.com ponent;
@Component
publicclassmyjobfactoryextendsadaptablejobfactory {
@Autowired
私有autowirecapablebeanfactorycapablebeanfactory;
@Override
protectedobjectcreatejobinstance (triggerfiredbundlebundle ) throws Exception {
objectjobinstance=super.create job instance;
capable beanfactory.auto wire bean (作业实例;
返回作业实例;
}
}
实现AdaptableJobFactory接口的JobFactory类并重写createJobInstance方法。
import java.util.Properties;
importorg.spring帧work.context.annotation.bean;
importorg.spring framework.scheduling.quartz.schedulerfactorybean;
importorg.spring framework.stereotype.com ponent;
@Component
公共类组件工厂{
@Bean
publicschedulerfactorybeangetschedulerfactorybean (jobfactorymyjobfactory ) throws Exception {
schedulerfactorybeanbean=newschedulerfactorybean (;
bean.setjobfactory (myjob factory;
bean.setschedulername (my scheduler );
propertiesquartzproperties=new properties (;
quartz properties.load (this.getclass (.getresourceasstream ) (/quartz.properties ) );
bean.setquartzproperties (quartz properties );
返回bean;
}
}
定义bean: schedulerFactoryBean。
public class DumpJob implements Job {
@Autowired
private ServiceA serviceA;
public void execute(JobExecutionContext context) throws JobExecutionException {
assertNotNull("Service should be injected.", serviceA);
}
}
定义一个Job,注入一个Service进行Test。
[b]2. 源码分析:[/b]
先是SchedulerFactoryBean类:
public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBean<Scheduler>,
BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle {
// Implementation of InitializingBean interface
@Override
public void afterPropertiesSet() throws Exception {
// 节选
// Create SchedulerFactory instance...
SchedulerFactory schedulerFactory = BeanUtils.instantiateClass(this.schedulerFactoryClass);
initSchedulerFactory(schedulerFactory);
}
}
首先看这个类的接口,实现了InitializingBean(该接口只定义了一个方法,叫afterPropertiesSet(),看源码知,SchedulerFactoryBean重写了afterPropertiesSet()方法,dcdcc做了很多事情,如:
a. 创建了SchedulerFactory
b. 创建Scheduler
c. 如果有jobFactory属性,那么set
d. 注册Scheduler、Job、Trigger的监听器listener(如果有定义的话)
e. 注册Job和Trigger
此外,我们对于Quartz Job的参数设定,也是通过SchedulerFactoryBean类来实现的,以下是该类的一些常用属性:
[list]
[*]public static final int DEFAULT_THREAD_COUNT = 10; 默认线程数为10。
[*]private String schedulerName; Scheduler的名字,若没有定义则默认用bean的名称(name)。
[*]private Resource configLocation; Quartz的配置如quartz.properties的存放位置,若是在xml中配置,则可以写成<property name="configLocation" value="classpath:quartz.properties"/>。
[*]private Properties quartzProperties; 若是使用Annotation来定义bean,那么初始化quartz.properties可以用bean.setQuartzProperties(Properties)。
[*]private JobFactory jobFactory; 注入一个JobFactory对象。
[/list]
介绍org.quartz.spi.JobFactory:
public interface JobFactory {
Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException;
}
介绍:org.springframework.scheduling.quartz.AdaptableJobFactory:
public class AdaptableJobFactory implements JobFactory {
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
return bundle.getJobDetail().getJobClass().newInstance();
}
}
若我们生成一个类实现AdaptableJobFactory,Job在实例化的时候都会调用AdaptableJobFactory#createJobInstance(),在上面的自定义MyJobFactory中重写了该方法:
a. 获得当前的jobInstance实例。
b. 利用AutowireCapableBeanFactory,将job实例设置为auto wired bean。
AutowireCapableBeanFactory是一个继承了BeanFactory的接口,虽然是BeanFacoty的子接口,但名气远没有ListableBeanFactory大(ApplicationContext的父接口)~ 这个类的主要功能就是将ApplicationContext之外的一些instance实例加入到Spring Application上下文中。如将JobInstance加入进来。
该类获取方式:org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
这就是为什么我们可以直接在MyJobFacoty中使用该bean。