关于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>
下面是定时相关的代码:
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>
相关文章推荐
- 关于Spring中用quartz定时器在定时到达时同时执行两次的问题
- 关于Java Web 使用Spring中使用Quartz(定时调用、实现固定时间执行), 触发定时器(执行某些任务)的实例
- 关于spring的quartz(定时任务执行)问题,
- 关于Spring容器中定时器到时执行会出现两个线程同时执行的问题
- SSH:解决Spring整合quartz定时任务执行两次问题
- spring quartz 定时任务执行两次的问题
- 解决Spring定时计划任务重复执行两次(实例被构造两次)问题的方法
- 解决Spring自带的定时任务执行两次的问题
- 关于spring定时器: 多方式实行任务定时执行
- Spring的quartz定时器同一时刻重复执行二次的问题解决
- 关于spring定时器一次调用却触发了两次trigger的问题分析
- Spring的quartz定时器同一时刻重复执行二次的问题解决
- 【Spring】解决SpringTask定时执行两次问题
- java springmvc 定时器方法执行两次问题
- Spring的Quartz定时器同一时刻重复执行二次的问题解决
- spring定时任务被执行两次的问题,原来是tomcat重复发布导致的
- 关于Spring Task 触发时同时执行两次的问题
- linux下spring定时器执行两次问题
- Spring的quartz定时器重复执行二次的问题解决
- Spring的quartz定时器同一时刻重复执行二次的问题解决