日期:2014-05-20 浏览次数:20934 次
<!-- 定时监听 -->
<listener>
<listener-class>com.numenzq.mc.service.impl.TimerListener</listener-class>
</listener>
public class TimerListener implements ServletContextListener {
private Timer timer;
private TimerTaskSelf task;
/**
* @see 销毁
*/
@Override
public void contextDestroyed(ServletContextEvent arg0) {
timer.cancel();
}
/**
* @see 创建
*/
@Override
public void contextInitialized(ServletContextEvent event) {
timer = new java.util.Timer(true);
task = new TimerTaskSelf(event.getServletContext());
//执行任务指定间隔
timer.scheduleAtFixedRate(task, 0, 1000*60*50);
}
}
package com.numenzq.mc.service.impl;
import java.util.Calendar;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
public class TimerTaskSelf extends TimerTask {
private ServletContext context;
public TimerTaskSelf(ServletContext context){
this.context = context;
}
@Override
public void run() {
System.out.println(DateUtils.format(DateUtils.currentDate(),"yyyy-MM-dd HH:mm:ss") + " web监听结合定时器应用每次执行2次的问题...");
}
}