`
angelbill3
  • 浏览: 252998 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

使用SchedulerFactoryBean集成Quarz Job与Spring

阅读更多
本文介绍Quartz Job与Spring的集成。


本次集成主要用到了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中配置,则可以写成<property name="configLocation" value="classpath:quartz.properties"/>。
  • 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。
分享到:
评论

相关推荐

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring 2.0 开发参考手册

    17. 使用Spring进行远程访问与Web服务 17.1. 简介 17.2. 使用RMI暴露服务 17.2.1. 使用 RmiServiceExporter 暴露服务 17.2.2. 在客户端链接服务 17.3. 使用Hessian或者Burlap通过HTTP远程调用服务 17.3.1. 为...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    java发送邮件(spring+模版)

    1 使用SchedulerFactoryBean+CronTriggerBean的Spring Quarter动态设置触发时间时。 2 使用velocityEngine模版引擎,获取模版信息。 3 JavaMailSenderImpl+SimpleMailMessage+BodyPart+Multipart:获得模版邮件body...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    17. 使用Spring进行远程访问与Web服务 17.1. 简介 17.2. 使用RMI暴露服务 17.2.1. 使用 RmiServiceExporter 暴露服务 17.2.2. 在客户端链接服务 17.3. 使用Hessian或者Burlap通过HTTP远程调用服务 17.3.1. 为...

    springjdbc

    springmvc 框架整合 ... &lt;bean id="sfb2" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;ref bean="querytestControllerTrigger" /&gt; &lt;/list&gt; &lt;/property&gt; &lt;/beans&gt;

    spring定时任务

    spring定时任务 xmlns="http://www.springframework.org/schema/beans" ...&lt;bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;/beans&gt;

    QuartzSpring

    org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean org.springframework.scheduling.quartz.CronTriggerBean org.springframework.scheduling.quartz.SchedulerFactoryBean spring中的...

    Spring3.2.4+Quartz2.2.0 Demo

    class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;!-- 启动触发器的配置结束 --&gt; &lt;!-- 调度的配置开始 --&gt; &lt;!-- quartz-1.8以前的配置 class="org.spring...

    封装通用的Spring3+Struts2+MyBatis3的CRUD+条件分页查询,Spring+Quartz调度,FunctionCharts图像化工具

    封装通用的Spring3+Struts2+MyBatis3的CRUD+条件分页查询,... &lt;bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;/beans&gt;

    spring定时器轻松搞定

    class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="triggers"&gt; &lt;list&gt; &lt;ref local="electriTrigger" /&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; 我用的spring.jar,有些低版本的...

    SPRING API 2.0.CHM

    All Classes AbstractAdvisorAutoProxyCreator AbstractApplicationContext AbstractApplicationEventMulticaster AbstractAspectJAdvice AbstractAspectJAdvisorFactory AbstractAspectJAdvisorFactory....

    quartz 定时任务

    &lt;beans xmlns="http://www.springframework.org/schema/beans" ... xmlns:context=... &lt;bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;/beans&gt;

    Spring2.0+quartz1.8定时执行任务内含Cron表达式生成器

    现有需求需要每天早上2点和下午5点执行一个方法Synchronization.run(): &lt;!-- 调用频率设置 每天上午2点和下午5点 ... class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;/bean&gt;

    QuartzManager

    /** * @Description: 添加一个定时任务 ... schedulerFactoryBean.getScheduler().scheduleJob(jobDetail, trigger); startSched(); } catch (Exception e) { throw new RuntimeException(e); } }

    java quartz任务示例

    java quartz任务示例,只能用炫酷来形容,感兴趣就下载看看吧

    Scene切换与scheduler实例

    代码实现一个类似微信的登录界面,工程是Cocos2d-x3.1工程,将Class文件夹与Resource文件夹替换3.1工程下的文件,编译运行即可

    MethodInvokingJobDetailFactoryBean.java

    class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="applicationContextSchedulerContextKey" value="applicationContext"&gt;&lt;/property&gt; ...

    quartz-hipster-entities

    您只需要添加带有库的jar文件,并且Spring上下文中具有SchedulerFactoryBean。 库提供了简单的Rest API,可用于从数据库检索数据并将其作为JSON返回如何好的,但是如何使用呢? 您唯一需要做的就是将库添加为依赖项...

Global site tag (gtag.js) - Google Analytics