java定时器,在web工程中执行

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

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

public class SendMessageTimer implements ServletContextListener {
  private Timer timer = null;
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
    timer.cancel(); 
  }

  @Override
  public void contextInitialized(ServletContextEvent arg0) {
    //设置执行时间
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
    //定制每天的10:00:00执行,
    calendar.set(year, month, day, 10, 00, 00);
    Date date = calendar.getTime();
    timer = new Timer(true); 
    System.out.println(date);
    int period = 2 * 1000;
    //每天的date时刻执行task,每隔2秒重复执行
    timer.schedule(new SendMessageTask(), date, period);
    //每天的date时刻执行task, 仅执行一次
    //timer.schedule(task, date);

  }
}


 

import java.util.TimerTask;

public class SendMessageTask extends TimerTask{

  @Override
  public void run() {
    System.out.println("执行了……");
  }

}


web.xml配置监听

     
            com.tsingsoft.basedata.sendMessage.SendMessageTimer 
    


 

你可能感兴趣的:(java)