From 72d10b65df3f8fb7f225b2a4aaddbc860dac81db Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Fri, 26 Nov 2021 21:54:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=92=89=E9=92=89=E6=8A=A5?= =?UTF-8?q?=E8=AD=A6=E9=80=9A=E7=9F=A5=E4=BF=A1=E6=81=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alarm/ding/DingAlarmConstants.java | 81 +++++++++++++++++++ .../{ => ding}/DingSendMessageHandler.java | 63 +++------------ 2 files changed, 93 insertions(+), 51 deletions(-) create mode 100644 hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/ding/DingAlarmConstants.java rename hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/{ => ding}/DingSendMessageHandler.java (61%) diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/ding/DingAlarmConstants.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/ding/DingAlarmConstants.java new file mode 100644 index 00000000..32ed3394 --- /dev/null +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/ding/DingAlarmConstants.java @@ -0,0 +1,81 @@ +package cn.hippo4j.starter.alarm.ding; + +/** + * Ding alarm constants. + * + * @author chen.ma + * @date 2021/11/26 20:03 + */ +public class DingAlarmConstants { + + /** + * 钉钉机器人 Url + */ + public static final String DING_ROBOT_SERVER_URL = "https://oapi.dingtalk.com/robot/send?access_token="; + + /** + * 线程池报警通知标题 + */ + public static final String DING_ALARM_TITLE = "动态线程池告警"; + + /** + * 线程池参数变更通知标题 + */ + public static final String DING_NOTICE_TITLE = "动态线程池通知"; + + /** + * 线程池报警通知文本 + */ + public static final String DING_ALARM_TXT = + "[警报] %s - 动态线程池运行告警 \n\n" + + " --- \n\n " + + "线程池ID:%s \n\n " + + "应用名称:%s \n\n " + + "应用实例:%s \n\n " + + "报警类型:%s \n\n " + + " --- \n\n " + + "核心线程数:%d \n\n " + + "最大线程数:%d \n\n " + + "当前线程数:%d \n\n " + + "活跃线程数:%d \n\n " + + "最大任务数:%d \n\n " + + "线程池任务总量:%d \n\n " + + " --- \n\n " + + "队列类型:%s \n\n " + + "队列容量:%d \n\n " + + "队列元素个数:%d \n\n " + + "队列剩余个数:%d \n\n " + + " --- \n\n " + + "拒绝策略:%s \n\n" + + "拒绝策略执行次数:%d \n\n " + + "OWNER:@%s \n\n" + + "提示:%d 分钟内此线程池不会重复告警(可配置) \n\n" + + " --- \n\n " + + "**播报时间:%s**"; + + /** + * 线程池参数变更通知文本 + */ + public static final String DING_NOTICE_TXT = + "[通知] %s - 动态线程池参数变更 \n\n" + + " --- \n\n " + + "线程池ID:%s \n\n " + + "应用名称:%s \n\n " + + "应用实例:%s \n\n " + + " --- \n\n " + + "核心线程数:%s \n\n " + + "最大线程数:%s \n\n " + + "线程存活时间:%s / SECONDS \n\n" + + " --- \n\n " + + "队列类型:%s \n\n " + + "队列容量:%s \n\n " + + " --- \n\n " + + "AGO 拒绝策略:%s \n\n" + + "NOW 拒绝策略:%s \n\n" + + " --- \n\n " + + "提示:动态线程池配置变更实时通知(无限制) \n\n" + + "OWNER:@%s \n\n" + + " --- \n\n " + + "**播报时间:%s**"; + +} diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/DingSendMessageHandler.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/ding/DingSendMessageHandler.java similarity index 61% rename from hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/DingSendMessageHandler.java rename to hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/ding/DingSendMessageHandler.java index 542d7401..72c944b1 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/DingSendMessageHandler.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/ding/DingSendMessageHandler.java @@ -1,7 +1,10 @@ -package cn.hippo4j.starter.alarm; +package cn.hippo4j.starter.alarm.ding; import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.model.PoolParameterInfo; +import cn.hippo4j.starter.alarm.NotifyDTO; +import cn.hippo4j.starter.alarm.NotifyPlatformEnum; +import cn.hippo4j.starter.alarm.SendMessageHandler; import cn.hippo4j.starter.core.DynamicThreadPoolExecutor; import cn.hippo4j.starter.core.GlobalThreadPoolManage; import cn.hippo4j.starter.toolkit.thread.QueueTypeEnum; @@ -22,6 +25,9 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import static cn.hippo4j.starter.alarm.ding.DingAlarmConstants.DING_ALARM_TXT; +import static cn.hippo4j.starter.alarm.ding.DingAlarmConstants.DING_NOTICE_TXT; + /** * Send ding notification message. * @@ -57,32 +63,7 @@ public class DingSendMessageHandler implements SendMessageHandler { BlockingQueue queue = pool.getQueue(); String text = String.format( - "[警报] %s - 动态线程池运行告警 \n\n" + - " --- \n\n " + - "线程池ID:%s \n\n " + - "应用名称:%s \n\n " + - "应用实例:%s \n\n " + - "报警类型:%s \n\n " + - " --- \n\n " + - "核心线程数:%d \n\n " + - "最大线程数:%d \n\n " + - "当前线程数:%d \n\n " + - "活跃线程数:%d \n\n " + - "最大任务数:%d \n\n " + - "线程池任务总量:%d \n\n " + - " --- \n\n " + - "队列类型:%s \n\n " + - "队列容量:%d \n\n " + - "队列元素个数:%d \n\n " + - "队列剩余个数:%d \n\n " + - " --- \n\n " + - "拒绝策略:%s \n\n" + - "拒绝策略执行次数:%d \n\n " + - "OWNER:@%s \n\n" + - "提示:%d 分钟内此线程池不会重复告警(可配置) \n\n" + - " --- \n\n " + - "**播报时间:%s**", - + DING_ALARM_TXT, // 环境 active.toUpperCase(), // 线程池ID @@ -125,7 +106,7 @@ public class DingSendMessageHandler implements SendMessageHandler { DateUtil.now() ); - execute(notifyConfig, "动态线程池告警", text, receives); + execute(notifyConfig, DingAlarmConstants.DING_ALARM_TITLE, text, receives); } private void dingChangeSendMessage(NotifyDTO notifyConfig, PoolParameterInfo parameter) { @@ -144,26 +125,7 @@ public class DingSendMessageHandler implements SendMessageHandler { * hesitant e.g. ➲ ➜ ⇨ ➪ */ String text = String.format( - "[通知] %s - 动态线程池参数变更 \n\n" + - " --- \n\n " + - "线程池ID:%s \n\n " + - "应用名称:%s \n\n " + - "应用实例:%s \n\n " + - " --- \n\n " + - "核心线程数:%s \n\n " + - "最大线程数:%s \n\n " + - "线程存活时间:%s / SECONDS \n\n" + - " --- \n\n " + - "队列类型:%s \n\n " + - "队列容量:%s \n\n " + - " --- \n\n " + - "AGO 拒绝策略:%s \n\n" + - "NOW 拒绝策略:%s \n\n" + - " --- \n\n " + - "提示:动态线程池配置变更实时通知(无限制) \n\n" + - "OWNER:@%s \n\n" + - " --- \n\n " + - "**播报时间:%s**", + DING_NOTICE_TXT, // 环境 active.toUpperCase(), // 线程池名称 @@ -191,12 +153,11 @@ public class DingSendMessageHandler implements SendMessageHandler { DateUtil.now() ); - execute(notifyConfig, "动态线程池通知", text, receives); + execute(notifyConfig, DingAlarmConstants.DING_NOTICE_TITLE, text, receives); } private void execute(NotifyDTO notifyConfig, String title, String text, List mobiles) { - String url = "https://oapi.dingtalk.com/robot/send?access_token="; - String serverUrl = url + notifyConfig.getSecretKey(); + String serverUrl = DingAlarmConstants.DING_ROBOT_SERVER_URL + notifyConfig.getSecretKey(); DingTalkClient dingTalkClient = new DefaultDingTalkClient(serverUrl); OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype("markdown");