最新 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 ...
随机推荐
- TriggerPrefab 拖拽物体
模拟经营类游戏 有一个特点,就是 拖拽物体.常见的有<帝国><红警><部落战争><凯撒大帝>等等 2d 拖拽 大部分都是 用 OnDrag 方法来 拖动 ...
- HDU 1532 Drainage Ditches (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
- pat L1-006. 连续因子
L1-006. 连续因子 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个正整数N的因子中可能存在若干连续的数字.例如630 ...
- POJ 1365 Prime Land(整数拆分)
题意:感觉题意不太好懂,题目并不难,就是给一些p和e,p是素数,e是指数,然后把这个数求出来,设为x,然后让我们逆过程输出x-1的素数拆分形式,形式与输入保持一致. 思路:素数打表以后正常拆分即可. ...
- HDU 2675 Equation Again
公式转化+二分答案 首先,把题目中给的等式转化一下,变成了这个样子. 等式右边的值是可以求出来的. ln(x)/x的大致图像是这样的 那么只要对[0,e]和[e,+∞]分别进行二分答案即可. #inc ...
- ArrayList遍历的同时删除--- 删除还是用迭代器的比较好,其它的都会有问题.
http://javag.iteye.com/blog/403097 ArrayList遍历的同时删除-- 删除还是用迭代器的比较好,其它的都会有问题. 博客分类: 随笔 Javathread ...
- IIS判断W3WP进程对应哪个网站
IIS 6 (Win2003 )中查看某个应用程序池对应那个 W3WP.exe 进程,可以使用如下命令,输出结果类似如下: C:\WINDOWS\system32>cscript iisapp. ...
- 关于submit与document.form1.submit();这2个提交的区别
首先要知道 一个是按钮提交 一个是在js函数里写代码 document.form1.submit() 提交 区别如下: 从使的方式及效主要有二点区别吧.一.使用submit()提交时,表单中不能存在s ...
- 设置div,td失去焦点
$(downobj).attr({"tabindex":"0"});还需要设置css样式outline:none;
- SDAU课程练习--problemC
题目描述 Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji ...