java定时任务的实现方法
在应用程序中,经常需要在后台运行任务,例如我们需要在服务后台运行任务来执行数据等等,本文是爱站技术频道小编为大家搜集的关于java定时任务的实现方法,希望对你了解这方面知识有所帮助!
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) {
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配置
上文是java定时任务的实现方法,大家了解了多少了?更多的java知识尽在爱站技术频道!