...
|
...
|
@@ -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,19 +227,20 @@ 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);
|
|
|
boolean cansend = canSend4GlobalScope() && canSend4AppReport();
|
|
|
if(cansend){
|
|
|
mailService.send(buildMailContent4AppCrash(req.getIp(), req.getScreenType()).toString());
|
|
|
needRemove.add(req.getIp());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//remove from map
|
|
|
if(CollectionUtils.isNotEmpty(needRemove)){
|
|
|
needRemove.stream().forEach(ip -> AppReportMap.remove(ip));
|
...
|
...
|
@@ -286,19 +287,20 @@ 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);
|
|
|
boolean cansend = canSend4GlobalScope() && canSend4HeartBeat();
|
|
|
if(cansend){
|
|
|
mailService.send(buildMailContent(packet.getIp(), packet.getScreenType()).toString());
|
|
|
needRemove.add(packet.getIp());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//remove from map
|
|
|
removeAll(needRemove);
|
|
|
//客户端到23点,早上9点还没有关机就邮件报警
|
...
|
...
|
@@ -314,34 +316,42 @@ 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){
|
|
|
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(), true);
|
|
|
mailService.send(buildMailContentOfExistOpenClient(ips).toString());
|
|
|
//移除的意义不大,心跳会不间断地上报
|
|
|
this.removeAll(ips);
|
|
|
logger.info("in sendMailIfExistOpenClient, ips {}", ips);
|
|
|
}
|
|
|
}
|
|
|
//已经发送通知后,需要恢复标识
|
|
|
if(isNoticedOpenClient){
|
|
|
currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
|
|
|
//auto wakeup, reset isNoticedOpenClient if it's false
|
|
|
boolean inBussiness = currentHour < systemConfig.getCloseHour()
|
|
|
&& currentHour >= systemConfig.getOpenHour()
|
|
|
&& isNoticedOpenClient;
|
|
|
boolean inBussiness = currentHour < clientConfig.getCloseHour()
|
|
|
&& currentHour >= clientConfig.getOpenHour();
|
|
|
if(inBussiness){
|
|
|
isNoticedOpenClient = false;
|
|
|
logger.info("in sendMailIfExistOpenClient, reset isNoticedOpenClient success");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public long getAliveDatetime(){
|
|
|
return aliveDatetime;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
StringBuilder buildMailContentOfExistOpenClient(List<String> ips){
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
String ipsStr = StringUtils.join(ips, ",") ;
|
...
|
...
|
@@ -350,6 +360,41 @@ public class MonitorService { |
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
long cnt = 1 << 31 -1 ;
|
|
|
Boolean flag = cnt == Integer.MAX_VALUE;
|
...
|
...
|
|