spring集成quartz,出现2次重复调用的有关问题

spring集成quartz,出现2次重复调用的问题

非web应用开发中,系统用Spring集成Quartz,也就是在Spring配置文件applicationContext.xml中配置Quartz,具体代码如下:

Xml代码 spring集成quartz,出现2次重复调用的有关问题
  1. <!-- Quartz调度模块 -->  
  2. <bean id="callJobBean" class="iprai.quartz.CallJobBean" />  
  3.   
  4. <bean id="callJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  5.     <property name="targetObject">  
  6.         <ref bean="callJobBean" />  
  7.     </property>  
  8.     <property name="targetMethod">  
  9.         <value>executeInternal</value>  
  10.     </property>  
  11. </bean>  
  12.   
  13. <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  14.     <property name="jobDetail">  
  15.         <ref bean="callJobDetail" />  
  16.     </property>  
  17.     <property name="cronExpression">  
  18.         <value>0 0/5 * * * ?</value>  
  19.     </property>  
  20. </bean>  
  21.   
  22. <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  23.     <property name="triggers">  
  24.         <list>  
  25.             <ref local="cronTrigger" />  
  26.         </list>  
  27.     </property>  
  28. </bean>  
	<!-- Quartz调度模块 -->
	<bean id="callJobBean" class="iprai.quartz.CallJobBean" />
	
	<bean id="callJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="callJobBean" />
		</property>
		<property name="targetMethod">
			<value>executeInternal</value>
		</property>
	</bean>
	
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="callJobDetail" />
		</property>
		<property name="cronExpression">
			<value>0 0/5 * * * ?</value>
		</property>
	</bean>
	
	<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="cronTrigger" />
			</list>
		</property>
	</bean>

 
当我的系统启动时,会读取并实例化applicationContext.xml文件中的其他bean,出现的问题就是读取一次配置文件并实例化一个与Quartz不相关的bean时,代码如下:

Java代码 spring集成quartz,出现2次重复调用的有关问题
  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");   
  2. RiddickMessage message = (RiddickMessage)ctx.getBean("message");  
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		RiddickMessage message = (RiddickMessage)ctx.getBean("message");

 

Quartz就会相应地被调用一次,之前读取了多少次applicationContext.xml文件,运行时就会在Quartz设定的时间点重复执行相同次数的代码,也就是执行上面配置文件中的callJobBean对应的类中的executeInternal方法相同的次数,而且是同一个时间点,相当于配置了有多个一模一样的Job了。具体还没弄清楚是Spring到底是怎么自动实例化Quartz的,因为我都没有在代码中显式地实例化schedulerFactory这个bean,也就是下面代码没有运行:

Java代码 spring集成quartz,出现2次重复调用的有关问题
  1. Resource resource = new ClassPathResource("applicationContext-CallJob.xml");   
  2. BeanFactory factory = new XmlBeanFactory(resource);   
  3. scheduler = (Scheduler) factory.getBean("schedulerFactory");  
		Resource resource = new ClassPathResource("applicationContext-CallJob.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		scheduler = (Scheduler) factory.getBean("schedulerFactory");

 最后没办法,只得将applicationContext.xml文件中配置的Quartz代码移到另一个配置文件applicationContext-callJob.xml中,这样就避免了读取applicationContext.xml文件时自动实例化Quartz了。

 

1 楼 cyz001 2011-09-01  
你加个判断,避免重复载入容器就行了