- import java.util.Timer;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- public class MyListener implements ServletContextListener {
- private Timer timer = null;
- public void contextInitialized(ServletContextEvent event) {
- timer = new Timer(true);
- //设置任务计划,启动和间隔时间
- timer.schedule(new MyTask(), 0, 86400000);
- }
- public void contextDestroyed(ServletContextEvent event) {
- timer.cancel();
- }
- }
MyTask:
- import java.util.TimerTask;
- public class MyTask extends TimerTask {
- public void run() {
- // System.out.println("call at " + (new Date()));
- // TODO 此处添加具体任务代码
- }
- }
web.xml配置:
- <listener>
- <listener-class>com.fastunit.samples.listener.MyListener</listener-class>
- </listener>