Quartz在Spring中的集成与应用
一、Quqrtz简介
Quartz是一个完全由Java编写的开源任务调度的框架,通过触发器设置作业定时运行规则,控制作业的运行时间。主要用来执行定时任务,如:定时发送信息、定时生成报表等等。
简而言之,Quartz是一个定时器组件,是可以整合Spring使用的一个定时器。
二、Quqrtz的配置文件
1.在maven中引入:
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.3.RELEASE</version> </dependency>
2.Spring-quqrtz.xml(以每天00:01定时执行一个任务为例):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- Quartz配置文件--> <!-- 定义执行任务:class为要执行任务的类的全限定名 --> <bean id="Job" class="com.**.XXXController"/> <!-- 定义JobDetail(任务详情) --> <bean id="JobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 执行任务的Bean类--> <property name="targetObject" ref="Job"/> <!-- 执行任务的方法 --> <property name="targetMethod" value="具体执行任务的方法名称"/> <!-- 是否允许多个任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程--> <property name="concurrent" value="false"/> </bean> <!-- 定义Scheduler调度器--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="Trigger" /> </list> </property> <!-- 是否自动启动 --> <property name="autoStartup" value="true" /> </bean> <!-- 定义Trigger(触发的时间) --> <bean id="Trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="JobDetail" /> <!-- 每天00:01开始执行--> <property name="cronExpression" value="0 1 0 * * ?" /> </bean> </beans>
(1)Cron表达式的格式:秒分时日 月 周年(可选)。 三、cron表达式
字段名
允许的值
允许的特殊字符
秒
0-59
, - * /
分
0-59
, - * /
小时
0-23
, - * /
日
1-31
, - * ? / L W C
月
1-12 or JAN-DEC
, - * /
周几
1-7 or SUN-SAT
, - * ? / L C #
年 (可选字段)
empty, 1970-2099
, - * /
特殊字符说明:
“?”字符
表示不确定的值
“,”字符
指定数个值
“-”字符
指定一个值的范围
“/”字符
指定一个值的增加幅度。n/m表示从n开始,每次增加m
“L”字符
用在日表示一个月中的最后一天,用在周表示该月最后一个星期X
“W”字符
指定离给定日期最近的工作日(周一到周五)
“#”字符
表示该月第几个周X。6#3表示该月第3个周五
(2)Cron表达式范例。
表达式
含义
0 0 12 * * ?
每天中午12点触发
0 15 10 ? * *
每天上午10:15触发
0 15 10 * * ?
每天上午10:15触发
0 15 10 * * ? *
每天上午10:15触发
0 15 10 * * ? 2005
2005年的每天上午10:15触发
0 * 14 * * ?
在每天下午2点到下午2:59期间的每1分钟触发
0 0/5 14 * * ?
在每天下午2点到下午2:55期间的每5分钟触发
0 0/5 14,18 * * ?
在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
0 0-5 14 * * ?
在每天下午2点到下午2:05期间的每1分钟触发
0 10,44 14 ? 3 WED
每年三月的星期三的下午2:10和2:44触发
0 15 10 ? * MON-FRI
周一至周五的上午10:15触发
0 15 10 15 * ?
每月15日上午10:15触发
0 15 10 L * ?
每月最后一日的上午10:15触发
0 15 10 ? * 6L
每月的最后一个星期五上午10:15触发
0 15 10 ? * 6L 2002-2005
2002年至2005年的每月的最后一个星期五上午10:15触发
0 15 10 ? * 6#3
每月的第三个星期五上午10:15触发
四、触发器表达式生成器
当然,如果你嫌Cron表达式手写费事,没关系,博主给大家提供一个CronExpBuilder-1.0(触发器表达式生成器),纯傻瓜式操作。
下载地址:http://download.csdn.net/detail/qq296398300/9743045
相关资源:Quartz 任务调度框架
