首先是监听模块保证tomcat启动时就开始计时
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 {
Try t=new Try();
public void run() {
// do something ...
t.smartcopy();
System.out.println("do something");
}
}