Servlet的监听器Listener
随web应用的启动而启动
public class TimingSynchronizationListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
@Override
public void contextInitialized(ServletContextEvent arg0) {
try {
TimingSynchronizationTask task = new TimingSynchronizationTask();
Calendar calendar = Calendar.getInstance();
// 获取系统当前时间
Date firstTime = calendar.getTime();
// 间隔:1小时
long period = 1000 * 60 * 60;
// 测试时间每分钟一次
// period = 1000 * 60;
Timer timer = new Timer();
//指定第一次执行的时间,然后按照间隔时间,重复执行
timer.schedule(task, firstTime, period);
} catch (Exception e) {
LogHelp.appendText("=>", "Timing synchronization U8 data failed......");
}
}
}
public class TimingSynchronizationTask extends TimerTask {
//定时同步的时间
private static List<Integer> synchronizationTime;
/*
* 静态初始化
* */
static {
initEatTimes();
}
/*
* 初始化定时同步的时间
* */
private static void initEatTimes(){
synchronizationTime = new ArrayList<Integer>();
synchronizationTime.add(00);
synchronizationTime.add(13);
}
/*
* 执行
* */
@Override
public void run() {
Call call = new Call();
// 获取当前时间
Calendar calendar = Calendar.getInstance();
System.out.println("检查是否到了同步的点");
int hour = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println("hour++++++++++++" + hour);
if(synchronizationTime.contains(hour)){
call.syncVendor(cVenCode, zcrtuid);
}else {
LogHelp.appendText("=>", "定时同步异常......");
}
}
}
在web.xml中配置
<!-- <listener>
<listener-class>com.rjp.srm.syncu8.u82les.TimingSynchronizationListener</listener-class>
</listener> -->