采用Spring来实现定时器

11 篇文章 0 订阅

主要是用Sping来配置定时触发任务函数,本质也是Java的TimerTask:

首先定义一个计时器配置文件:

### schedulingContext-timer.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!-- ======================================================================= -->
<!-- 缓存配置文件 -->
<!-- @author linshutao -->
<!-- CleanEntryEventCacheTask: 执行任务的类 delay: 延迟执行 period:刷新间隔 -->
<!-- ======================================================================= -->

<beans>
	<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
		<property name="scheduledTimerTasks">
			<list>
				<ref local="MyScheduledTimerTask" />
			</list>
		</property>
	</bean>

	<bean id="CleanEntryEventCacheTask" class="com.syni.im800.kb.auto.service.KbsCleanEntryEventCache">
	</bean>

	<bean id="MyScheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<property name="timerTask">
			<ref bean="CleanEntryEventCacheTask" />
		</property>
		<property name="delay">
			<value>10000</value>
		</property>
		<property name="period">
			<value>60000</value>
		</property>
	</bean>
</beans>

然后再web.xml中配置,读取该文件


	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/applicationContext-*.xml,
			/WEB-INF/schedulingContext-timer.xml,
			/WEB-INF/schedule-context.xml,
			classpath*:com/syni/im800/kb/auto/dao/applicationContext-hibernate.xml,
			classpath*:com/syni/im800/kb/auto/service/applicationContext-service.xml,
			classpath*:com/syni/im800/kb/auto/service/applicationContext-mail.xml,
			/WEB-INF/security/security.xml
		</param-value>
	</context-param>


因为已经配置了加载文件内容的servlet,所以上面的/WEB-INF/schedulingContext-timer.xml,就可以直接添加进去,如果你的web.xml还没配置,那么可以用

org.springframework.web.context.ContextLoaderServlet,

3.0中把这个删掉了,你也可以用

Spring3.0下可以采用另外两种启动方式:ContextLoaderListener和ContextLoaderPlugIn。建议使用ContextLoaderListener,具体实现就是在web.xml中添加:

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>
          org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
最后是我的任务类:

/**
 * 清除显示条目相关事件(编辑中,被退回,审核中等等)的缓存
 * @author linshutao
 * 2011年8月11日 14:43:33
 * */

package com.syni.im800.kb.auto.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.syni.im800.kb.attendant.webapp.action.AttendantLoginAction;
import com.syni.im800.kb.common.CacheManager;

public class KbsCleanEntryEventCache extends java.util.TimerTask{
	private static final Log log = LogFactory.getLog(KbsCleanEntryEventCache.class);
	public void run() {
		log.debug("#############清除显示条目相关事件的缓存...");
		CacheManager cacheManager = CacheManager.getInstance();
		cacheManager.clearAll();
	}
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值