---
title: "关于Spring中用quartz定时器在定时到达时同时执行两次的问题"
author: "cjc"
date: "2009-04-16T10:12:00Z"
tags: []
slug: "spring-quartz"
draft: false
lastmod: "2009-04-16T10:12:00Z"
id: 446846
url: "https://cjc.iteye.com/blog/446846"
---

我在使用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&gt;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的配置:

&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 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="luceneTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
&lt;property name="jobDetail" ref="luceneTimerMethod" /&gt;

&lt;property name="cronExpression" value="0 1/4 * * * ?" /&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;ref bean="luceneTrigger"/&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;