定时监听启动
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
import org.apache.commons.logging.LogFactory;
public class XXXListener extends HttpServlet implements ServletContextListener{
private static final long serialVersionUID = 7065236947173387428L;
// 设置启动时间为1点;
private static final int one_hours =1;
//private static final int one_hours = 0;
private static final int one_minutes = 0;
private static final int one_seconds = 0;
// 设置间隔时间为1天;
private static final long oneday_interval = 24 * 60 * 60 * 1000;
//private static final long oneday_interval = 5 * 60 * 1000;
// 定时器;
private static Timer timer;
public void contextDestroyed(ServletContextEvent sce) {
LogFactory.getLog(XXXListener .class).info("XXXListener >>stop!");
timer.cancel();
}
public void contextInitialized(ServletContextEvent sce) {
if (timer == null) {
LogFactory.getLog(ApplyTaskListener.class).info("XXXListener >>start!");
timer = new Timer("XXXListener");
// 定时受理业务
Calendar receiveDate = Calendar.getInstance();
receiveDate.set(Calendar.HOUR_OF_DAY, one_hours);
receiveDate.set(Calendar.MINUTE, one_minutes);
receiveDate.set(Calendar.SECOND, one_seconds);
if (new Date().getTime() > receiveDate.getTime().getTime()) {
receiveDate.add(Calendar.DATE, 1);
}
//new MyTask().run();
//执行生成文件的任务
timer.schedule(new myTask(), receiveDate.getTime(), oneday_interval);
}
}
web-xml配置
<listener>
<listener-class>com.sooyie.sms.listener.XXXListener</listener-class>
</listener>