最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例

本文将演示如何通过 Spring 使用 Quartz Scheduler 进行任务调度。Spring 为简化 Quartz 的操作提供了相关支持类。
本文示例使用的相关工具如下:
  • Spring 4.2.2 (发布于 2015 年 10 月)
  • Quartz Scheduler 2.2.2 (发布于 2015 年 10 月)
  • Maven 3
  • JDK 1.7
  • Eclipse Luna Service Release 1 (4.4.1)

步骤 1:新建 Maven 项目

最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例_第1张图片
这一步如果不知道怎么做可以参考博客《 使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目》。

步骤 2:第三方依赖包的引入

Maven pom.xml 编辑如下:

  4.0.0

  settle
  spring-quartz
  1.0.0
  jar
  spring-quartz
  http://maven.apache.org

	
		UTF-8
		4.2.2.RELEASE
		2.2.2
	

	
		
            org.springframework
            spring-core
            ${springframework.version}
        
        
            org.springframework
            spring-context-support
            ${springframework.version}
        
        
        
            org.springframework
            spring-tx
            ${springframework.version}
        
         
        
        
            org.quartz-scheduler
            quartz
            ${quartz.version}
        
	

步骤 3:Quartz Scheduler 配置作业

有两种方式在 Spring 中使用 Quartz 来配置一个作业。
A:使用 MethodInvokingJobDetailFactoryBean
这种方式在你想要调用特定 bean 的一个方法的时候很是方便,比另一种方法要简单的多。
    
    
        
        
    

以上作业配置简单调用了 myBeanprintMessage 方法, myBean 是一个简单的 POJO。
B:使用 JobDetailFactoryBean
如果你需要更高级的设置,需要给作业传递数据,想更加灵活的话就使用这种方式。
    
    
        
        
            
                
            
        
        
    

jobClass 关联到一个继承自 QuartzJobBean 的类,它实现了 Quartz 作业接口。调用到这个作业的时候,它的 executeInternal 将被执行。
jobDataMap 允许我们给相关作业 bean 传递一些数据。在这个例子里,我们将 ScheduledJob 将要使用到的 'anotherBean' 传递给它。
以下是引用 jobclass( FirstScheduledJob)的具体实现。
com.defonds.scheduler.jobs.FirstScheduledJob
/**
 * File Name:ScheduledJob.java
 *
 * Copyright Defonds Corporation 2015 
 * All Rights Reserved
 *
 */
package com.defonds.scheduler.jobs;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.defonds.scheduler.util.AnotherBean;

/**
 * 
 * Project Name:spring-quartz
 * Type Name:ScheduledJob
 * Type Description:
 * Author:Defonds
 * Create Date:2015-10-29
 * @version 
 * 
 */
public class FirstScheduledJob extends QuartzJobBean {
	
	private AnotherBean anotherBean;

	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		System.out.println("I am FirstScheduledJob");
		this.anotherBean.printAnotherMessage();

	}

	public void setAnotherBean(AnotherBean anotherBean) {
		this.anotherBean = anotherBean;
	}
}

步骤 4:配置 Quartz 调度时要使用到的触发器

触发器用来定义调度器何时将会执行你的调度任务的那个时间。有两种可能的触发器类型:
A:简单触发器,使用 SimpleTriggerFactoryBean
你可以定义作业的启动时间、触发器之间的延迟时间以及 repeatInterval(频率)。
    
    
        
        
        
    

B:计划触发器,使用 CronTriggerFactoryBean
这种类型更加灵活,允许你针对特定实例选择计划方案以及将来要执行的频率。
    
    
        
        
        
        
    

步骤 5:配置创建定配置 Quartz 调度器的 SchedulerFactoryBean

SchedulerFactoryBeanjobDetailstriggers 整合在一起以配置 Quartz 调度器
    
    
        
            
                
                
                
            
        
 
        
            
                
                
                
            
        
    

下面贴出我们示例的完整的上下文文件。
src/main/resources/quartz-context.xml


 
    
 
 
    
    
        
        
    
 
 
    
    
        
        
            
                
            
        
        
    
    
    
    
        
        
    
 
 
    
    
        
        
        
    
 
 
    
    
        
        
        
        
    
    
    
    
        
        
    
 
 
    
    
        
            
                
                
                
            
        
 
        
            
                
                
                
            
        
    
 

步骤 6:创建本文用到的几个简单 POJO 任务 Bean

com.defonds.scheduler.jobs.MyBean
/**
 * File Name:MyBean.java
 *
 * Copyright Defonds Corporation 2015 
 * All Rights Reserved
 *
 */
package com.defonds.scheduler.jobs;

import org.springframework.stereotype.Component;

/**
 * 
 * Project Name:spring-quartz
 * Type Name:MyBean
 * Type Description:
 * Author:Defonds
 * Create Date:2015-10-29
 * @version 
 * 
 */
@Component("myBean")
public class MyBean {
 
    public void printMessage() {
        System.out.println("I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean");
    }
     
}

com.defonds.scheduler.util.AnotherBean
/**
 * File Name:AnotherBean.java
 *
 * Copyright Defonds Corporation 2015 
 * All Rights Reserved
 *
 */
package com.defonds.scheduler.util;

import org.springframework.stereotype.Component;

/**
 * 
 * Project Name:spring-quartz
 * Type Name:AnotherBean
 * Type Description:
 * Author:Defonds
 * Create Date:2015-10-29
 * @version 
 * 
 */
@Component("anotherBean")
public class AnotherBean {
     
    public void printAnotherMessage(){
        System.out.println("I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean");
    }
     
}

com.defonds.scheduler.jobs.SecondScheduledJob
/**
 * File Name:SecondScheduledJob.java
 *
 * Copyright Defonds Corporation 2015 
 * All Rights Reserved
 *
 */
package com.defonds.scheduler.jobs;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * 
 * Project Name:spring-quartz
 * Type Name:SecondScheduledJob
 * Type Description:
 * Author:Defonds
 * Create Date:2015-10-29
 * @version 
 * 
 */
public class SecondScheduledJob extends QuartzJobBean {

	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		System.out.println("I am SecondScheduledJob");

	}

}

为了演示多个执行计划的一起调度,我们写了两个 JobDetailFactoryBean,于是就有了 SecondScheduledJob

步骤 7:创建执行程序的 Main 类

/**
 * File Name:AppMain.java
 *
 * Copyright Defonds Corporation 2015 
 * All Rights Reserved
 *
 */
package com.defonds.scheduler;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * Project Name:spring-quartz
 * Type Name:AppMain
 * Type Description:
 * Author:Defonds
 * Create Date:2015-10-29
 * @version 
 * 
 */
public class AppMain {

	public static void main(String args[]){
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("quartz-context.xml");
    }

}

这时整个项目目录结构如下图所示:
最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例_第2张图片
执行这个 Main 类,输出结果如下:
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am SecondScheduledJob
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am FirstScheduledJob
I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean
I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean

可以看到,简单触发器调用的作业每隔两秒执行一次,而计划触发器一的则是每隔五秒钟执行一次,计划触发器二则是固定只执行了一次(晚上八点整,红色字体部分)。

后记

  • 大多数情况下都会使用 JobDetailFactoryBean 进行任务调度配置;
  • 每个 JobDetailFactoryBean 都有一个触发器与之匹配。配置多个调度计划,需要配置多个这种匹配对;
  • 由触发器指向 JobDetailFactoryBean;

本文完整 Eclipse 下的开发项目示例已上传 CSDN 资源,有兴趣的朋友可以去下载下来参考: http://download.csdn.net/detail/defonds/9224893。

参考资料

  • Spring 4 + Quartz Scheduler Integration Example

你可能感兴趣的:(Spring框架)