...
|
...
|
@@ -10,18 +10,21 @@ import com.monitor.influxdb.mapper.MonitorAlarmMapper; |
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/5/3.
|
|
|
*/
|
|
|
@Service("alarmMsgService")
|
|
|
public class AlarmMsgServiceImpl implements AlarmMsgService {
|
|
|
public static final Logger DEBUG = LoggerFactory.getLogger(AlarmMsgService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private HttpRestClientService httpRestClientService;
|
...
|
...
|
@@ -39,6 +42,11 @@ public class AlarmMsgServiceImpl implements AlarmMsgService { |
|
|
|
|
|
private static final int CONTENT_SUB_LENGTH = 250;
|
|
|
|
|
|
|
|
|
public static final String DEFAULT_SENT_LOCK="sent_lock";
|
|
|
|
|
|
private Map<String, Long> msgSentLogMap = new HashMap<>();
|
|
|
|
|
|
/**
|
|
|
* 发送短信
|
|
|
*
|
...
|
...
|
@@ -61,6 +69,13 @@ public class AlarmMsgServiceImpl implements AlarmMsgService { |
|
|
*/
|
|
|
@Override
|
|
|
public boolean sendSms(String type, String content, String alarmInfo, String mobile) {
|
|
|
|
|
|
//无需发送
|
|
|
if (!checkSentOrNo(content, mobile)) {
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
Map<String, String> contentMap = new HashMap<String, String>();
|
|
|
contentMap.put("OperID", sendsmsConfig.getUserName());
|
|
|
contentMap.put("OperPass", sendsmsConfig.getPwd());
|
...
|
...
|
@@ -91,8 +106,8 @@ public class AlarmMsgServiceImpl implements AlarmMsgService { |
|
|
|
|
|
try {
|
|
|
monitorAlarmMapper.insertAlarmMsg(InfluxDBContants.AWS, type, content, alarmInfo, sendStatus ? "successed" : "failed");
|
|
|
}catch (Exception e){
|
|
|
logger.error("insert sms into influxdb failed",e);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("insert sms into influxdb failed", e);
|
|
|
}
|
|
|
|
|
|
return sendStatus;
|
...
|
...
|
@@ -110,7 +125,7 @@ public class AlarmMsgServiceImpl implements AlarmMsgService { |
|
|
smsMap.put("Content", URLEncoder.encode("【有货运维】" + smsMap.get("Content"), "UTF-8"));
|
|
|
String result = httpRestClientService.get(sendsmsConfig.getSendsmsUrl(), smsMap, String.class);
|
|
|
|
|
|
if (result == null || !("01" .equals(result.split(",")[0]) || "00" .equals(result.split(",")[0]) || "03" .equals(result.split(",")[0]))) {
|
|
|
if (result == null || !("01".equals(result.split(",")[0]) || "00".equals(result.split(",")[0]) || "03".equals(result.split(",")[0]))) {
|
|
|
logger.error("发送短信失败,短信内容|" + JSONArray.toJSONString(smsMap));
|
|
|
smsMap.put("Content", URLEncoder.encode("【有货运维】发送短信告警失败!!!", "UTF-8"));
|
|
|
smsMap.put("DesMobile", snsMobileConfig.getBaseMobile());
|
...
|
...
|
@@ -125,4 +140,60 @@ public class AlarmMsgServiceImpl implements AlarmMsgService { |
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private boolean checkSentOrNo(String content, String mobile) {
|
|
|
|
|
|
String msgKey = content + "@" + mobile;
|
|
|
|
|
|
synchronized (DEFAULT_SENT_LOCK) {
|
|
|
//没有发送记录
|
|
|
if (!msgSentLogMap.containsKey(msgKey)) {
|
|
|
|
|
|
msgSentLogMap.put(msgKey, System.currentTimeMillis());
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} else {
|
|
|
Long sentTime = msgSentLogMap.get(msgKey);
|
|
|
|
|
|
Long sentAgainTime = System.currentTimeMillis();
|
|
|
|
|
|
//距离发次发送已超过5分钟
|
|
|
if (5 * 60 * 1000 <= (sentAgainTime - sentTime)) {
|
|
|
|
|
|
msgSentLogMap.put(msgKey, System.currentTimeMillis());
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 清理垃圾的短信发送记录
|
|
|
*/
|
|
|
@Scheduled(fixedRate = 5 * 60 * 1000)
|
|
|
public void clearMsgSentLog() {
|
|
|
|
|
|
synchronized (DEFAULT_SENT_LOCK)
|
|
|
{
|
|
|
for (Map.Entry<String, Long> entry : msgSentLogMap.entrySet()) {
|
|
|
|
|
|
String sentKey = entry.getKey();
|
|
|
|
|
|
Long sentTime = msgSentLogMap.get(sentKey);
|
|
|
|
|
|
Long sentAgainTime = System.currentTimeMillis();
|
|
|
|
|
|
if (5 * 60 * 1000 <= (sentAgainTime - sentTime)) {
|
|
|
|
|
|
msgSentLogMap.remove(sentKey);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|