测试可用
web.xml 配置监听器
<listener>
<listener-class>com.iAe.common.auto.ServletListener</listener-class> //servlet对应的位置
</listener>
写一个servlet
public class ServletListener implements ServletContextListener {
private final Timer timer = new Timer();
private Calendar calFirst=Calendar.getInstance(); //第一个任务的启动时间
private Calendar calSecond=Calendar.getInstance(); //第二个任务的启动时间
private Calendar calThird=Calendar.getInstance(); //第二个任务的启动时间
public void contextDestroyed(ServletContextEvent se) {
timer.cancel();
}
public void contextInitialized(ServletContextEvent se) {
//String firstTime=se.getServletContext().getInitParameter("firstTime");
//System.out.println(firstTime);
//String secondTime=se.getServletContext().getInitParameter("secondTime");
//System.out.println(secondTime);
//设置第一启动时间23:30
calFirst.set(Calendar.HOUR_OF_DAY, 23);
calFirst.set(Calendar.MINUTE,30);
//启动第一个任务
timer.schedule(new FirstTimerTask(), calFirst.getTime(),24*60*60*1000);
//设置第二启动时间23:45
calSecond.set(Calendar.HOUR_OF_DAY, 23);
calSecond.set(Calendar.MINUTE, 45);
//启动第二个任务
timer.scheduleAtFixedRate(new SecondTimerTask(), calSecond.getTime(),24*60*60*1000);
//设置第三启动时间00:05
calThird.set(Calendar.HOUR_OF_DAY, 0);
calThird.set(Calendar.MINUTE, 5);
//启动第三个任务
timer.scheduleAtFixedRate(new ThirdTimerTask(), calThird.getTime(),24*60*60*1000);
//设置第四启动时间00:10
calThird.set(Calendar.HOUR_OF_DAY, 0);
calThird.set(Calendar.MINUTE, 10);
//启动第四个任务
timer.scheduleAtFixedRate(new ForthTimerTask(), calThird.getTime(),24*60*60*1000);
}
public class FirstTimerTask extends TimerTask{
public void run() {
makeTonRent();
clientStoreReport();
}
}
在这里面可以获得aplicationContext.xml文件,再进行数据库方面操作。