Java scheduled task implementation method and java task implementation method
Copy codeThe Code is as follows:
Package com. ucap. sms;
Import java. util. Timer;
Import javax. servlet. ServletContextEvent;
Import javax. servlet. ServletContextListener;
Public class SmsListener implements ServletContextListener {
Private Timer timer = null;
Public void contextDestroyed (ServletContextEvent sce ){
Timer. cancel ();
}
Public void contextInitialized (ServletContextEvent sce ){
Timer = new Timer (true );
// Set the task schedule, start time, and interval.
Timer. schedule (new NoteReplyTask (), 0, 86400000 );
}
}
Copy codeThe Code is as follows:
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
}
}
WebXml Configuration
Copy codeThe Code is as follows:
<Listener>
<Listener-class> com. fastunit. samples. listener. MyListener </listener-class>
</Listener>