Browse Source

错误日志打印

dev
jianjun 3 years ago
parent
commit
cb1a67580a
  1. 300
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DingdingMsgSender.java

300
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DingdingMsgSender.java

@ -1,6 +1,10 @@
package com.epmet.commons.tools.utils; package com.epmet.commons.tools.utils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.epmet.commons.tools.constant.DingDingRobotConstant;
import com.epmet.commons.tools.dto.form.DingTalkTextMsg; import com.epmet.commons.tools.dto.form.DingTalkTextMsg;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
@ -26,137 +30,167 @@ import java.util.concurrent.ArrayBlockingQueue;
@Component @Component
public class DingdingMsgSender { public class DingdingMsgSender {
private final Logger logger = LoggerFactory.getLogger(DingdingMsgSender.class); private final Logger logger = LoggerFactory.getLogger(DingdingMsgSender.class);
//如果不设置则为 开发环境机器人地址 //如果不设置则为 开发环境机器人地址
private static final String webHook = "https://oapi.dingtalk.com/robot/send?access_token=e894e5690f9d6a527722974c71548ff6c0fe29bd956589a09e21b16442a35ed4"; private static final String webHook = "https://oapi.dingtalk.com/robot/send?access_token=e894e5690f9d6a527722974c71548ff6c0fe29bd956589a09e21b16442a35ed4";
private static final String secret = "SECfcc020bdc83bb17a2c00f39977b1fbc409ef4188c7beaea11c5caa90eeaf87fd"; private static final String secret = "SECfcc020bdc83bb17a2c00f39977b1fbc409ef4188c7beaea11c5caa90eeaf87fd";
/** /**
* 默认10 * 默认10
*/ */
private static Integer maxQueueSize = 8; private static Integer maxQueueSize = 8;
/** /**
* 有序队列 * 有序队列
*/ */
private ArrayBlockingQueue<DingTalkTextMsg> msgQueue = new ArrayBlockingQueue<>(maxQueueSize); private ArrayBlockingQueue<DingTalkTextMsg> msgQueue = new ArrayBlockingQueue<>(maxQueueSize);
private volatile boolean running = false; private volatile boolean running = false;
private void handleMsg() { private void handleMsg() {
DingTalkTextMsg msg = null; DingTalkTextMsg msg = null;
try { try {
//阻塞取元素 //阻塞取元素
msg = msgQueue.take(); msg = msgQueue.take();
if (msg != null) { if (msg != null) {
sendMsg(msg); //sendMsg(msg);
} else { sendMsgSdk(msg);
Thread.sleep(1000); } else {
} Thread.sleep(1000);
} catch (Exception e) { }
logger.warn("handleMsg exception,serverUrl:" + msg.getWebHook() + ",msg:" + JSON.toJSONString(msg), e); } catch (Exception e) {
} logger.warn("handleMsg exception,serverUrl:" + msg.getWebHook() + ",msg:" + JSON.toJSONString(msg), e);
} }
}
/**
* desc:异步发送消息 /**
* * desc:异步发送消息
* @param messageParam *
* @return * @param messageParam
* @throws IOException * @return
*/ * @throws IOException
public boolean sendMsgAsync(DingTalkTextMsg messageParam) { */
if (!running) { public boolean sendMsgAsync(DingTalkTextMsg messageParam) {
running = true; if (!running) {
getThread().start(); running = true;
} getThread().start();
boolean flag = false; }
int currentQueueSize = msgQueue.size(); boolean flag = false;
//非阻塞 添加/删除元素 int currentQueueSize = msgQueue.size();
if (currentQueueSize < maxQueueSize) { //非阻塞 添加/删除元素
flag = msgQueue.offer(messageParam); if (currentQueueSize < maxQueueSize) {
} else { flag = msgQueue.offer(messageParam);
msgQueue.poll(); } else {
msgQueue.poll();
DingTalkTextMsg param = new DingTalkTextMsg();
param.setContent("待发送消息队列已满,当前队列个数" + msgQueue.size() + "\n" + "最新消息内容:" + JSON.toJSONString(messageParam)); DingTalkTextMsg param = new DingTalkTextMsg();
param.setWebHook(messageParam.getWebHook()); param.setContent("待发送消息队列已满,当前队列个数" + msgQueue.size() + "\n" + "最新消息内容:" + JSON.toJSONString(messageParam));
sendMsg(param); param.setWebHook(messageParam.getWebHook());
} sendMsgSdk(param);
return flag; }
} return flag;
}
/**
* 同步发送报警 /**
* * 同步发送报警
* @param messageParam *
* @return * @param messageParam
* @throws IOException * @return
*/ * @throws IOException
public Result<String> sendMsgSync(DingTalkTextMsg messageParam) { */
return sendMsg(messageParam); public Result<String> sendMsgSync(DingTalkTextMsg messageParam) {
} return sendMsg(messageParam);
}
private Thread getThread() {
return new Thread("MsgSender-Thread") { private Thread getThread() {
@Override return new Thread("MsgSender-Thread") {
public void run() { @Override
while (running) { public void run() {
handleMsg(); while (running) {
} handleMsg();
} }
}; }
} };
}
@PreDestroy
public void destroy() { @PreDestroy
running = false; public void destroy() {
} running = false;
}
/**
* 发送POST 请求 /**
* * 发送POST 请求
* @param param 请求参数JSON格式 *
* @return * @param param 请求参数JSON格式
*/ * @return
private Result<String> sendMsg(DingTalkTextMsg param) { */
if (StringUtils.isBlank(param.getWebHook())) { @Deprecated
param.setWebHook(webHook); private Result<String> sendMsg(DingTalkTextMsg param) {
} if (StringUtils.isBlank(param.getWebHook())) {
if (StringUtils.isBlank(param.getSecret())) { param.setWebHook(webHook);
param.setSecret(secret); }
} if (StringUtils.isBlank(param.getSecret())) {
Result<String> result = new Result<String>().error(); param.setSecret(secret);
Long timestamp = System.currentTimeMillis(); }
try { Result<String> result = new Result<String>().error();
String stringToSign = timestamp + "\n" + param.getSecret(); Long timestamp = System.currentTimeMillis();
Mac mac = Mac.getInstance("HmacSHA256"); try {
mac.init(new SecretKeySpec(param.getSecret().getBytes(StandardCharsets.UTF_8), "HmacSHA256")); String stringToSign = timestamp + "\n" + param.getSecret();
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)); Mac mac = Mac.getInstance("HmacSHA256");
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); mac.init(new SecretKeySpec(param.getSecret().getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
String url = param.getWebHook(); byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
url = url.concat("&timestamp=" + timestamp + "&sign=" + sign); String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
String jsonStrParam = param.getMsgContent(); String url = param.getWebHook();
result = HttpClientManager.getInstance().sendPostByJSON(url, jsonStrParam); url = url.concat("&timestamp=" + timestamp + "&sign=" + sign);
} catch (Exception e) { String jsonStrParam = param.getMsgContent();
logger.warn("sendPostByJSON error", e); result = HttpClientManager.getInstance().sendPostByJSON(url, jsonStrParam);
} } catch (Exception e) {
return result; logger.warn("sendPostByJSON error", e);
} }
return result;
public static void main(String[] args) { }
for (int i = 0; i < 50; i++) {
/**
DingTalkTextMsg dingTalkTextMsg = new DingTalkTextMsg(); * 发送POST 请求
dingTalkTextMsg.setWebHook(""); *
dingTalkTextMsg.setContent("测试消息" + i); * @param param 请求参数JSON格式
dingTalkTextMsg.setAtMobiles(Lists.newArrayList()); * @return
dingTalkTextMsg.setAtAll(false); */
dingTalkTextMsg.setSecret(""); private Result<String> sendMsgSdk(DingTalkTextMsg param) {
DingdingMsgSender msgSender = new DingdingMsgSender(); if (StringUtils.isBlank(param.getWebHook())) {
msgSender.sendMsgAsync(dingTalkTextMsg); param.setWebHook(webHook);
} }
} if (StringUtils.isBlank(param.getSecret())) {
param.setSecret(secret);
}
Result<String> result = new Result<String>().error();
try {
DingTalkClient client = new DefaultDingTalkClient(StringUtils.isBlank(webHook) ? DingDingRobotConstant.DEPLOY_ROBOT : webHook);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("sdk"+param.getContent());
request.setText(text);
client.execute(request);
} catch (Exception e) {
logger.warn("sendPostByJSON error", e);
}
return result;
}
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
DingTalkTextMsg dingTalkTextMsg = new DingTalkTextMsg();
dingTalkTextMsg.setWebHook("");
dingTalkTextMsg.setContent("测试消息" + i);
dingTalkTextMsg.setAtMobiles(Lists.newArrayList());
dingTalkTextMsg.setAtAll(false);
dingTalkTextMsg.setSecret("");
DingdingMsgSender msgSender = new DingdingMsgSender();
msgSender.sendMsgAsync(dingTalkTextMsg);
}
}
} }

Loading…
Cancel
Save