Spring定时器问题

今天在使用Spring的quartz定时器时,遇到了一个奇怪的问题,就是使用了定时器后tomcat一直启动不完,卡在Hibernate读取数据库这一步长期等待。如果一直不管的话居然会把数据库的进程数占满!

核心代码如下:

public class VisitorTimer {

	private ICache iCache;
	public VisitorTimer() {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
		iCache = (ICache)ctx.getBean("icache");
	}
	public void vistorSynchronize() throws JobExecutionException{
		
		String strLock = iCache.getKey("vistorsynlock");
		if(strLock!=null&&strLock.equalsIgnoreCase("true"))
			return;
		iCache.putKey("vistorsynlock", "true");
		System.out.println("正在同步数据   时间:" + new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(new Date()));
		iCache.putKey("vistorsynlock", "false");
	}
}

Spring中的配置信息如下:

<!-- 定时任务(信息同步) -->
<bean id="jobVistorSynchronize" class="com.supermap.smartcity.facility.visitorengine.utils.VisitorTimer"/>
<bean id="timeDetailVistorSynchronize" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="jobVistorSynchronize"/>
    <property name="targetMethod" value="vistorSynchronize"/>
</bean>
<bean id="timeTiggerVistorSynchronize" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="timeDetailVistorSynchronize"/>
    <property name="cronExpression" value="0/5 * * * * ?"/>
</bean>
		
<!-- 启动定时器 -->
<bean id="startJob" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
	    <ref bean="timeTiggerVistorSynchronize"/>
        </list>
    </property>
</bean>

初看上述代码和配置跟Hibernate都没有关系呀。一时间没有了思路。

后来,万般无奈之下,我把VisitorTimer类的构造函数去掉,并将Spring Bean的获取放到了方法vistorSynchronize()中后,问题居然解决了!!!代码如下:

public class VisitorTimer {

	public void vistorSynchronize() throws JobExecutionException{
		ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
		ICache iCache = (ICache)ctx.getBean("icache");
		String strLock = iCache.getKey("vistorsynlock");
		if(strLock!=null&&strLock.equalsIgnoreCase("true"))
			return;
		iCache.putKey("vistorsynlock", "true");
		System.out.println("正在同步数据   时间:" + new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(new Date()));
		iCache.putKey("vistorsynlock", "false");
	}
}

分析上述代码中,我本来的意图是说因为每隔一段时间都要执行vistorSynchronize()方法,而在该方法里每次都会读取Spring的Bean对象。这样的话为啥不在类的构造函数里将接口iCache注入好,在vistorSynchronize()方法中直接调用接口iCache中的getKey()和putKey()方法就可以了。

然而通过遇到的错误才明白,在使用Spring的quartz定时器时,不能在类的构造函数里来获取Bean。在我潜意识里还是将定时器对应的类当成是单例模式了,而实际上并不是单例,而是每到指定的时间都是重新实例化定时器类的。所以在构造函数里获取Bean是没有必要的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值