
com.github.javaclub.monitor.DynamicGroupDingTalkSender Maven / Gradle / Ivy
The newest version!
/*
* @(#)DynamicGroupDingTalkSender.java 2019年11月15日
*
* Copyright (c) 2019. All Rights Reserved.
*
*/
package com.github.javaclub.monitor;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.github.javaclub.monitor.util.HttpClientUtils;
import com.github.javaclub.monitor.util.Utils;
/**
* DynamicGroupDingTalkSender
*
* @author Gerald Chen
* @version $Id: DynamicGroupDingTalkSender.java 2019年11月15日 14:03:47 Exp $
*/
public abstract class DynamicGroupDingTalkSender {
static final Logger log = LoggerFactory.getLogger(DynamicGroupDingTalkSender.class);
/**
* 发送markdown类型消息
*/
public boolean sendMarkdown(String content, List atMobiles, boolean isAtAll) {
String webhook = getOneServerUrl();
if(Utils.isBlank(webhook)) {
log.error("钉钉webhook地址尚未配置,请检查代码配置!!!");
return false;
}
JSONObject json = new JSONObject();
json.put("msgtype", "markdown");
JSONObject text = new JSONObject();
text.put("title", "通知消息");
text.put("text", content + " \n\n");
json.put("markdown", text);
// @指定的人 或 所有人
if((null != atMobiles && atMobiles.size() > 0) || isAtAll) {
if(isAtAll) {
JSONObject at = new JSONObject();
at.put("isAtAll", true);
json.put("at", at);
} else {
JSONObject at = new JSONObject();
at.put("atMobiles", atMobiles);
at.put("isAtAll", false);
json.put("at", at);
}
}
String response = HttpClientUtils.postJson(webhook, json.toJSONString());
return checkDingTalkIsOk(response, json);
}
public boolean sendDingTalk(String content) {
String webhook = getOneServerUrl();
if(Utils.isBlank(webhook)) {
log.error("钉钉webhook地址尚未: 配置,请检查代码配置!!!");
return false;
}
JSONObject json = new JSONObject();
json.put("msgtype", "text");
JSONObject text = new JSONObject();
text.put("content", content);
json.put("text", text);
String response = HttpClientUtils.postJson(webhook, json.toJSONString());
return checkDingTalkIsOk(response, json);
}
public boolean sendDingTalk(String content, List atMobiles, boolean isAtAll) {
String webhook = getOneServerUrl();
if(Utils.isBlank(webhook)) {
log.error("钉钉webhook地址尚未配置,请检查代码配置!!!");
return false;
}
JSONObject json = new JSONObject();
json.put("msgtype", "text");
JSONObject text = new JSONObject();
text.put("content", content + " \n");
json.put("text", text);
// @指定的人 或 所有人
if((null != atMobiles && atMobiles.size() > 0) || isAtAll) {
if(isAtAll) {
JSONObject at = new JSONObject();
at.put("isAtAll", true);
json.put("at", at);
} else {
JSONObject at = new JSONObject();
at.put("atMobiles", atMobiles);
at.put("isAtAll", false);
json.put("at", at);
}
}
String response = HttpClientUtils.postJson(webhook, json.toJSONString());
return checkDingTalkIsOk(response, json);
}
/**
* 检查钉钉消息是否发送成功,同时做容灾重试(若有失败)
*/
boolean checkDingTalkIsOk(String response, JSONObject sendJson) {
boolean isDoneOK = false;
try {
JSONObject respJson = JSONObject.parseObject(response);
isDoneOK = isRespDoneOk(respJson);
} catch (Exception e) {
isDoneOK = false;
log.warn("DingTalk message send Exception={}, content={}", e.getMessage(), sendJson.toJSONString());
}
int retryTimes = 0;
while (!isDoneOK && retryTimes < 6) { // 容灾,仅重试6次
String webhook = getOneServerUrl();
try { // 发起重试并忽略异常
String postResp = HttpClientUtils.postJson(webhook, sendJson.toJSONString());
JSONObject respJson = JSONObject.parseObject(postResp);
isDoneOK = isRespDoneOk(respJson);
if(!isDoneOK && retryTimes == 3) { // 已经尝试了3次还失败的话,则切换到另一Group
this.switchServerGroup();
}
} catch (Exception e) {
try {
Thread.sleep(60L);
} catch (Exception ex) {
}
} finally {
retryTimes++;
}
}
return isDoneOK;
}
boolean isRespDoneOk(JSONObject respJson) {
return ("0".equals(String.valueOf(respJson.get("errcode"))))
&& ("ok".equals(String.valueOf(respJson.get("errmsg"))));
}
public abstract void switchServerGroup();
public abstract String getOneServerUrl();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy