diff --git a/hippo4j-message/pom.xml b/hippo4j-message/pom.xml index 7d88e83486..0c4a74924b 100644 --- a/hippo4j-message/pom.xml +++ b/hippo4j-message/pom.xml @@ -50,6 +50,15 @@ + + + src/main/resources + + **/*.txt + **/*.json + + + org.apache.maven.plugins diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/DingSendMessageHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/DingSendMessageHandler.java index b0f5b9d5ea..4c3c7322c5 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/DingSendMessageHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/DingSendMessageHandler.java @@ -20,17 +20,14 @@ import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.message.dto.NotifyConfigDTO; import cn.hippo4j.message.enums.NotifyPlatformEnum; -import cn.hippo4j.message.enums.NotifyTypeEnum; +import cn.hippo4j.message.platform.base.AbstractRobotSendMessageHandler; +import cn.hippo4j.message.platform.base.RobotMessageActualContent; +import cn.hippo4j.message.platform.base.RobotMessageExecuteDTO; import cn.hippo4j.message.platform.constant.DingAlarmConstants; -import cn.hippo4j.message.request.AlarmNotifyRequest; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; -import cn.hippo4j.message.service.SendMessageHandler; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.StrUtil; +import cn.hutool.core.io.FileUtil; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest; -import com.google.common.base.Joiner; import com.google.common.collect.Lists; import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; @@ -40,7 +37,6 @@ import javax.crypto.spec.SecretKeySpec; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.List; import java.util.Objects; import static cn.hippo4j.message.platform.constant.DingAlarmConstants.*; @@ -49,7 +45,7 @@ * Send ding notification message. */ @Slf4j -public class DingSendMessageHandler implements SendMessageHandler { +public class DingSendMessageHandler extends AbstractRobotSendMessageHandler { @Override public String getType() { @@ -57,114 +53,21 @@ public String getType() { } @Override - public void sendAlarmMessage(NotifyConfigDTO notifyConfig, AlarmNotifyRequest alarmNotifyRequest) { - String[] receives = notifyConfig.getReceives().split(","); - String afterReceives = Joiner.on(", @").join(receives); - String dingAlarmTxt; - String dingAlarmTimoutReplaceTxt; - if (Objects.equals(alarmNotifyRequest.getNotifyTypeEnum(), NotifyTypeEnum.TIMEOUT)) { - String executeTimeoutTrace = alarmNotifyRequest.getExecuteTimeoutTrace(); - if (StringUtil.isNotBlank(executeTimeoutTrace)) { - String dingAlarmTimoutTraceReplaceTxt = String.format(DING_ALARM_TIMOUT_TRACE_REPLACE_TXT, executeTimeoutTrace); - dingAlarmTimoutReplaceTxt = StrUtil.replace(DING_ALARM_TIMOUT_REPLACE_TXT, DING_ALARM_TIMOUT_TRACE_REPLACE_TXT, dingAlarmTimoutTraceReplaceTxt); - } else { - dingAlarmTimoutReplaceTxt = StrUtil.replace(DING_ALARM_TIMOUT_REPLACE_TXT, DING_ALARM_TIMOUT_TRACE_REPLACE_TXT, ""); - } - dingAlarmTimoutReplaceTxt = String.format(dingAlarmTimoutReplaceTxt, alarmNotifyRequest.getExecuteTime(), alarmNotifyRequest.getExecuteTimeOut()); - dingAlarmTxt = StrUtil.replace(DING_ALARM_TXT, DING_ALARM_TIMOUT_REPLACE_TXT, dingAlarmTimoutReplaceTxt); - } else { - dingAlarmTxt = StrUtil.replace(DING_ALARM_TXT, DING_ALARM_TIMOUT_REPLACE_TXT, ""); - } - - String[] strings = alarmNotifyRequest.getIdentify().split("_"); - String text = String.format( - dingAlarmTxt, - // 环境 - alarmNotifyRequest.getActive(), - // 报警类型 - alarmNotifyRequest.getNotifyTypeEnum(), - // 线程池ID - alarmNotifyRequest.getThreadPoolId(), - // 应用名称 - alarmNotifyRequest.getAppName(), - // Host - strings[0], - // 实例ID - strings[1], - // 核心线程数 - alarmNotifyRequest.getCorePoolSize(), - // 最大线程数 - alarmNotifyRequest.getMaximumPoolSize(), - // 当前线程数 - alarmNotifyRequest.getPoolSize(), - // 活跃线程数 - alarmNotifyRequest.getActiveCount(), - // 最大任务数 - alarmNotifyRequest.getLargestPoolSize(), - // 线程池任务总量 - alarmNotifyRequest.getCompletedTaskCount(), - // 队列类型名称 - alarmNotifyRequest.getQueueName(), - // 队列容量 - alarmNotifyRequest.getCapacity(), - // 队列元素个数 - alarmNotifyRequest.getQueueSize(), - // 队列剩余个数 - alarmNotifyRequest.getRemainingCapacity(), - // 拒绝策略名称 - alarmNotifyRequest.getRejectedExecutionHandlerName(), - // 拒绝策略次数 - alarmNotifyRequest.getRejectCountNum(), - // 告警手机号 - afterReceives, - // 报警频率 - notifyConfig.getInterval(), - // 当前时间 - DateUtil.now()); - execute(notifyConfig, DingAlarmConstants.DING_ALARM_TITLE, text, Lists.newArrayList(receives)); + protected RobotMessageActualContent buildMessageActualContent() { + RobotMessageActualContent robotMessageActualContent = RobotMessageActualContent.builder() + .receiveSeparator(", @") + .changeSeparator(" -> ") + .replaceTxt(DING_ALARM_TIMOUT_REPLACE_TXT) + .traceReplaceTxt(DING_ALARM_TIMOUT_TRACE_REPLACE_TXT) + .alarmMessageContent(FileUtil.readUtf8String("message/robot/dynamic-thread-pool/ding-alarm.txt")) + .configMessageContent(FileUtil.readUtf8String("message/robot/dynamic-thread-pool/ding-config.txt")) + .build(); + return robotMessageActualContent; } @Override - public void sendChangeMessage(NotifyConfigDTO notifyConfig, ChangeParameterNotifyRequest changeParameterNotifyRequest) { - String threadPoolId = changeParameterNotifyRequest.getThreadPoolId(); - String[] receives = notifyConfig.getReceives().split(","); - String afterReceives = Joiner.on(", @").join(receives); - String text = String.format( - DING_NOTICE_TXT, - // 环境 - changeParameterNotifyRequest.getActive(), - // 线程池名称 - threadPoolId, - // 应用名称 - changeParameterNotifyRequest.getAppName(), - // 实例信息 - changeParameterNotifyRequest.getIdentify(), - // 核心线程数 - changeParameterNotifyRequest.getBeforeCorePoolSize() + " ➲ " + changeParameterNotifyRequest.getNowCorePoolSize(), - // 最大线程数 - changeParameterNotifyRequest.getBeforeMaximumPoolSize() + " ➲ " + changeParameterNotifyRequest.getNowMaximumPoolSize(), - // 核心线程超时 - changeParameterNotifyRequest.getBeforeAllowsCoreThreadTimeOut() + " ➲ " + changeParameterNotifyRequest.getNowAllowsCoreThreadTimeOut(), - // 线程存活时间 - changeParameterNotifyRequest.getBeforeKeepAliveTime() + " ➲ " + changeParameterNotifyRequest.getNowKeepAliveTime(), - // 执行超时时间 - changeParameterNotifyRequest.getBeforeExecuteTimeOut() + " ➲ " + changeParameterNotifyRequest.getNowExecuteTimeOut(), - // 阻塞队列 - changeParameterNotifyRequest.getBlockingQueueName(), - // 阻塞队列容量 - changeParameterNotifyRequest.getBeforeQueueCapacity() + " ➲ " + changeParameterNotifyRequest.getNowQueueCapacity(), - // 拒绝策略 - changeParameterNotifyRequest.getBeforeRejectedName(), - changeParameterNotifyRequest.getNowRejectedName(), - // 告警手机号 - afterReceives, - // 当前时间 - DateUtil.now()); - - execute(notifyConfig, DingAlarmConstants.DING_NOTICE_TITLE, text, Lists.newArrayList(receives)); - } - - private void execute(NotifyConfigDTO notifyConfig, String title, String text, List mobiles) { + protected void execute(RobotMessageExecuteDTO robotMessageExecuteDTO) { + NotifyConfigDTO notifyConfig = robotMessageExecuteDTO.getNotifyConfig(); String serverUrl = DingAlarmConstants.DING_ROBOT_SERVER_URL + notifyConfig.getSecretKey(); String secret = notifyConfig.getSecret(); if (StringUtil.isNotBlank(secret)) { @@ -184,10 +87,10 @@ private void execute(NotifyConfigDTO notifyConfig, String title, String text, Li OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype("markdown"); OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); - markdown.setTitle(title); - markdown.setText(text); + markdown.setTitle(Objects.equals(notifyConfig.getType(), "CONFIG") ? DING_NOTICE_TITLE : DING_ALARM_TITLE); + markdown.setText(robotMessageExecuteDTO.getText()); OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); - at.setAtMobiles(mobiles); + at.setAtMobiles(Lists.newArrayList(notifyConfig.getReceives().split(","))); request.setAt(at); request.setMarkdown(markdown); try { diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java index 5afe9ecbd3..c09beb1d22 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java @@ -18,30 +18,23 @@ package cn.hippo4j.message.platform; import cn.hippo4j.common.toolkit.JSONUtil; -import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.message.dto.NotifyConfigDTO; import cn.hippo4j.message.enums.NotifyPlatformEnum; -import cn.hippo4j.message.enums.NotifyTypeEnum; -import cn.hippo4j.message.service.SendMessageHandler; -import cn.hippo4j.message.request.AlarmNotifyRequest; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.StrUtil; +import cn.hippo4j.message.platform.base.AbstractRobotSendMessageHandler; +import cn.hippo4j.message.platform.base.RobotMessageActualContent; +import cn.hippo4j.message.platform.base.RobotMessageExecuteDTO; +import cn.hutool.core.io.FileUtil; import cn.hutool.http.HttpRequest; -import com.google.common.base.Joiner; import lombok.Data; import lombok.experimental.Accessors; import lombok.extern.slf4j.Slf4j; -import java.util.Objects; - import static cn.hippo4j.message.platform.constant.WeChatAlarmConstants.*; /** * WeChat send message handler. */ @Slf4j -public class WeChatSendMessageHandler implements SendMessageHandler { +public class WeChatSendMessageHandler extends AbstractRobotSendMessageHandler { @Override public String getType() { @@ -49,122 +42,26 @@ public String getType() { } @Override - public void sendAlarmMessage(NotifyConfigDTO notifyConfig, AlarmNotifyRequest alarmNotifyRequest) { - String[] receives = notifyConfig.getReceives().split(","); - String afterReceives = Joiner.on("><@").join(receives); - String weChatAlarmTxt; - String weChatAlarmTimoutReplaceTxt; - if (Objects.equals(alarmNotifyRequest.getNotifyTypeEnum(), NotifyTypeEnum.TIMEOUT)) { - String executeTimeoutTrace = alarmNotifyRequest.getExecuteTimeoutTrace(); - if (StringUtil.isNotBlank(executeTimeoutTrace)) { - String weChatAlarmTimoutTraceReplaceTxt = String.format(WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT, executeTimeoutTrace); - weChatAlarmTimoutReplaceTxt = StrUtil.replace(WE_CHAT_ALARM_TIMOUT_REPLACE_TXT, WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT, weChatAlarmTimoutTraceReplaceTxt); - } else { - weChatAlarmTimoutReplaceTxt = StrUtil.replace(WE_CHAT_ALARM_TIMOUT_REPLACE_TXT, WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT, ""); - } - weChatAlarmTimoutReplaceTxt = String.format(weChatAlarmTimoutReplaceTxt, alarmNotifyRequest.getExecuteTime(), alarmNotifyRequest.getExecuteTimeOut()); - weChatAlarmTxt = StrUtil.replace(WE_CHAT_ALARM_TXT, WE_CHAT_ALARM_TIMOUT_REPLACE_TXT, weChatAlarmTimoutReplaceTxt); - } else { - weChatAlarmTxt = StrUtil.replace(WE_CHAT_ALARM_TXT, WE_CHAT_ALARM_TIMOUT_REPLACE_TXT, ""); - } - - String text = String.format( - weChatAlarmTxt, - // 环境 - alarmNotifyRequest.getActive(), - // 报警类型 - alarmNotifyRequest.getNotifyTypeEnum(), - // 线程池ID - alarmNotifyRequest.getThreadPoolId(), - // 应用名称 - alarmNotifyRequest.getAppName(), - // 实例信息 - alarmNotifyRequest.getIdentify(), - // 核心线程数 - alarmNotifyRequest.getCorePoolSize(), - // 最大线程数 - alarmNotifyRequest.getMaximumPoolSize(), - // 当前线程数 - alarmNotifyRequest.getPoolSize(), - // 活跃线程数 - alarmNotifyRequest.getActiveCount(), - // 最大任务数 - alarmNotifyRequest.getLargestPoolSize(), - // 线程池任务总量 - alarmNotifyRequest.getCompletedTaskCount(), - // 队列类型名称 - alarmNotifyRequest.getQueueName(), - // 队列容量 - alarmNotifyRequest.getCapacity(), - // 队列元素个数 - alarmNotifyRequest.getQueueSize(), - // 队列剩余个数 - alarmNotifyRequest.getRemainingCapacity(), - // 拒绝策略名称 - alarmNotifyRequest.getRejectedExecutionHandlerName(), - // 拒绝策略次数 - alarmNotifyRequest.getRejectCountNum(), - // 告警手机号 - afterReceives, - // 报警频率 - notifyConfig.getInterval(), - // 当前时间 - DateUtil.now()); - execute(notifyConfig.getSecretKey(), text); + protected RobotMessageActualContent buildMessageActualContent() { + RobotMessageActualContent robotMessageActualContent = RobotMessageActualContent.builder() + .receiveSeparator("><@") + .changeSeparator(" ➲ ") + .replaceTxt(WE_CHAT_ALARM_TIMOUT_REPLACE_TXT) + .traceReplaceTxt(WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT) + .alarmMessageContent(FileUtil.readUtf8String("message/robot/dynamic-thread-pool/wechat-alarm.txt")) + .configMessageContent(FileUtil.readUtf8String("message/robot/dynamic-thread-pool/wechat-config.txt")) + .build(); + return robotMessageActualContent; } @Override - public void sendChangeMessage(NotifyConfigDTO notifyConfig, ChangeParameterNotifyRequest changeParameterNotifyRequest) { - String threadPoolId = changeParameterNotifyRequest.getThreadPoolId(); - String[] receives = notifyConfig.getReceives().split(","); - String afterReceives = Joiner.on("><@").join(receives); - String text = String.format( - WE_CHAT_NOTICE_TXT, - // 环境 - changeParameterNotifyRequest.getActive(), - // 线程池名称 - threadPoolId, - // 应用名称 - changeParameterNotifyRequest.getAppName(), - // 实例信息 - changeParameterNotifyRequest.getIdentify(), - // 核心线程数 - changeParameterNotifyRequest.getBeforeCorePoolSize() + " ➲ " + changeParameterNotifyRequest.getNowCorePoolSize(), - // 最大线程数 - changeParameterNotifyRequest.getBeforeMaximumPoolSize() + " ➲ " + changeParameterNotifyRequest.getNowMaximumPoolSize(), - // 核心线程超时 - changeParameterNotifyRequest.getBeforeAllowsCoreThreadTimeOut() + " ➲ " + changeParameterNotifyRequest.getNowAllowsCoreThreadTimeOut(), - // 线程存活时间 - changeParameterNotifyRequest.getBeforeKeepAliveTime() + " ➲ " + changeParameterNotifyRequest.getNowKeepAliveTime(), - // 执行超时时间 - changeParameterNotifyRequest.getBeforeExecuteTimeOut() + " ➲ " + changeParameterNotifyRequest.getNowExecuteTimeOut(), - // 阻塞队列 - changeParameterNotifyRequest.getBlockingQueueName(), - // 阻塞队列容量 - changeParameterNotifyRequest.getBeforeQueueCapacity() + " ➲ " + changeParameterNotifyRequest.getNowQueueCapacity(), - // 拒绝策略 - changeParameterNotifyRequest.getBeforeRejectedName(), - changeParameterNotifyRequest.getNowRejectedName(), - // 告警手机号 - afterReceives, - // 当前时间 - DateUtil.now()); - execute(notifyConfig.getSecretKey(), text); - } - - /** - * Execute. - * - * @param secretKey - * @param text - */ - private void execute(String secretKey, String text) { - String serverUrl = WE_CHAT_SERVER_URL + secretKey; + protected void execute(RobotMessageExecuteDTO robotMessageExecuteDTO) { + String serverUrl = WE_CHAT_SERVER_URL + robotMessageExecuteDTO.getNotifyConfig().getSecretKey(); try { WeChatReqDTO weChatReq = new WeChatReqDTO(); weChatReq.setMsgtype("markdown"); Markdown markdown = new Markdown(); - markdown.setContent(text); + markdown.setContent(robotMessageExecuteDTO.getText()); weChatReq.setMarkdown(markdown); HttpRequest.post(serverUrl).body(JSONUtil.toJSONString(weChatReq)).execute(); } catch (Exception ex) { diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java new file mode 100644 index 0000000000..12423ff3c9 --- /dev/null +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java @@ -0,0 +1,164 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.message.platform.base; + +import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.message.dto.NotifyConfigDTO; +import cn.hippo4j.message.enums.NotifyTypeEnum; +import cn.hippo4j.message.request.AlarmNotifyRequest; +import cn.hippo4j.message.request.ChangeParameterNotifyRequest; +import cn.hippo4j.message.service.SendMessageHandler; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.StrUtil; +import com.google.common.base.Joiner; + +import java.util.Objects; + +/** + * Abstract robot send message handler. + */ +public abstract class AbstractRobotSendMessageHandler implements SendMessageHandler { + + /** + * Build message actual content. + * + * @return + */ + protected abstract RobotMessageActualContent buildMessageActualContent(); + + /** + * Execute. + * + * @param robotMessageExecuteDTO + */ + protected abstract void execute(RobotMessageExecuteDTO robotMessageExecuteDTO); + + /** + * Send alarm message. + * + * @param notifyConfig + * @param alarmNotifyRequest + */ + public void sendAlarmMessage(NotifyConfigDTO notifyConfig, AlarmNotifyRequest alarmNotifyRequest) { + RobotMessageActualContent robotMessageActualContent = buildMessageActualContent(); + String replaceTxt = robotMessageActualContent.getReplaceTxt(); + String traceReplaceTxt = robotMessageActualContent.getTraceReplaceTxt(); + String alarmContentTxt = robotMessageActualContent.getAlarmMessageContent(); + String alarmTimoutReplaceTxt; + if (Objects.equals(alarmNotifyRequest.getNotifyTypeEnum(), NotifyTypeEnum.TIMEOUT)) { + String executeTimeoutTrace = alarmNotifyRequest.getExecuteTimeoutTrace(); + if (StringUtil.isNotBlank(executeTimeoutTrace)) { + String alarmTimoutTraceReplaceTxt = String.format(traceReplaceTxt, executeTimeoutTrace); + alarmTimoutReplaceTxt = StrUtil.replace(replaceTxt, traceReplaceTxt, alarmTimoutTraceReplaceTxt); + } else { + alarmTimoutReplaceTxt = StrUtil.replace(replaceTxt, traceReplaceTxt, ""); + } + replaceTxt = String.format(alarmTimoutReplaceTxt, alarmNotifyRequest.getExecuteTime(), alarmNotifyRequest.getExecuteTimeOut()); + } else { + replaceTxt = ""; + } + alarmContentTxt = StrUtil.replace(alarmContentTxt, "${timout-content}", replaceTxt); + String text = String.format( + alarmContentTxt, + // 环境 + alarmNotifyRequest.getActive(), + // 报警类型 + alarmNotifyRequest.getNotifyTypeEnum(), + // 线程池ID + alarmNotifyRequest.getThreadPoolId(), + // 应用名称 + alarmNotifyRequest.getAppName(), + // 实例信息 + alarmNotifyRequest.getIdentify(), + // 核心线程数 + alarmNotifyRequest.getCorePoolSize(), + // 最大线程数 + alarmNotifyRequest.getMaximumPoolSize(), + // 当前线程数 + alarmNotifyRequest.getPoolSize(), + // 活跃线程数 + alarmNotifyRequest.getActiveCount(), + // 最大任务数 + alarmNotifyRequest.getLargestPoolSize(), + // 线程池任务总量 + alarmNotifyRequest.getCompletedTaskCount(), + // 队列类型名称 + alarmNotifyRequest.getQueueName(), + // 队列容量 + alarmNotifyRequest.getCapacity(), + // 队列元素个数 + alarmNotifyRequest.getQueueSize(), + // 队列剩余个数 + alarmNotifyRequest.getRemainingCapacity(), + // 拒绝策略名称 + alarmNotifyRequest.getRejectedExecutionHandlerName(), + // 拒绝策略次数 + alarmNotifyRequest.getRejectCountNum(), + // 告警手机号 + Joiner.on(robotMessageActualContent.getReceiveSeparator()).join(notifyConfig.getReceives().split(",")), + // 报警频率 + notifyConfig.getInterval(), + // 当前时间 + DateUtil.now()); + execute(RobotMessageExecuteDTO.builder().text(text).notifyConfig(notifyConfig).build()); + } + + /** + * Send change message. + * + * @param notifyConfig + * @param changeParameterNotifyRequest + */ + public void sendChangeMessage(NotifyConfigDTO notifyConfig, ChangeParameterNotifyRequest changeParameterNotifyRequest) { + RobotMessageActualContent robotMessageActualContent = buildMessageActualContent(); + String threadPoolId = changeParameterNotifyRequest.getThreadPoolId(); + String changeSeparator = robotMessageActualContent.getChangeSeparator(); + String text = String.format( + robotMessageActualContent.getConfigMessageContent(), + // 环境 + changeParameterNotifyRequest.getActive(), + // 线程池名称 + threadPoolId, + // 应用名称 + changeParameterNotifyRequest.getAppName(), + // 实例信息 + changeParameterNotifyRequest.getIdentify(), + // 核心线程数 + changeParameterNotifyRequest.getBeforeCorePoolSize() + changeSeparator + changeParameterNotifyRequest.getNowCorePoolSize(), + // 最大线程数 + changeParameterNotifyRequest.getBeforeMaximumPoolSize() + changeSeparator + changeParameterNotifyRequest.getNowMaximumPoolSize(), + // 核心线程超时 + changeParameterNotifyRequest.getBeforeAllowsCoreThreadTimeOut() + changeSeparator + changeParameterNotifyRequest.getNowAllowsCoreThreadTimeOut(), + // 线程存活时间 + changeParameterNotifyRequest.getBeforeKeepAliveTime() + changeSeparator + changeParameterNotifyRequest.getNowKeepAliveTime(), + // 执行超时时间 + changeParameterNotifyRequest.getBeforeExecuteTimeOut() + changeSeparator + changeParameterNotifyRequest.getNowExecuteTimeOut(), + // 阻塞队列 + changeParameterNotifyRequest.getBlockingQueueName(), + // 阻塞队列容量 + changeParameterNotifyRequest.getBeforeQueueCapacity() + changeSeparator + changeParameterNotifyRequest.getNowQueueCapacity(), + // 拒绝策略 + changeParameterNotifyRequest.getBeforeRejectedName(), + changeParameterNotifyRequest.getNowRejectedName(), + // 告警手机号 + Joiner.on(robotMessageActualContent.getReceiveSeparator()).join(notifyConfig.getReceives().split(",")), + // 当前时间 + DateUtil.now()); + execute(RobotMessageExecuteDTO.builder().text(text).notifyConfig(notifyConfig).build()); + } +} diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageActualContent.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageActualContent.java new file mode 100644 index 0000000000..fb48320637 --- /dev/null +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageActualContent.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.message.platform.base; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Robot message actual content. + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class RobotMessageActualContent { + + /** + * Alarm message content + */ + private String alarmMessageContent; + + /** + * Config message content + */ + private String configMessageContent; + + /** + * Replace txt + */ + private String replaceTxt; + + /** + * Trace replace txt + */ + private String traceReplaceTxt; + + /** + * Receive separator + */ + private String receiveSeparator; + + /** + * Change separator + */ + private String changeSeparator; +} diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageExecuteDTO.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageExecuteDTO.java new file mode 100644 index 0000000000..29191d6ff8 --- /dev/null +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageExecuteDTO.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.message.platform.base; + +import cn.hippo4j.message.dto.NotifyConfigDTO; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Robot message execute DTO. + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class RobotMessageExecuteDTO { + + /** + * Text + */ + private String text; + + /** + * Notify config + */ + private NotifyConfigDTO notifyConfig; +} diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/constant/DingAlarmConstants.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/constant/DingAlarmConstants.java index 92c131bc52..e3f9af357a 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/constant/DingAlarmConstants.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/constant/DingAlarmConstants.java @@ -40,16 +40,16 @@ public class DingAlarmConstants { /** * Trace 信息 */ - public static final String DING_ALARM_TIMOUT_TRACE_REPLACE_TXT = "链路信息:%s \n\n"; + public static final String DING_ALARM_TIMOUT_TRACE_REPLACE_TXT = "链路信息:%s \n\n "; /** * 替换任务超时模板 */ public static final String DING_ALARM_TIMOUT_REPLACE_TXT = - "任务执行时间:%d / ms \n\n" + - "超时时间:%d / ms \n\n" + + "任务执行时间:%d / ms \n\n " + + "超时时间:%d / ms \n\n " + DING_ALARM_TIMOUT_TRACE_REPLACE_TXT + - " --- \n\n "; + " --- \n\n "; /** * 线程池报警通知文本 @@ -60,7 +60,6 @@ public class DingAlarmConstants { "线程池ID:%s \n\n " + "应用名称:%s \n\n " + "应用实例:%s \n\n " + - "实例标识:%s \n\n " + " --- \n\n " + "核心线程数:%d \n\n " + "最大线程数:%d \n\n " + diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/constant/WeChatAlarmConstants.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/constant/WeChatAlarmConstants.java index 3394ad2e0e..83bc5182ea 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/constant/WeChatAlarmConstants.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/constant/WeChatAlarmConstants.java @@ -30,59 +30,13 @@ public class WeChatAlarmConstants { /** * Trace 信息 */ - public static final String WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT = "> 链路信息:%s \n"; + public static final String WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT = "\n> 链路信息:%s "; /** * 替换任务超时模板 */ public static final String WE_CHAT_ALARM_TIMOUT_REPLACE_TXT = - "> 任务执行时间:%s / ms \n" + - "> 超时时间:%s / ms \n" + + "\n> 任务执行时间:%s / ms \n" + + "> 超时时间:%s / ms " + WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT; - - /** - * 线程池报警通知文本 - */ - public static final String WE_CHAT_ALARM_TXT = - "### [警报] %s - 动态线程池运行告警(%s) \n" + - "> 线程池ID:%s \n" + - "> 应用名称:%s \n" + - "> 应用实例:%s \n" + - "> 核心线程数:%s \n" + - "> 最大线程数:%s \n" + - "> 当前线程数:%s \n" + - "> 活跃线程数:%s \n" + - "> 同存最大线程数:%s \n" + - "> 线程池任务总量:%s \n" + - "> 队列类型:%s \n" + - "> 队列容量:%s \n" + - "> 队列元素个数:%s \n" + - "> 队列剩余个数:%s \n" + - "> 拒绝策略:%s \n" + - "> 拒绝策略执行次数:%s \n" + - WE_CHAT_ALARM_TIMOUT_REPLACE_TXT + - "> OWNER:<@%s> \n" + - "> 提示:%d 分钟内此线程池不会重复告警(可配置) \n\n" + - "**播报时间:%s**"; - - /** - * 线程池参数变更通知文本 - */ - public static final String WE_CHAT_NOTICE_TXT = - "### [通知] %s - 动态线程池参数变更 \n" + - "> 线程池ID:%s \n" + - "> 应用名称:%s \n" + - "> 应用实例:%s \n" + - "> 核心线程数:%s \n" + - "> 最大线程数:%s \n" + - "> 核心线程超时:%s \n" + - "> 线程存活时间:%s \n" + - "> 执行超时时间:%s \n" + - "> 队列类型:%s \n" + - "> 队列容量:%s \n" + - "> AGO 拒绝策略:%s \n" + - "> NOW 拒绝策略:%s \n" + - "> OWNER:<@%s> \n" + - "> 提示:动态线程池配置变更实时通知(无限制) \n\n" + - "**播报时间:%s**"; } diff --git a/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/ding-alarm.txt b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/ding-alarm.txt new file mode 100644 index 0000000000..55064be3b4 --- /dev/null +++ b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/ding-alarm.txt @@ -0,0 +1,49 @@ +**[警报] %s - 动态线程池运行告警(%s)** + + --- + +线程池ID:%s + +应用名称:%s + +应用实例:%s + + --- + +核心线程数:%d + +最大线程数:%d + +当前线程数:%d + +活跃线程数:%d + +同存最大线程数:%d + +线程池任务总量:%d + + --- + +队列类型:%s + +队列容量:%d + +队列元素个数:%d + +队列剩余个数:%d + + --- + + ${timout-content} + +拒绝策略:%s + +拒绝策略执行次数:%d + +OWNER:@%s + +提示:%d 分钟内此线程池不会重复告警(可配置) + + --- + +**播报时间:%s** diff --git a/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/ding-config.txt b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/ding-config.txt new file mode 100644 index 0000000000..dd4a902ff1 --- /dev/null +++ b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/ding-config.txt @@ -0,0 +1,41 @@ +**[通知] %s - 动态线程池参数变更** + + --- + +线程池ID:%s + +应用名称:%s + +应用实例:%s + + --- + +核心线程数:%s + +最大线程数:%s + +核心线程超时:%s + +线程存活时间:%s + +执行超时时间:%s + + --- + +队列类型:%s + +队列容量:%s + +AGO 拒绝策略:%s + +NOW 拒绝策略:%s + + --- + +提示:动态线程池配置变更实时通知(无限制) + +OWNER:@%s + + --- + +**播报时间:%s** \ No newline at end of file diff --git a/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json new file mode 100644 index 0000000000..93ceef7aba --- /dev/null +++ b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json @@ -0,0 +1,3 @@ +{ + "name": "123" +} \ No newline at end of file diff --git a/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/wechat-alarm.txt b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/wechat-alarm.txt new file mode 100644 index 0000000000..56f7ace12c --- /dev/null +++ b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/wechat-alarm.txt @@ -0,0 +1,21 @@ +### [警报] %s - 动态线程池运行告警(%s) + +> 线程池ID:%s +> 应用名称:%s +> 应用实例:%s +> 核心线程数:%s +> 最大线程数:%s +> 当前线程数:%s +> 活跃线程数:%s +> 同存最大线程数:%s +> 线程池任务总量:%s +> 队列类型:%s +> 队列容量:%s +> 队列元素个数:%s +> 队列剩余个数:%s +> 拒绝策略:%s +> 拒绝策略执行次数:%s ${timout-content} +> OWNER:<@%s> +> 提示:%d 分钟内此线程池不会重复告警(可配置) + +**播报时间:%s** \ No newline at end of file diff --git a/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/wechat-config.txt b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/wechat-config.txt new file mode 100644 index 0000000000..f583eb45bc --- /dev/null +++ b/hippo4j-message/src/main/resources/message/robot/dynamic-thread-pool/wechat-config.txt @@ -0,0 +1,17 @@ +### [通知] %s - 动态线程池参数变更 +> 线程池ID:%s +> 应用名称:%s +> 应用实例:%s +> 核心线程数:%s +> 最大线程数:%s +> 核心线程超时:%s +> 线程存活时间:%s +> 执行超时时间:%s +> 队列类型:%s +> 队列容量:%s +> AGO 拒绝策略:%s +> NOW 拒绝策略:%s +> OWNER:<@%s> +> 提示:动态线程池配置变更实时通知(无限制) + +**播报时间:%s** \ No newline at end of file