import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class TestListener implements ServletContextListener {
private Timer timer = null;
public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
timer.schedule(new TestTimer(), 0, 86400000);//milliseconds
}
public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}
}
------------------------------------------
import java.util.TimerTask;
public class TestTimer extends TimerTask {
public void run() {
// do something ...
}
}
------------------------------------------
web.xml:
<listener>
<listener-class>test.TestListener</listener-class>
</listener>
Tomcat启动就执行特定方法,布布扣,bubuko.com
时间: 2024-10-08 20:59:33