最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例
参考http://blog.csdn.net/defonds/article/details/49496895
本文将演示如何通过 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 项目
这一步如果不知道怎么做可以参考博客《使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目》。
步骤 2:第三方依赖包的引入
Maven pom.xml 编辑如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>settle</groupId>
- <artifactId>spring-quartz</artifactId>
- <version>1.0.0</version>
- <packaging>jar</packaging>
- <name>spring-quartz</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <springframework.version>4.2.2.RELEASE</springframework.version>
- <quartz.version>2.2.2</quartz.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <!-- Transaction dependency is required with Quartz integration -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <!-- Quartz framework -->
- <dependency>
- <groupId>org.quartz-scheduler</groupId>
- <artifactId>quartz</artifactId>
- <version>${quartz.version}</version>
- </dependency>
- </dependencies>
- </project>
步骤 3:Quartz Scheduler 配置作业
有两种方式在 Spring 中使用 Quartz 来配置一个作业。
A:使用 MethodInvokingJobDetailFactoryBean
这种方式在你想要调用特定 bean 的一个方法的时候很是方便,比另一种方法要简单的多。
- <!-- For times when you just need to invoke a method on a specific object -->
- <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject" ref="myBean" />
- <property name="targetMethod" value="printMessage" />
- </bean>
以上作业配置简单调用了 myBean 的 printMessage 方法,myBean 是一个简单的 POJO。
B:使用 JobDetailFactoryBean
如果你需要更高级的设置,需要给作业传递数据,想更加灵活的话就使用这种方式。
- <!-- For times when you need more complex processing, passing data to the scheduled job -->
- <bean name="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
- <property name="jobClass" value="com.defonds.scheduler.jobs.FirstScheduledJob" />
- <property name="jobDataMap">
- <map>
- <entry key="anotherBean" value-ref="anotherBean" />
- </map>
- </property>
- <property name="durability" value="true" />
- </bean>
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(频率)。
- <!-- Run the job every 2 seconds with initial delay of 1 second -->
- <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
- <property name="jobDetail" ref="simpleJobDetail" />
- <property name="startDelay" value="1000" />
- <property name="repeatInterval" value="2000" />
- </bean>
B:计划触发器,使用 CronTriggerFactoryBean
这种类型更加灵活,允许你针对特定实例选择计划方案以及将来要执行的频率。
- <!-- Run the job every 5 seconds -->
- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
- <property name="jobDetail" ref="firstComplexJobDetail" />
- <!--<property name="cronExpression" value="0/5 * * ? * SAT-SUN" />-->
- <property name="cronExpression" value="0/5 * * ? * *" />
- </bean>
步骤 5:配置创建定配置 Quartz 调度器的 SchedulerFactoryBean
SchedulerFactoryBean 将 jobDetails 和 triggers 整合在一起以配置 Quartz 调度器。
- <!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler -->
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="jobDetails">
- <list>
- <ref bean="simpleJobDetail" />
- <ref bean="firstComplexJobDetail" />
- <ref bean="secondComplexJobDetail" />
- </list>
- </property>
- <property name="triggers">
- <list>
- <ref bean="simpleTrigger" />
- <ref bean="cronTrigger" />
- <ref bean="secondCronTrigger" />
- </list>
- </property>
- </bean>
下面贴出我们示例的完整的上下文文件。
src/main/resources/quartz-context.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <context:component-scan base-package="com.defonds.scheduler" />
- <!-- For times when you just need to invoke a method on a specific object -->
- <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject" ref="myBean" />
- <property name="targetMethod" value="printMessage" />
- </bean>
- <!-- For times when you need more complex processing, passing data to the scheduled job -->
- <bean name="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
- <property name="jobClass" value="com.defonds.scheduler.jobs.FirstScheduledJob" />
- <property name="jobDataMap">
- <map>
- <entry key="anotherBean" value-ref="anotherBean" />
- </map>
- </property>
- <property name="durability" value="true" />
- </bean>
- <!-- Always run at 20:00 everyday -->
- <bean name="secondComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
- <property name="jobClass" value="com.defonds.scheduler.jobs.SecondScheduledJob" />
- <property name="durability" value="true" />
- </bean>
- <!-- Run the job every 2 seconds with initial delay of 1 second -->
- <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
- <property name="jobDetail" ref="simpleJobDetail" />
- <property name="startDelay" value="1000" />
- <property name="repeatInterval" value="2000" />
- </bean>
- <!-- Run the job every 5 seconds -->
- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
- <property name="jobDetail" ref="firstComplexJobDetail" />
- <!--<property name="cronExpression" value="0/5 * * ? * SAT-SUN" />-->
- <property name="cronExpression" value="0/5 * * ? * *" />
- </bean>
- <!-- Always run at 20:00 everyday -->
- <bean id="secondCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
- <property name="jobDetail" ref="secondComplexJobDetail" />
- <property name="cronExpression" value="0 0 20 ? * *" />
- </bean>
- <!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler -->
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="jobDetails">
- <list>
- <ref bean="simpleJobDetail" />
- <ref bean="firstComplexJobDetail" />
- <ref bean="secondComplexJobDetail" />
- </list>
- </property>
- <property name="triggers">
- <list>
- <ref bean="simpleTrigger" />
- <ref bean="cronTrigger" />
- <ref bean="secondCronTrigger" />
- </list>
- </property>
- </bean>
- </beans>
步骤 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");
- }
- }
这时整个项目目录结构如下图所示:
执行这个 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.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例的更多相关文章
- spring集成quartz scheduler
创建项目 有两种创建quart配置作业 1.使用MethodInvokingJobDetailFactoryBean Quartz Scheduler 配置作业(MethodInvokingJobD ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...
- spring boot1.0 集成quartz 动态配置定时任务
转载自 https://www.imooc.com/article/36278 一.Quartz简介了解 Quartz Quartz 是一个完全由 Java 编写的开源作业调度框架,为在 Java 应 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...
- (转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
http://blog.csdn.net/u010648555/article/details/60767633 当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的 ...
- Spring Boot集成quartz实现定时任务并支持切换任务数据源
org.quartz实现定时任务并自定义切换任务数据源 在工作中经常会需要使用到定时任务处理各种周期性的任务,org.quartz是处理此类定时任务的一个优秀框架.随着项目一点点推进,此时我们并不满足 ...
- Spring/Spring boot正确集成Quartz及解决@Autowired失效问题
周五检查以前Spring boot集成Quartz项目的时候,发现配置错误,因此通过阅读源码的方式,探索Spring正确集成Quartz的方式. 问题发现 检查去年的项目代码,发现关于QuartzJo ...
- Spring Boot 入门(九):集成Quartz定时任务
本片文章续<Spring Boot 入门(八):集成RabbitMQ消息队列>,关于Quartz定时任务请参考<Quartz的基本使用之入门(2.3.0版本)> spring ...
- Spring集成Quartz定时器
<!-- Spring集成Quartz开始 --> <bean id="startQuertz" lazy-init="false" auto ...
随机推荐
- flexigrid
一.参考资料 1.jQuery插件flexiGrid的完全使用,附代码下载 2.修改flexigrid源码一(json,checkbox)[原创] 3.jQuery +UI + flexigrid做的 ...
- 快学scala-第七章 包和引入
知识点: 1. Scala.Java.C++的包的目的都是为了管理大型程序中的名称.与对象或类的定义不同,同一个包可以定义在多个文件当中.或者在同一个文件中,为多个包贡献内容. 2. Scala的包和 ...
- [转]Java初始化顺序总结 - 静态变量、静态代码块、成员变量、构造函数
Java初始化顺序1在new B一个实例时首先要进行类的装载.(类只有在使用New调用创建的时候才会被java类装载器装入)2,在装载类时,先装载父类A,再装载子类B3,装载父类A后,完成静态动作(包 ...
- 《割绳子》《蜡笔物理学》《Contre Jour》《顽皮鳄鱼爱洗澡》等游戏用Box2D引擎实现物理部分的方法(转)
从最热门游戏排行榜和flash游戏网站上,你能看到什么?许多2D游戏都有非常出色的物理学和美术设计.现在我们要学习那些游戏使用了什么物理学以及如何用Box2D制作它们. 除了知道是“什么”,更重要的是 ...
- Android真机连接手机Target显示unknown cmd命令下adb devices 显示offline
主要原因是adb版本的问题: 1.找到adb.exe路径 比如:G:\BaiduYunDownload\Android\android-sdk\platform-tools,将该路径放到环境便利-- ...
- SGU_390_Tickets(另类数位DP)
Tickets Time Limit : 1000/500ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) Total Subm ...
- input text设置字体
控件里设置: style="font-family:Arial" html里设置 <font face="Arial">
- jsp如果超过字数就变成...
<script> var infoTitle = '<ww:property value="infoTitle"/>'; if(infoTitle.leng ...
- this.button1.Click += new System.EventHandler(this.button1_Click);
在这个程序里,这里的修改是为了更好理解,当然这种写法是语法错误的. 下面我们对其进行分析: 首先,观察“=”右面的表达式. new System.EventHandler(this.button1_C ...
- 深入理解setTimeout和setinterval
以前一直以为这两个函数就是简单了认为类似thread一样的东西, 认为会在一个时间片内, 并发的执行调用的函数, 似乎很好很强大, 但其实并不是如此, 实际的情况是javascript都是以单线程的方 ...