tomcat定时任务--ServletContextListener

SysContextListener.java

 

package com.tq365.util;

import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.tq365.test.Task;

public class SysContextListener implements ServletContextListener{
private Timer timer=null;

//在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能
public void contextInitialized(ServletContextEvent event) {
System.out.println(
"监听器已经初始化!");
timer
=new Timer();
event.getServletContext().log(
"Tomcat定时器已经启动.....");
//调用exportHistoryBean,0表示任务无延迟,5*1000表示每隔5秒执行任务,60*60*1000表示一个小时
//timer.schedule(new SpecifiedTask(), 0, 5*1000);
//timer.schedule(new Task(), 0, 10*1000);
event.getServletContext().log("任务已经添加.....");
}
public void contextDestroyed(ServletContextEvent event) {
//在这里关闭监听器,同时关闭定时器
timer.cancel();
event.getServletContext().log(
"定时器关闭.....");
}
}

SpecifiedTask.java 定时执行的任务

 

package com.tq365.util;
import java.util.TimerTask;
import com.tq365.mail.SendMail;

public class SpecifiedTask extends TimerTask {
private static boolean isRunning = false;
public SpecifiedTask(ServletContext context){
this.context=context;
}
*/
@Override
public void run() {
if(!isRunning){
isRunning
=true;
System.out.println(
"定时打印...........");
SendMail.sendMail();
//定时发送邮件
}
}

}

 

 

 

posted on 2011-01-14 17:58  archie2010  阅读(1319)  评论(0编辑  收藏  举报