web.xml监听配置
com.numenzq.mc.service.impl.TimerListener
TimerListener类
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;