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

关于Spring中用quartz定时器在定时到达时同时执行两次的问题

2009-04-16 10:12 567 查看
我在使用spring的quartz配置定时任务时,发现每次定时时间到达后,指定的定时方法同时执行两次,而且此方法还是使用的synchronized关键字,每次定时一到,会发现此方法内的System.out输出信息输出两次,说明方法在这时执行了两次,解决方法没有找到更好的,不过有一个方法很有效,我设置了一个静态变量,只要此方法一执行,就将变量由0变为1,执行完再设置为0.如果运行方法前检查此静态变量不为0,则return.开发时注意此静态变量不要让别的方法使用.

下面是定时相关的代码:

package org.openjweb.core.schedule;

import org.apache.log4j.Logger;
import org.apache.lucene.demo.IndexHTML;
//import org.apache.lucene.demo.IndexHTML;
import org.openjweb.core.service.ServiceLocator;
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; //计数器
//static long priorTime = System.currentTimeMillis();
static int searchIndexFlag =0;//由doBuildIndex修改,其他方法不要修改此参数

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()
{
//定时器同时调用两次的问题需要查找原因
if(searchIndexFlag>0)return ;//保证同一时刻只有一个定时器运行,通过这种方式保证每次定时时间到时,只执行一个线程
searchIndexFlag = 1;//锁定
System.out.println("开始构造索引库....");
String indexPath = ServiceLocator.getSysConfigService().getStringValueByParmName("luceneIndexDir");
String filePath = ServiceLocator.getSysConfigService().getStringValueByParmName("searchRoot");
System.out.println("索引路径为:"+indexPath);
System.out.println("搜索路径为:"+filePath);

IndexHTML.buildIndex(indexPath,filePath);
System.out.println("索引库构造完毕!");
searchIndexFlag = 0;//锁定解除
}

}

使用静态变量标志的方式解决了同一时刻调用两次方法的问题,但我想Spring应该有办法配置只能同一时刻运行一次,是什么造成的定时执行两次呢?大家有没有发现这种问题,欢迎赐教.下面是我在spring的配置:

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

<bean id="luceneTimerMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="defaultTimerBean" />
<property name="targetMethod" value="doBuildIndex" />
<property name="concurrent" value="false" /> <!--将并发设置为false-->
</bean>
<bean id="luceneTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="luceneTimerMethod" />

<property name="cronExpression" value="0 1/4 * * * ?" />
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--作业调度器,list下可加入其他的调度器-->
<ref bean="defaultTrigger"/>
<ref bean="luceneTrigger"/>
</list>
</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: