---
title: "OpenJWeb使用Spring框架实现定时作业调度的例子"
author: "cjc"
date: "2009-03-13T14:13:00Z"
tags: []
slug: "openjweb-spring"
draft: false
lastmod: "2009-03-13T14:13:00Z"
id: 446861
url: "https://cjc.iteye.com/blog/446861"
---

因Spring集成了QuartzJob,所以实现定时作业调度非常方便,首先实现一个作业调度类,类里面可以任意增加用于定时调用的方法,如定时发邮件,定时构造索引,工作流引擎定时监控等,下面是一个继承了的类:

import org.apache.log4j.Logger;  
//import org.apache.lucene.demo.IndexHTML;  
import org.quartz.JobExecutionContext;  
import org.quartz.JobExecutionException;  
import org.springframework.scheduling.quartz.QuartzJobBean;

public class JobSchedule extends QuartzJobBean  
{  
static Logger logger = Logger.getLogger(JobSchedule.class);  
static long counter = 0; //计数器  
public synchronized void doTimerSchedule()  
{  
counter++;  
System.out.println("测试定时器,总计数:"+String.valueOf(counter));  
}

protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {  
// TODO Auto-generated method stub

}

public synchronized void doBuildIndex()  
{  
System.out.println("开始构造索引库....");  
//String indexPath = ServiceLocator.getSysConfigService().getStringValueByParmName("luceneIndexDir");  
//String filePath = ServiceLocator.getSysConfigService().getStringValueByParmName("searchRoot");  
//IndexHTML.buildIndex(indexPath,filePath);  
System.out.println("索引库构造完毕!");  
}

}

有了这个类以后,我们可以在spring的配置文件配置一个定时器,定时调用doTimerSchedule(),下面是spring配置文件的配置定时器的例子:

&lt;!-- Timer schedule --&gt;  
&lt;bean id="defaultTimerBean" class="org.openjweb.core.schedule.JobSchedule"/&gt;  
&lt;bean id="defaultTimerMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;  
&lt;property name="targetObject" ref="defaultTimerBean" /&gt;  
&lt;property name="targetMethod" value="doTimerSchedule" /&gt;  
&lt;property name="concurrent" value="false" /&gt; &lt;!--将并发设置为false--&gt;  
&lt;/bean&gt;  
&lt;bean id="defaultTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
&lt;property name="jobDetail" ref="defaultTimerMethod" /&gt;  
&lt;!--每三分钟的第一分钟触发--&gt;  
&lt;property name="cronExpression" value="0 1/3 * * * ?" /&gt;  
&lt;/bean&gt;

&lt;bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;  
&lt;property name="triggers"&gt;  
&lt;list&gt;  
&lt;!--作业调度器,list下可加入其他的调度器--&gt;  
&lt;ref bean="defaultTrigger"/&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

如果还要配置另外的定时器定时执行doBuildIndex,则 增加下面的配置:

&lt;bean id="luceneTimerMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;  
&lt;property name="targetObject" ref="defaultTimerBean" /&gt;  
&lt;property name="targetMethod" value="doBuildIndex" /&gt;  
&lt;property name="concurrent" value="false" /&gt; &lt;!--将并发设置为false--&gt;  
&lt;/bean&gt;

&lt;bean id="luceneTimerTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
&lt;property name="jobDetail" ref="luceneTimerMethod" /&gt;  
&lt;property name="cronExpression" value="0 1/30 * * * ?" /&gt; &lt;!--每30分钟构造一次索引--&gt;  
&lt;/bean&gt;

并在上面的&lt;ref bean="defaultTrigger"/&gt;下面增加&lt;ref bean="luceneTimerTrigger"/&gt;.

不过奇怪的是,&lt;property name="cronExpression" value="0 1/3 * * * ?" /&gt;  
设置了每3分钟的第1分钟执行一次,但后台输出在每次时间到的时候定时器运行了两便,为什么?哪位高人知道原因指点一下?