企业微信通知和飞书通知增加发送失败的日志打印

This commit is contained in:
lizhongyuan3 2023-12-18 13:53:36 +08:00 committed by byteblogs168
parent f5d40944f8
commit c923d0a1db
2 changed files with 16 additions and 12 deletions

View File

@ -36,7 +36,7 @@ import java.util.Map;
public class LarkAlarm extends AbstractAlarm<AlarmContext> {
public static final String atLabel = "<at id={0}></at>";
public static final String AT_LABEL = "<at id={0}></at>";
@Override
public Integer getAlarmType() {
@ -45,9 +45,7 @@ public class LarkAlarm extends AbstractAlarm<AlarmContext> {
@Override
public boolean asyncSendMessage(AlarmContext context) {
threadPoolExecutor.execute(() -> {
syncSendMessage(context);
});
threadPoolExecutor.execute(() -> syncSendMessage(context));
return true;
}
@ -69,12 +67,15 @@ public class LarkAlarm extends AbstractAlarm<AlarmContext> {
HttpRequest request = post.body(JsonUtil.toJsonString(builder), ContentType.JSON.toString());
HttpResponse execute = request.execute();
LogUtils.debug(log, JsonUtil.toJsonString(execute));
if (execute.isOk()) {
return true;
}
log.error("发送lark消息失败:{}", execute.body());
return false;
} catch (Exception e) {
log.error("发送lark消息失败", e);
return false;
}
return true;
}
private List buildElements(String text, List<String> ats) {
@ -123,10 +124,10 @@ public class LarkAlarm extends AbstractAlarm<AlarmContext> {
StringBuilder sb = new StringBuilder(text);
if (ats.stream().map(String::toLowerCase).anyMatch(SystemConstants.AT_ALL::equals)) {
sb.append(MessageFormat.format(atLabel, SystemConstants.AT_ALL));
sb.append(MessageFormat.format(AT_LABEL, SystemConstants.AT_ALL));
} else {
ats.stream().filter(StrUtil::isNotBlank)
.forEach(at -> sb.append(MessageFormat.format(atLabel, at)));
.forEach(at -> sb.append(MessageFormat.format(AT_LABEL, at)));
}
return sb.toString();
}

View File

@ -26,7 +26,7 @@ import java.util.Map;
@Component
public class QiYeWechatAlarm extends AbstractAlarm<AlarmContext> {
public static final String atLabel = "<@{0}>";
public static final String AT_LABEL = "<@{0}>";
@Override
public Integer getAlarmType() {
@ -50,19 +50,22 @@ public class QiYeWechatAlarm extends AbstractAlarm<AlarmContext> {
}
Map<String, Object> map = MapUtil.newHashMap();
QiYeWechatMessageContent messageContent = new QiYeWechatMessageContent();
messageContent.setContent(StrUtil.sub(DingDingUtils.getAtText(qiYeWechatAttribute.getAts(), context.getText(), atLabel), 0, 4096));
messageContent.setContent(StrUtil.sub(DingDingUtils.getAtText(qiYeWechatAttribute.getAts(), context.getText(), AT_LABEL), 0, 4096));
map.put("msgtype", "markdown");
map.put("markdown", messageContent);
HttpRequest post = HttpUtil.createPost(webhookUrl);
HttpRequest request = post.body(JsonUtil.toJsonString(map), ContentType.JSON.toString());
HttpResponse execute = request.execute();
LogUtils.debug(log, JsonUtil.toJsonString(execute));
if (execute.isOk()) {
return true;
}
log.error("发送企业微信消息失败:{}", execute.body());
return false;
} catch (Exception e) {
log.error("发送企业微信消息失败", e);
return false;
}
return true;
}
@Override