发布时间:2025-12-09 13:57:33 浏览次数:4
Quartz是一个开源的定时调度框架,支持集群部署。我们可以通过其Java API来使用它,或者通过Spring来配置与管理,也可以结合使用两种方式。本文重点分析Quartz2.2.3与Spring4.3.0.RELEASE集成时的初始化过程。
与Spring集成时通常需要在Spring配置文件中加入SchedulerFactoryBean这个工厂Bean,例如:
<bean > <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方法,这里只会挑出其中的关键代码进行分析。
在afterPropertiesSet中首先会初始化SchedulerFactory,代码如下:
// Create SchedulerFactory instance...SchedulerFactory schedulerFactory = BeanUtils.instantiateClass(this.schedulerFactoryClass);initSchedulerFactory(schedulerFactory);属性schedulerFactoryClass的默认值是StdSchedulerFactory.class,因此这里默认会初始化StdSchedulerFactory,用户也可以使用Spring的配置文件修改schedulerFactoryClass的值为其他SchedulerFactory接口的实现(比如RemoteScheduler或者继承RemoteMBeanScheduler的子类)。在使用Spring的BeanUtils工具类对SchedulerFactory实例化后,调用initSchedulerFactory方法(见代码清单1)对SchedulerFactory初始化。
代码清单1 初始化SchedulerFactory
private void initSchedulerFactory(SchedulerFactory schedulerFactory) throws SchedulerException, IOException {if (!(schedulerFactory instanceof StdSchedulerFactory)) {if (this.configLocation != null || this.quartzProperties != null ||this.taskExecutor != null || this.dataSource != null) {throw new IllegalArgumentException("StdSchedulerFactory required for applying Quartz properties: " + schedulerFactory);}// Otherwise assume that no initialization is necessary...return;}Properties mergedProps = new Properties();if (this.resourceLoader != null) {mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_CLASS_LOAD_HELPER_CLASS,ResourceLoaderClassLoadHelper.class.getName());}if (this.taskExecutor != null) {mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,LocalTaskExecutorThreadPool.class.getName());}else {// Set necessary default properties here, as Quartz will not apply// its default configuration when explicitly given properties.mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());mergedProps.setProperty(PROP_THREAD_COUNT, Integer.toString(DEFAULT_THREAD_COUNT));}if (this.configLocation != null) {if (logger.isInfoEnabled()) {logger.info("Loading Quartz config from [" + this.configLocation + "]");}PropertiesLoaderUtils.fillProperties(mergedProps, this.configLocation);}CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);if (this.dataSource != null) {mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());}// Make sure to set the scheduler name as configured in the Spring configuration.if (this.schedulerName != null) {mergedProps.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);}((StdSchedulerFactory) schedulerFactory).initialize(mergedProps);}仔细阅读initSchedulerFactory方法,可以理解其初始化过程如下:
代码清单2StdSchedulerFactory的initialize实现
public void initialize(Properties props) throws SchedulerException { if (propSrc == null) { propSrc = "an externally provided properties instance."; } this.cfg = new PropertiesParser(props); }在SchedulerFactory初始化完成后,还会执行代码清单3中代码,其步骤归纳如下:
this.scheduler = createScheduler(schedulerFactory, this.schedulerName);populateSchedulerContext();if (!this.jobFactorySet && !(this.scheduler instanceof RemoteScheduler)) {// Use AdaptableJobFactory as default for a local Scheduler, unless when// explicitly given a null value through the "jobFactory" bean property.this.jobFactory = new AdaptableJobFactory();}if (this.jobFactory != null) {if (this.jobFactory instanceof SchedulerContextAware) {((SchedulerContextAware) this.jobFactory).setSchedulerContext(this.scheduler.getContext());}this.scheduler.setJobFactory(this.jobFactory);} //省略次要代码registerListeners();registerJobsAndTriggers(); protected void registerListeners() throws SchedulerException {ListenerManager listenerManager = getScheduler().getListenerManager();if (this.schedulerListeners != null) {for (SchedulerListener listener : this.schedulerListeners) {listenerManager.addSchedulerListener(listener);}}if (this.globalJobListeners != null) {for (JobListener listener : this.globalJobListeners) {listenerManager.addJobListener(listener);}}if (this.globalTriggerListeners != null) {for (TriggerListener listener : this.globalTriggerListeners) {listenerManager.addTriggerListener(listener);}}}对于熟悉Java的开发人员而言,任何Java对象(基本对象和复合对象)在使用之前都需要初始化,Quartz作为一个大的组件,其本身也是一个对象。从SchedulerFactoryBean的类定义中,我们可以看到其充分利用了Spring提供的各种扩展接口,以便于在调度上下文中使用Spring支持的丰富功能。在SchedulerFactory的初始化过程中,我们看到SchedulerFactoryBean支持多种注入属性,而且这些属性可以覆盖内置的属性设置,使用者可以根据自身需要进行配置。另外通过configLocation属性指定属性文件,可以在单独的属性文件中配置属性,当要配置的属性很多时,可以避免xml配置臃肿。添加对调度、作业及触发器等内容的监听器添加,以便于感兴趣的组件,在以上内容发生变化时,进行一些操作。这种方式也能够将其他组件与SchedulerFactoryBean之间的关系进行解耦。
后记:个人总结整理的《深入理解Spark:核心思想与源码分析》一书现在已经正式出版上市,目前京东、当当、天猫等网站均有销售,欢迎感兴趣的同学购买。
京东:http://item.jd.com/11846120.html
当当:http://product.dangdang.com/23838168.html