In this example, a Listener (Listener) and Timer (Timer) are used to regularly execute a Task based on the Java interface ).
MyListener:
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 );
// Set the task schedule, start time, and interval.
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 Add the specific task code here
}
}
Web. xml configuration:
<Listener>
<Listener-class> com. fastunit. samples. listener. MyListener </listener-class>
</Listener>
Author: maid