java定时任务的实现方法
本文导语: 代码如下: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...
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);
//设置任务计划,启动和间隔时间
timer.schedule(new NoteReplyTask(), 0, 86400000);
}
}
import java.util.TimerTask;
public class MyTask extends TimerTask {
public void run() {
// System.out.println("call at " + (new Date()));
// TODO 此处添加具体任务代码
}
}
webXml配置
com.fastunit.samples.listener.MyListener