您的位置:首页 > 编程语言 > Java开发

使用Spring Quartz执行定时任务

2012-08-06 22:57 771 查看

使用Spring Quartz执行定时任务

(2006-06-03 18:40:09)


转载▼

分类:JAVA-Spring
Quartz是OpenSymphony下的一个开源项目,提供了比JDK的TimeTask更强大的定时任务执行功能。Spring在Quartz的基础上包装了一层,使得在不使用数据库配置Quartz的情况下,不必再用Quartz的JavaBean设置参数,代码更优雅,可配置性高。
下面我就举个简单的例子。首先,配置Spring的配置文件,起名叫applicationContext.xml

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!-- 配置 -->

<bean name="randomPriceJob"class="org.springframework.scheduling.quartz.JobDetailBean">

<propertyname="jobClass">

<value>test.RandomPriceJob</value>

</property>

<propertyname="jobDataAsMap">

<map>

<entrykey="timeout"><value>5</value></entry>

</map>

</property>

</bean>

<!-- 配置触发器 -->

<bean id="cronTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">

<propertyname="jobDetail">

<refbean="randomPriceJob"/>

</property>

<!-- 每天的11点到11点59分中,每分钟触发RandomPriceJob,具体说明见附录 -->

<propertyname="cronExpression">

<value>0* 11 * * ?</value>

</property>

</bean>

<beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<!-- 添加触发器-->

<propertyname="triggers">

<list>

<reflocal="cronTrigger"/>

</list>

</property>

</bean>

</beans>

然后编写具体操作代码

package test;

import org.apache.log4j.Category;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

/**

* @author shenshan

* @version 1.0

*/

public class RandomPriceJob extends QuartzJobBean

{

private static finalCategory cat = Category

.getInstance(RandomPriceJob.class );

privateint timeout;

/**

* @param timeout

*/

public void setTimeout( int timeout )

{

this.timeout = timeout;

}

/*

* (non-Javadoc)

*

* @seeorg.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)

*/

protected void executeInternal(JobExecutionContext context )

throwsJobExecutionException

{

cat.debug( "Job start" );

//执行具体操作

}

}

最后编写运行程序

package test;

import org.quartz.Scheduler;

import org.quartz.impl.StdSchedulerFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.scheduling.quartz.CronTriggerBean;

import org.springframework.scheduling.quartz.JobDetailBean;

/**

* @author shenshan

* @version 1.0

*/

public class RandomPrice

{

public static void main( String[ ] args )throws Exception

{

ClassPathResource res = newClassPathResource( "applicationContext.xml" );

XmlBeanFactory factory = newXmlBeanFactory( res );

JobDetailBean job = (JobDetailBean ) factory

.getBean("randomPriceJob" );

CronTriggerBean trigger = (CronTriggerBean ) factory

.getBean("cronTrigger" );

Scheduler scheduler =StdSchedulerFactory.getDefaultScheduler( );

scheduler.start( );

scheduler.scheduleJob( job,trigger );

}

}

编译后运行RandomPrice就OK了。需要注意的是,必须使用main函数才能运行,不能使用JUnit。

附:cronExpression配置说明

字段允许值允许的特殊字符
0-59
, - */
0-59
, - */
小时
0-23
, - */
日期
1-31
, - * ? / L WC
月份
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 ? 3WED"
每年三月的星期三的下午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 ? * 6L2002-2005"
2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? *6#3"
每月的第三个星期五上午10:15触发
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: