今天在使用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是没有必要的。