Ryan's Java world!

something about Java and opensource!

BlogJava 首页 新随笔 联系 聚合 管理
  51 Posts :: 25 Stories :: 59 Comments :: 0 Trackbacks

http://www.52blog.net/user1/580/archives/2005/101299.shtml
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">
  
  <property name="jobClass">
   <value>test.RandomPriceJob</value>
  </property>
  
  <property name="jobDataAsMap">
   <map>
    <entry key="timeout"><value>5</value></entry>
   </map>
  </property>
  
 </bean>
 <!-- 配置触发器 --> 
 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  
  <property name="jobDetail">
   <ref bean="randomPriceJob"/>
  </property>
  <!-- 每天的11点到11点59分中,每分钟触发RandomPriceJob,具体说明见附录 -->
  <property name="cronExpression">
   <value>0 * 11 * * ?</value>
  </property>
  
 </bean>

 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

  <!-- 添加触发器 -->
  <property name="triggers">
   <list>
    <ref local="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 final Category cat = Category
            .getInstance( RandomPriceJob.class );

 private int      timeout;

 /**
  * @param timeout
  */
 public void setTimeout( int timeout )
 {
  this.timeout = timeout;
 }

 /*
  * (non-Javadoc)
  *
  * @see org.springframework.scheduling.quartz.QuartzJobBean#e xecuteInternal(org.quartz.JobExecutionContext)
  */
 protected void executeInternal( JobExecutionContext context )
   throws JobExecutionException
 {
    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 = new ClassPathResource( "applicationContext.xml" );
  XmlBeanFactory factory = new XmlBeanFactory( 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 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触发
posted on 2006-04-27 15:53 冰雨 阅读(2920) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航:
 

JSF中文技术文摘