...
|
...
|
@@ -2,6 +2,7 @@ package com.yoho.rfid.service; |
|
|
|
|
|
import com.yoho.rfid.constant.BackWorkerType;
|
|
|
import com.yoho.rfid.helper.MonitorHelper;
|
|
|
import com.yoho.rfid.model.ClientConfig;
|
|
|
import com.yoho.rfid.model.MonitorFuture;
|
|
|
import com.yoho.rfid.model.SystemConfig;
|
|
|
import com.yoho.rfid.model.TreadNode;
|
...
|
...
|
@@ -9,6 +10,7 @@ import com.yoho.rfid.model.req.AppReportReq; |
|
|
import com.yoho.rfid.model.req.InfoScreenHeartBeatPacket;
|
|
|
import com.yoho.rfid.thread.MonitorThreadFactory;
|
|
|
import com.yoho.rfid.util.DateUtil;
|
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
...
|
...
|
@@ -19,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.*;
|
...
|
...
|
@@ -27,7 +30,7 @@ import java.util.concurrent.*; |
|
|
* Created by chenchao on 2017/10/16.
|
|
|
*/
|
|
|
@Service
|
|
|
public class MonitorService {
|
|
|
public class MonitorService extends AbstractMonitorService{
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
//
|
...
|
...
|
@@ -41,10 +44,7 @@ public class MonitorService { |
|
|
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
|
|
|
private ExecutorService executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
|
|
|
0L, TimeUnit.MILLISECONDS, workQueue, monitorThreadFactory);
|
|
|
//崩溃
|
|
|
private static final String CRASH = "crash",
|
|
|
//重启完毕
|
|
|
REBOOTED = "rebooted";
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
...
|
...
|
@@ -227,17 +227,18 @@ public class MonitorService { |
|
|
continue;
|
|
|
}
|
|
|
long lastUpdateDT = req.getUpdateDateTime();
|
|
|
long currentDT = System.currentTimeMillis();
|
|
|
long diff = currentDT - lastUpdateDT;
|
|
|
|
|
|
if(diff > timeout){
|
|
|
logger.warn("AppReportMap find reeboot, req {},currentDT {}, diff {}, timeout {}",
|
|
|
req, currentDT, diff, timeout);
|
|
|
boolean isTimeout = isTimeout(timeout, lastUpdateDT);
|
|
|
if(isTimeout){
|
|
|
logger.warn("AppReportMap find reboot, req {} timeout {}",
|
|
|
req, timeout);
|
|
|
//reboot
|
|
|
//infoScreenService.reboot(req.getIp(), req.getScreenType());
|
|
|
//TODO use queue to split send mail function, use a thread to send mail
|
|
|
mailService.send(buildMailContent4AppCrash(req.getIp(), req.getScreenType()).toString(),false);
|
|
|
needRemove.add(req.getIp());
|
|
|
boolean cansend = canSend4GlobalScope() && canSend4AppReport();
|
|
|
if(cansend){
|
|
|
mailService.send(buildMailContent4AppCrash(req.getIp(), req.getScreenType()).toString());
|
|
|
needRemove.add(req.getIp());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//remove from map
|
...
|
...
|
@@ -286,17 +287,18 @@ public class MonitorService { |
|
|
continue;
|
|
|
}
|
|
|
long lastUpdateDT = packet.getUpdateDateTime();
|
|
|
long currentDT = System.currentTimeMillis();
|
|
|
long diff = currentDT - lastUpdateDT;
|
|
|
|
|
|
boolean isTimeout = isTimeout(timeout, lastUpdateDT);
|
|
|
//todo 合并多个,只发送一封邮件
|
|
|
if(diff > timeout){
|
|
|
if(isTimeout){
|
|
|
//reboot
|
|
|
logger.warn("HeartBeatMap find no heartbeat, req {},currentDT {}, diff {}, timeout {}",
|
|
|
packet, currentDT, diff, timeout);
|
|
|
logger.warn("HeartBeatMap find no heartbeat, req {}, timeout {}",
|
|
|
packet, timeout);
|
|
|
//infoScreenService.reboot(req.getIp(), req.getScreenType());
|
|
|
mailService.send(buildMailContent(packet.getIp(), packet.getScreenType()).toString(), false);
|
|
|
needRemove.add(packet.getIp());
|
|
|
boolean cansend = canSend4GlobalScope() && canSend4HeartBeat();
|
|
|
if(cansend){
|
|
|
mailService.send(buildMailContent(packet.getIp(), packet.getScreenType()).toString());
|
|
|
needRemove.add(packet.getIp());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//remove from map
|
...
|
...
|
@@ -314,25 +316,34 @@ public class MonitorService { |
|
|
}
|
|
|
|
|
|
private void sendMailIfExistOpenClient(){
|
|
|
int currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
|
|
|
int currentHour;
|
|
|
SystemConfig systemConfig = SystemConfig.getInstance();
|
|
|
boolean isNotClose = (currentHour >= systemConfig.getCloseHour() || currentHour < systemConfig.getOpenHour())
|
|
|
&& MapUtils.isNotEmpty(HeartBeatMap) && !isNoticedOpenClient;
|
|
|
if (isNotClose){
|
|
|
isNoticedOpenClient = true;
|
|
|
List<String> ips = new ArrayList<String>(HeartBeatMap.keySet());
|
|
|
mailService.send(buildMailContentOfExistOpenClient(ips).toString(), true);
|
|
|
//移除的意义不大,心跳会不间断地上报
|
|
|
this.removeAll(ips);
|
|
|
logger.info("in sendMailIfExistOpenClient, ips {}", ips);
|
|
|
ClientConfig clientConfig = systemConfig.getClientConfig();
|
|
|
//尚未发送通知
|
|
|
if(!isNoticedOpenClient){
|
|
|
currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
|
|
|
boolean hasData = MapUtils.isNotEmpty(HeartBeatMap);
|
|
|
boolean cansend = clientCanSendMail(clientConfig);
|
|
|
boolean isNotClose = (currentHour >= clientConfig.getCloseHour() || currentHour < clientConfig.getOpenHour());
|
|
|
if (isNotClose && hasData && cansend){
|
|
|
isNoticedOpenClient = true;
|
|
|
List<String> ips = new ArrayList<String>(HeartBeatMap.keySet());
|
|
|
mailService.send(buildMailContentOfExistOpenClient(ips).toString());
|
|
|
//移除的意义不大,心跳会不间断地上报
|
|
|
this.removeAll(ips);
|
|
|
logger.info("in sendMailIfExistOpenClient, ips {}", ips);
|
|
|
}
|
|
|
}
|
|
|
//auto wakeup, reset isNoticedOpenClient if it's false
|
|
|
boolean inBussiness = currentHour < systemConfig.getCloseHour()
|
|
|
&& currentHour >= systemConfig.getOpenHour()
|
|
|
&& isNoticedOpenClient;
|
|
|
if(inBussiness){
|
|
|
isNoticedOpenClient = false;
|
|
|
logger.info("in sendMailIfExistOpenClient, reset isNoticedOpenClient success");
|
|
|
//已经发送通知后,需要恢复标识
|
|
|
if(isNoticedOpenClient){
|
|
|
currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
|
|
|
//auto wakeup, reset isNoticedOpenClient if it's false
|
|
|
boolean inBussiness = currentHour < clientConfig.getCloseHour()
|
|
|
&& currentHour >= clientConfig.getOpenHour();
|
|
|
if(inBussiness){
|
|
|
isNoticedOpenClient = false;
|
|
|
logger.info("in sendMailIfExistOpenClient, reset isNoticedOpenClient success");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -341,13 +352,47 @@ public class MonitorService { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
StringBuilder buildMailContentOfExistOpenClient(List<String> ips){
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
String ipsStr = StringUtils.join(ips, ",") ;
|
|
|
sb.append("信息屏[").append(ipsStr).append("]还没有关闭,请尽早安排其休息,以免次日出现不良情绪");
|
|
|
return sb;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean canSend4AppReport(){
|
|
|
SystemConfig systemConfig = SystemConfig.getInstance();
|
|
|
//上午10点到晚上22点发送,其他时间不发
|
|
|
int currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
|
|
|
if(currentHour<systemConfig.getSendMailBeginHour() || currentHour >=systemConfig.getSendMailEndHour()){
|
|
|
logger.warn("in canSend4AppReport,it's too late, don't need to send mail, thanks developer sailing");
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
private boolean canSend4GlobalScope(){
|
|
|
SystemConfig systemConfig = SystemConfig.getInstance();
|
|
|
if(!systemConfig.isSendMail()){//不发送一般发生在测试环境
|
|
|
logger.warn("in canSend4GlobalScope,systemConfig.sendMail is set {},u can use manage/sendMail/open to control", systemConfig.isSendMail());
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
private boolean canSend4HeartBeat(){
|
|
|
SystemConfig systemConfig = SystemConfig.getInstance();
|
|
|
//上午10点到晚上22点发送,其他时间不发
|
|
|
int currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
|
|
|
if(currentHour<systemConfig.getSendMailBeginHour() || currentHour >=systemConfig.getSendMailEndHour()){
|
|
|
logger.warn("in canSend4HeartBeat,it's too late, don't need to send mail, thanks developer sailing");
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
...
|
...
|
|