前言
Quartz是一个开源的定时调度框架,支持集群部署。我们可以通过其Java API来使用它,或者通过Spring来配置与管理,也可以结合使用两种方式。本文重点分析Quartz2.2.3与Spring4.3.0.RELEASE集成时的初始化过程。
SchedulerFactoryBean
与Spring集成时通常需要在Spring配置文件中加入SchedulerFactoryBean这个工厂Bean,例如: <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="overwriteExistingJobs" value="true"/>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>
再来看看SchedulerFactoryBean的类定义:
public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBean<Scheduler>, BeanNameAware,
ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle {
从中看到其实现了FactoryBean、BeanNameAware、ApplicationContextAware、InitializingBean、DisposableBean等常用接口,这些接口的具体意义本文不作赘述,不了解的可以专门研究下Spring的原理和源码实现。根据Spring的原理我们知道,如果Bean本身实现了InitializingBean接口,那么在Spring加载解析BeanDefinition,并初始化Bean后会调用SchedulerFactoryBean的afterPropertiesSet方法,这里只会挑出其中的关键代码进行分析。
初始化SchedulerFactory
在afterPropertiesSet中首先会初始化SchedulerFactory,代码如下:
// Create SchedulerFactory instance...
SchedulerFactory schedulerFactory = BeanUtils.instantiateClass(this.schedulerFactoryClass);
initSchedulerFactory(schedulerFacto