最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现。对于一个新手来说,花了我不少时间,这里我写个笔记,给大家参考。
我使用的是Maven来管理项目,需要的Jar包我给大家贴出来。
quartz-1.8.5.jar
commons-logging.jar
spring-core-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-context-support-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring.transaction-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
Maven的pom.xml的配置:
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < 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 > QtzTest </ groupId >
- < artifactId > QtzTest </ artifactId >
- < version > 1.0 </ version >
- < properties >
- < springframework.version > 3.0.5.RELEASE </ springframework.version >
- </ properties >
- < dependencies >
- < dependency >
- < groupId > org.springframework </ groupId >
- < artifactId > spring-context </ artifactId >
- < version > ${springframework.version} </ version >
- </ dependency >
- < dependency >
- < groupId > org.springframework </ groupId >
- < artifactId > spring-context-support </ artifactId >
- < version > ${springframework.version} </ version >
- </ dependency >
- < dependency >
- < groupId > org.springframework </ groupId >
- < artifactId > spring-tx </ artifactId >
- < version > ${springframework.version} </ version >
- </ dependency >
- < dependency >
- < groupId > org.springframework </ groupId >
- < artifactId > spring-web </ artifactId >
- < version > ${springframework.version} </ version >
- </ dependency >
- < dependency >
- < groupId > org.quartz-scheduler </ groupId >
- < artifactId > quartz </ artifactId >
- < version > 1.8.5 </ version >
- </ dependency >
- </ dependencies >
- < build >
- < finalName > ${project.artifactId} </ finalName >
- < plugins >
- < plugin >
- < groupId > org.mortbay.jetty </ groupId >
- < artifactId > jetty-maven-plugin </ artifactId >
- < version > 7.5.4.v20111024 </ version >
- < configuration >
- < scanIntervalSeconds > 10 </ scanIntervalSeconds >
- < webApp >
- < contextPath > /${project.artifactId} </ contextPath >
- </ webApp >
- </ configuration >
- </ plugin >
- </ plugins >
- </ build >
- </ project >
特别注意一点,与Spring3.1以下版本整合必须使用Quartz1,最初我拿2.1.3的,怎么搞都报错:
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.CronTriggerBean] for bean with name 'mytrigger' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class
查看发现spring3.0.5中org.springframework.scheduling.quartz.CronTriggerBean继承了org.quartz.CronTrigger(public class CronTriggerBeanextends CronTrigger),而在quartz2.1.3中org.quartz.CronTrigger是个接口(publicabstract interface CronTrigger extends Trigger),而在quartz1.8.5及1.8.4中org.quartz.CronTrigger是个类(publicclass CronTrigger extends Trigger),从而造成无法在applicationContext中配置触发器。这是spring3.1以下版本和quartz2版本不兼容的一个bug。(感谢tiren的回复,spring3.1以及以后版本支持quartz2)
在Spring中使用Quartz有两种方式实现:第一种是任务类继承QuartzJobBean,
<bean name=" SpringQtzJob " class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 表示任务完成之后是否依然保留到数据库,默认false -->
<property name="durability" value="true" />
<property name="jobClass" value=" com.ncs.hj.SpringQtz " />
</bean>
第二种则是在配置文件里定义任务类和要执行的方法,类和方法仍然是普通类,方法必须无参数和返回值。很显然,第二种方式远比第一种方式来的灵活。
第一种方式的JAVA代码:
- package com.ncs.hj;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.springframework.scheduling.quartz.QuartzJobBean;
- public class SpringQtz extends QuartzJobBean{
- private static int counter = 0 ;
- protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
- System.out.println();
- long ms = System.currentTimeMillis();
- System.out.println( "\t\t" + new Date(ms));
- System.out.println(ms);
- System.out.println( "(" + counter++ + ")" );
- String s = (String) context.getMergedJobDataMap().get( "service" );
- System.out.println(s);
- System.out.println();
- }
- }
第二种方式的JAVA代码:
- package com.ncs.hj;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.springframework.scheduling.quartz.QuartzJobBean;
- import java.util.Date;
- public class SpringQtz {
- private static int counter = 0 ;
- protected void execute() {
- long ms = System.currentTimeMillis();
- System.out.println( "\t\t" + new Date(ms));
- System.out.println( "(" + counter++ + ")" );
- }
- }
Spring的配置文件:
- <!------------ 配置调度程序quartz ,其中配置JobDetail有两种方式-------------->
- <!--方式一:使用JobDetailBean,任务类必须实现Job接口 -->
- < bean id = "myjob" class = "org.springframework.scheduling.quartz.JobDetailBean" >
- < property name = "name" value = "exampleJob" > </ property >
- < property name = "jobClass" value = "com.ncs.hj.SpringQtz" > </ property >
- < property name = "jobDataAsMap" >
- < map >
- < entry key = "service" > < value > simple is the beat </ value > </ entry >
- </ map >
- </property>
- </ bean >
- <!--运行时请将方式一注释掉! -->
- <!-- 方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->
- <!-- 定义目标bean和bean中的方法 -->
- < bean id = "SpringQtzJob" class = "com.ncs.hj.SpringQtz" />
- < bean id = "SpringQtzJobMethod" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
- < property name = "targetObject" >
- < ref bean = "SpringQtzJob" />
- </ property >
- < property name = "targetMethod" > <!-- 要执行的方法名称 -->
- < value > execute </ value >
- </ property >
- </ bean >
- <!-- ======================== 调度触发器 ======================== -->
- < bean id = "CronTriggerBean" class = "org.springframework.scheduling.quartz.CronTriggerBean" >
- < property name = "jobDetail" ref = "SpringQtzJobMethod" > </ property >
- < property name = "cronExpression" value = "0/5 * * * * ?" > </ property >
- </ bean >
- <!-- ======================== 调度工厂 ======================== -->
- < bean id = "SpringJobSchedulerFactoryBean" class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" >
- < property name = "triggers" >
- < list >
- < ref bean = "CronTriggerBean" />
- </ list >
- </ property >
- </ bean >
关于cronExpression表达式,这里讲解一下:
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
表达式意义
"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触发
每天早上6点
0 6 * * *
每两个小时
0 */2 * * *
晚上11点到早上8点之间每两个小时,早上八点
0 23-7/2,8 * * *
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * 1-3
1月1日早上4点
0 4 1 1 *
最后别忘了在web.xml里面配置Spring:
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < web-app xmlns = "http://java.sun.com/xml/ns/javaee"
- xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version = "2.5" >
- < welcome-file-list >
- < welcome-file > index.html </ welcome-file >
- </ welcome-file-list >
- < context-param >
- < param-name > contextConfigLocation </ param-name >
- < param-value > /WEB-INF/spring-config.xml </ param-value >
- </ context-param >
- < listener >
- < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
- </ listener >
- </ web-app >
运行结果:
Wed Feb 08 13:58:30 CST 2012
(0)
Wed Feb 08 13:58:35 CST 2012
(1)
Wed Feb 08 13:58:40 CST 2012
(2)
Wed Feb 08 13:58:45 CST 2012
(3)
Wed Feb 08 13:58:50 CST 2012
(4)
Wed Feb 08 13:58:55 CST 2012
(5)
Wed Feb 08 13:59:00 CST 2012
(6)