public class TimerListener
implements javax.servlet.ServletContextListener {
private java.util.Timer timer;
long iVoxCount = 0;
long iMsgCount = 0;
private java.io.InputStream is = null;
public TimerListener() {
System.out.println("初始化成功");
timer = new java.util.Timer(true);
}
public void contextDestroyed(javax.servlet.ServletContextEvent event) {
System.out.println("系统关闭");
timer.cancel();
}
public void contextInitialized(javax.servlet.ServletContextEvent event) {
System.out.println("开始检测");
//System.out.println( event.getServletContext().getRealPath( "/" ) );
timer.schedule( new java.util.TimerTask() {
private HttpURLConnection conn;
public void run() {
// System.out.println("111");
try {
Timer timer=new Timer();
TimerTask task=new TimerTask(){
public void run(){
//here is what you want to do, println helloworld for example
System.out.println("HelloWorld");
}
};
//period between two execute
long period=5*1000;
timer.schedule(task,0,period);
5秒打印一次helloworld