ITeye博客

使用SchedulerFactoryBean集成Quarz Job与Spring

angelbill3 · 2017-04-30 21:32 · 企业架构 · Markdown

本文介绍Quartz Job与Spring的集成。

Quartz Job官网:http://quartz-scheduler.org Spring官网:https://spring.io SchedulerFactoryBean Java Doc:http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html

本次集成主要用到了Spring提供的 org.springframework.scheduling.quartz.SchedulerFactoryBean,该类使得Spring application context可以创建或管理Quartz的Scheduler,包括注册JobDetails、Calendars和Triggers等。 有了这个类,可以Retire掉org.quartz.ee.servlet.QuartzInitializerListener这个Listener。

注:这个类兼容Quartz 2.1.4及以上版本,Spring 4.1及以上版本。

1. 例子(使用Annotation): quartz_jobs.xml:略 quartz.properties:略


import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
@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;  
    }  
}

实现AdaptableJobFactory接口的JobFactory类,并重写createJobInstance方法。


import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class ComponentFactory {

	@Bean
	public SchedulerFactoryBean getSchedulerFactoryBean(JobFactory myJobFactory) throws Exception {
		SchedulerFactoryBean bean = new SchedulerFactoryBean();
		bean.setJobFactory(myJobFactory);
		bean.setSchedulerName("myscheduler");
		Properties quartzProperties = new Properties();
		quartzProperties.load(this.getClass().getResourceAsStream("/quartz.properties"));
		bean.setQuartzProperties(quartzProperties);
		return 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。

2. 源码分析: 先是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()方法,在里面做了很多事情,如: a. 创建了SchedulerFactory b. 创建Scheduler c. 如果有jobFactory属性,那么set d. 注册Scheduler、Job、Trigger的监听器listener(如果有定义的话) e. 注册Job和Trigger

此外,我们对于Quartz Job的参数设定,也是通过SchedulerFactoryBean类来实现的,以下是该类的一些常用属性:

public static final int DEFAULT_THREAD_COUNT = 10; 默认线程数为10。 private String schedulerName; Scheduler的名字,若没有定义则默认用bean的名称(name)。 private Resource configLocation; Quartz的配置如quartz.properties的存放位置,若是在xml中配置,则可以写成。 private Properties quartzProperties; 若是使用Annotation来定义bean,那么初始化quartz.properties可以用bean.setQuartzProperties(Properties)。 private JobFactory jobFactory; 注入一个JobFactory对象。

介绍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。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。