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;
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.google.common.collect.Lists;
import org.apache.commons.codec.binary.Base64;
@ -26,137 +30,167 @@ import java.util.concurrent.ArrayBlockingQueue;
@Component
public class DingdingMsgSender {
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 secret = "SECfcc020bdc83bb17a2c00f39977b1fbc409ef4188c7beaea11c5caa90eeaf87fd";
/**
* 默认10
*/
private static Integer maxQueueSize = 8;
/**
* 有序队列
*/
private ArrayBlockingQueue<DingTalkTextMsg> msgQueue = new ArrayBlockingQueue<>(maxQueueSize);
private volatile boolean running = false;
private void handleMsg() {
DingTalkTextMsg msg = null;
try {
//阻塞取元素
msg = msgQueue.take();
if (msg != null) {
sendMsg(msg);
} else {
Thread.sleep(1000);
}
} catch (Exception e) {
logger.warn("handleMsg exception,serverUrl:" + msg.getWebHook() + ",msg:" + JSON.toJSONString(msg), e);
}
}
/**
* desc:异步发送消息
*
* @param messageParam
* @return
* @throws IOException
*/
public boolean sendMsgAsync(DingTalkTextMsg messageParam) {
if (!running) {
running = true;
getThread().start();
}
boolean flag = false;
int currentQueueSize = msgQueue.size();
//非阻塞 添加/删除元素
if (currentQueueSize < maxQueueSize) {
flag = msgQueue.offer(messageParam);
} else {
msgQueue.poll();
DingTalkTextMsg param = new DingTalkTextMsg();
param.setContent("待发送消息队列已满,当前队列个数" + msgQueue.size() + "\n" + "最新消息内容:" + JSON.toJSONString(messageParam));
param.setWebHook(messageParam.getWebHook());
sendMsg(param);
}
return flag;
}
/**
* 同步发送报警
*
* @param messageParam
* @return
* @throws IOException
*/
public Result<String> sendMsgSync(DingTalkTextMsg messageParam) {
return sendMsg(messageParam);
}
private Thread getThread() {
return new Thread("MsgSender-Thread") {
@Override
public void run() {
while (running) {
handleMsg();
}
}
};
}
@PreDestroy
public void destroy() {
running = false;
}
/**
* 发送POST 请求
*
* @param param 请求参数JSON格式
* @return
*/
private Result<String> sendMsg(DingTalkTextMsg param) {
if (StringUtils.isBlank(param.getWebHook())) {
param.setWebHook(webHook);
}
if (StringUtils.isBlank(param.getSecret())) {
param.setSecret(secret);
}
Result<String> result = new Result<String>().error();
Long timestamp = System.currentTimeMillis();
try {
String stringToSign = timestamp + "\n" + param.getSecret();
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(param.getSecret().getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
String url = param.getWebHook();
url = url.concat("&timestamp=" + timestamp + "&sign=" + sign);
String jsonStrParam = param.getMsgContent();
result = HttpClientManager.getInstance().sendPostByJSON(url, jsonStrParam);
} 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);
}
}
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 secret = "SECfcc020bdc83bb17a2c00f39977b1fbc409ef4188c7beaea11c5caa90eeaf87fd";
/**
* 默认10
*/
private static Integer maxQueueSize = 8;
/**
* 有序队列
*/
private ArrayBlockingQueue<DingTalkTextMsg> msgQueue = new ArrayBlockingQueue<>(maxQueueSize);
private volatile boolean running = false;
private void handleMsg() {
DingTalkTextMsg msg = null;
try {
//阻塞取元素
msg = msgQueue.take();
if (msg != null) {
//sendMsg(msg);
sendMsgSdk(msg);
} else {
Thread.sleep(1000);
}
} catch (Exception e) {
logger.warn("handleMsg exception,serverUrl:" + msg.getWebHook() + ",msg:" + JSON.toJSONString(msg), e);
}
}
/**
* desc:异步发送消息
*
* @param messageParam
* @return
* @throws IOException
*/
public boolean sendMsgAsync(DingTalkTextMsg messageParam) {
if (!running) {
running = true;
getThread().start();
}
boolean flag = false;
int currentQueueSize = msgQueue.size();
//非阻塞 添加/删除元素
if (currentQueueSize < maxQueueSize) {
flag = msgQueue.offer(messageParam);
} else {
msgQueue.poll();
DingTalkTextMsg param = new DingTalkTextMsg();
param.setContent("待发送消息队列已满,当前队列个数" + msgQueue.size() + "\n" + "最新消息内容:" + JSON.toJSONString(messageParam));
param.setWebHook(messageParam.getWebHook());
sendMsgSdk(param);
}
return flag;
}
/**
* 同步发送报警
*
* @param messageParam
* @return
* @throws IOException
*/
public Result<String> sendMsgSync(DingTalkTextMsg messageParam) {
return sendMsg(messageParam);
}
private Thread getThread() {
return new Thread("MsgSender-Thread") {
@Override
public void run() {
while (running) {
handleMsg();
}
}
};
}
@PreDestroy
public void destroy() {
running = false;
}
/**
* 发送POST 请求
*
* @param param 请求参数JSON格式
* @return
*/
@Deprecated
private Result<String> sendMsg(DingTalkTextMsg param) {
if (StringUtils.isBlank(param.getWebHook())) {
param.setWebHook(webHook);
}
if (StringUtils.isBlank(param.getSecret())) {
param.setSecret(secret);
}
Result<String> result = new Result<String>().error();
Long timestamp = System.currentTimeMillis();
try {
String stringToSign = timestamp + "\n" + param.getSecret();
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(param.getSecret().getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
String url = param.getWebHook();
url = url.concat("&timestamp=" + timestamp + "&sign=" + sign);
String jsonStrParam = param.getMsgContent();
result = HttpClientManager.getInstance().sendPostByJSON(url, jsonStrParam);
} catch (Exception e) {
logger.warn("sendPostByJSON error", e);
}
return result;
}
/**
* 发送POST 请求
*
* @param param 请求参数JSON格式
* @return
*/
private Result<String> sendMsgSdk(DingTalkTextMsg param) {
if (StringUtils.isBlank(param.getWebHook())) {
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