Spring定时器

新建一个用于执行定时器的类 TimerTask ,新建准备用于执行同步任务的方法 startTimerJob

public void startTimerJob(){
		StaticServiceImpl staticService = (StaticServiceImpl)SpringInit.getApplicationContext().getBean("staticServiceImpl");
		logger.debug("Start Timer Job");
		System.out.println("Start Timer Job");
		logger.debug("sync " + staticService.syncSalesFromSqlServer() + " datas");
	}

StaticServiceImpl 是用标签注入的类, 在这里用SPring的工厂取得。

配置定时器

<bean id="timerTask" class="com.ceair.muss.common.timer.TimerTask"></bean> 
	<bean id="ZntaskTopSaleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<property name="targetObject" ref="timerTask"></property>
	<property name="targetMethod" value="startTimerJob"></property>
	</bean>
	
	<bean id="zntaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
	<property name="jobDetail">
	<ref bean="ZntaskTopSaleJobDetail"/>
	</property>
	<property name="cronExpression">
	<value>0 0 1 * * ?</value>      
	</property>
	</bean>
	
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	  <property name="triggers">
	<list>
	<ref bean="zntaskTrigger"/>
	</list> 
	  </property>
	</bean>

OK 成功运行。

具体配置参考

相关推荐