/**
* 加载新的数据
*
* @author yzygenuine
*
*/
class RefreshMonitorService implements Runnable {
private long sleepTime = 1000;
private ServiceDao dao;
public RefreshMonitorService(long sleepTime, ServiceDao dao) {
this.sleepTime = sleepTime;
if (sleepTime < 60000) {
logger.warn("刷新加载数据的间隔时间不能太短");
throw new RuntimeException("刷新加载数据的间隔时间不能太短");
}
this.dao = dao;
}
private void firstLoad() {
List<MonitorService> monitorService = dao.getService();
logger.info("加载记录:" + monitorService.size());
// 将被监控服务加入优先级队列里
for (int j = 0; j < monitorService.size(); j++) {
MonitorService service = monitorService.get(j);
// 初始化好时间
service.setCurrentTime(service.getIntervalTime() + intervalTime.get());
currentSet.add(service);
sleepQueue.add(service);
}
}
@Override
public void run() {
logger.info("读取新的service开启工作");
firstLoad();
try {
do {
logger.info("定时加载新的数据监听者休息{}ms", sleepTime);
Thread.sleep(sleepTime);
logger.info("##########开始执行更新数据############");
// 加载新的所有所数据 ,与当前的数据比较
List<MonitorService> deleteList = dao.deleteService();
List<MonitorService> addList = dao.incrementalService();
logger.info("删除旧的数据共:{}", deleteList.size());
currentSet.removeAll(deleteList);
logger.info("增加新的数据共:{}", addList.size());
currentSet.addAll(addList);
logger.info("更新后的currentSet size:{}", currentSet.size());
for (MonitorService service : addList) {
// 初始化绝对间隔时间
service.setCurrentTime(service.getIntervalTime() + intervalTime.get());
sleepQueue.add(service);
}
logger.info("########这一轮更新结束");
} while (!isClose.get());
} catch (Exception e) {
logger.error("", e);
}
}
}
/**
* 响应的封装类
*
* @author yzygenuine
*
*/
class Response {
public Map<String, String> response;
public MonitorService service;
public boolean isNeedWarn;
public boolean isSucToNotify;
}
public void setDao(ServiceDao dao) {
this.dao = dao;
}
}