当前位置: 首页 > news >正文

使用SchedulerFactoryBean集成Quarz Job与Spring

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

[list]
[*]Quartz Job官网:[url]http://quartz-scheduler.org[/url]
[*]Spring官网:[url]https://spring.io[/url]
[*][b]SchedulerFactoryBean Java Doc:[/b][url]http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html[/url]
[/list]
本次集成主要用到了Spring提供的
org.springframework.scheduling.quartz.SchedulerFactoryBean,该类使得Spring application context可以创建或管理Quartz的Scheduler,包括注册JobDetails、Calendars和Triggers等。
有了这个类,可以Retire掉org.quartz.ee.servlet.QuartzInitializerListener这个Listener。

[b][color=blue]注:这个类兼容Quartz 2.1.4及以上版本,Spring 4.1及以上版本。[/color][/b]


[b]1. 例子(使用Annotation):[/b]
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。


[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()方法,在里面做了很多事情,如:
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。
http://www.xdnf.cn/news/809443.html

相关文章:

  • Web 2.0(维基百科)
  • Oracle之主键(Primary Key)用法详解
  • Android ActivityManagerService总结(一)AMS启动
  • 智慧档案室一体化建设方案
  • Linux makefile详解
  • .NET Framework 3.5 SP1 最终文件下载及离线安装
  • 一站式Shell编程攻略:从入门到精通
  • java中用中国网建提供的SMS短信平台发送短信
  • 计算机网络基础知识(非常详细)从零基础入门到精通,看完这一篇就够了
  • 亚马逊开店详细教程(3)- 分配存款方式
  • Tomcat的webapps文件夹
  • FormulaR1C1是EXCEL中单元格公式输入方法
  • 40个在线杀毒网站
  • kb931125—rootsupd_kb931125-rootsupd补丁下载
  • 创业投资——IDG技术创业投资基金
  • JAVA安装教程
  • (转)FPE修改全教程1
  • VUE实现下一页的功能
  • java response.write_response.write()区别response.getWrite().write()
  • 简单卷、跨区卷、带区卷、镜像卷和 RAID-5 卷 区别
  • BitCome比特彗星v1.82豪华版(bt下载)
  • 外贸干货|最完整的外贸出口流程,收藏起来耐心看完!
  • 固态硬盘如何4K对齐?扇区大小,簇大小的影响_固态硬盘扇区
  • 自定义Android应用字体的完整指南
  • 香农编码,哈夫曼编码与费诺编码的比较
  • 嵌入式开发对学历门槛要求高吗?
  • X Window系统(X Window System,也常称为X11或X,天窗口系统)是一种以位图方式显示的软件窗口系统。
  • 一文读懂DDR内存基础知识|值得收藏
  • js中cloneNode()的使用 两个例子
  • API函数大全