feature: 消息通知 (变更配置、报警信息) 支持 @ 多人, 修改变量命名.

pull/161/head
chen.ma 4 years ago
parent 83e49afb5e
commit 7a2e1aa877

@ -61,6 +61,11 @@
<groupId>com.aliyun</groupId> <groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId> <artifactId>alibaba-dingtalk-service-sdk</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -23,7 +23,7 @@ import java.util.Map;
public class BaseSendMessageService implements InitializingBean, SendMessageService { public class BaseSendMessageService implements InitializingBean, SendMessageService {
@NonNull @NonNull
private final List<AlarmConfig> alarmConfigs; private final List<NotifyConfig> notifyConfigs;
private final List<SendMessageHandler> sendMessageHandlers = new ArrayList(4); private final List<SendMessageHandler> sendMessageHandlers = new ArrayList(4);
@ -31,7 +31,7 @@ public class BaseSendMessageService implements InitializingBean, SendMessageServ
public void sendAlarmMessage(CustomThreadPoolExecutor threadPoolExecutor) { public void sendAlarmMessage(CustomThreadPoolExecutor threadPoolExecutor) {
for (SendMessageHandler messageHandler : sendMessageHandlers) { for (SendMessageHandler messageHandler : sendMessageHandlers) {
try { try {
messageHandler.sendAlarmMessage(alarmConfigs, threadPoolExecutor); messageHandler.sendAlarmMessage(notifyConfigs, threadPoolExecutor);
} catch (Exception ex) { } catch (Exception ex) {
log.warn("Failed to send thread pool alarm notification.", ex); log.warn("Failed to send thread pool alarm notification.", ex);
} }
@ -42,7 +42,7 @@ public class BaseSendMessageService implements InitializingBean, SendMessageServ
public void sendChangeMessage(PoolParameterInfo parameter) { public void sendChangeMessage(PoolParameterInfo parameter) {
for (SendMessageHandler messageHandler : sendMessageHandlers) { for (SendMessageHandler messageHandler : sendMessageHandlers) {
try { try {
messageHandler.sendChangeMessage(alarmConfigs, parameter); messageHandler.sendChangeMessage(notifyConfigs, parameter);
} catch (Exception ex) { } catch (Exception ex) {
log.warn("Failed to send thread pool change notification.", ex); log.warn("Failed to send thread pool change notification.", ex);
} }

@ -1,7 +1,7 @@
package com.github.dynamic.threadpool.starter.alarm; package com.github.dynamic.threadpool.starter.alarm;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.request.OapiRobotSendRequest;
@ -12,6 +12,7 @@ import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExec
import com.github.dynamic.threadpool.starter.toolkit.thread.QueueTypeEnum; import com.github.dynamic.threadpool.starter.toolkit.thread.QueueTypeEnum;
import com.github.dynamic.threadpool.starter.toolkit.thread.RejectedTypeEnum; import com.github.dynamic.threadpool.starter.toolkit.thread.RejectedTypeEnum;
import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap;
import com.google.common.base.Joiner;
import com.taobao.api.ApiException; import com.taobao.api.ApiException;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -45,22 +46,25 @@ public class DingSendMessageHandler implements SendMessageHandler {
} }
@Override @Override
public void sendAlarmMessage(List<AlarmConfig> alarmConfigs, CustomThreadPoolExecutor pool) { public void sendAlarmMessage(List<NotifyConfig> notifyConfigs, CustomThreadPoolExecutor pool) {
Optional<AlarmConfig> alarmConfigOptional = alarmConfigs.stream() Optional<NotifyConfig> notifyConfigOptional = notifyConfigs.stream()
.filter(each -> Objects.equals(each.getType(), getType())) .filter(each -> Objects.equals(each.getType(), getType()))
.findFirst(); .findFirst();
alarmConfigOptional.ifPresent(each -> dingAlarmSendMessage(each, pool)); notifyConfigOptional.ifPresent(each -> dingAlarmSendMessage(each, pool));
} }
@Override @Override
public void sendChangeMessage(List<AlarmConfig> alarmConfigs, PoolParameterInfo parameter) { public void sendChangeMessage(List<NotifyConfig> notifyConfigs, PoolParameterInfo parameter) {
Optional<AlarmConfig> changeConfigOptional = alarmConfigs.stream() Optional<NotifyConfig> changeConfigOptional = notifyConfigs.stream()
.filter(each -> Objects.equals(each.getType(), getType())) .filter(each -> Objects.equals(each.getType(), getType()))
.findFirst(); .findFirst();
changeConfigOptional.ifPresent(each -> dingChangeSendMessage(each, parameter)); changeConfigOptional.ifPresent(each -> dingChangeSendMessage(each, parameter));
} }
private void dingAlarmSendMessage(AlarmConfig alarmConfig, CustomThreadPoolExecutor pool) { private void dingAlarmSendMessage(NotifyConfig notifyConfig, CustomThreadPoolExecutor pool) {
List<String> receives = StrUtil.split(notifyConfig.getReceives(), ',');
String afterReceives = Joiner.on(", @").join(receives);
BlockingQueue<Runnable> queue = pool.getQueue(); BlockingQueue<Runnable> queue = pool.getQueue();
String text = String.format( String text = String.format(
"<font color='#FF0000'>[警报] </font>%s - 动态线程池运行告警 \n\n" + "<font color='#FF0000'>[警报] </font>%s - 动态线程池运行告警 \n\n" +
@ -118,16 +122,15 @@ public class DingSendMessageHandler implements SendMessageHandler {
// 拒绝策略次数 // 拒绝策略次数
pool.getRejectCount(), pool.getRejectCount(),
// 告警手机号 // 告警手机号
"15601166691", afterReceives,
// 当前时间 // 当前时间
DateUtil.now() DateUtil.now()
); );
execute(alarmConfig, "动态线程池告警", text, CollUtil.newArrayList("15601166691")); execute(notifyConfig, "动态线程池告警", text, receives);
} }
private void dingChangeSendMessage(AlarmConfig alarmConfig, PoolParameterInfo parameter) { private void dingChangeSendMessage(NotifyConfig notifyConfig, PoolParameterInfo parameter) {
String threadPoolId = parameter.getTpId(); String threadPoolId = parameter.getTpId();
DynamicThreadPoolWrap poolWrap = GlobalThreadPoolManage.getExecutorService(threadPoolId); DynamicThreadPoolWrap poolWrap = GlobalThreadPoolManage.getExecutorService(threadPoolId);
if (poolWrap == null) { if (poolWrap == null) {
@ -135,6 +138,9 @@ public class DingSendMessageHandler implements SendMessageHandler {
return; return;
} }
List<String> receives = StrUtil.split(notifyConfig.getReceives(), ',');
String afterReceives = Joiner.on(", @").join(receives);
CustomThreadPoolExecutor customPool = poolWrap.getPool(); CustomThreadPoolExecutor customPool = poolWrap.getPool();
/** /**
* hesitant e.g. * hesitant e.g.
@ -179,16 +185,16 @@ public class DingSendMessageHandler implements SendMessageHandler {
customPool.getRejectedExecutionHandler().getClass().getSimpleName(), customPool.getRejectedExecutionHandler().getClass().getSimpleName(),
RejectedTypeEnum.getRejectedNameByType(parameter.getRejectedType()), RejectedTypeEnum.getRejectedNameByType(parameter.getRejectedType()),
// 告警手机号 // 告警手机号
"15601166691", afterReceives,
// 当前时间 // 当前时间
DateUtil.now() DateUtil.now()
); );
execute(alarmConfig, "动态线程池通知", text, CollUtil.newArrayList("15601166691")); execute(notifyConfig, "动态线程池通知", text, receives);
} }
private void execute(AlarmConfig alarmConfig, String title, String text, List<String> mobiles) { private void execute(NotifyConfig notifyConfigs, String title, String text, List<String> mobiles) {
String serverUrl = alarmConfig.getUrl() + alarmConfig.getToken(); String serverUrl = notifyConfigs.getUrl() + notifyConfigs.getToken();
DingTalkClient dingTalkClient = new DefaultDingTalkClient(serverUrl); DingTalkClient dingTalkClient = new DefaultDingTalkClient(serverUrl);
OapiRobotSendRequest request = new OapiRobotSendRequest(); OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("markdown"); request.setMsgtype("markdown");

@ -9,7 +9,7 @@ import lombok.Data;
* @date 2021/8/15 16:09 * @date 2021/8/15 16:09
*/ */
@Data @Data
public class AlarmConfig { public class NotifyConfig {
/** /**
* type * type
@ -26,4 +26,9 @@ public class AlarmConfig {
*/ */
private String token; private String token;
/**
* receives
*/
private String receives;
} }

@ -23,17 +23,17 @@ public interface SendMessageHandler {
/** /**
* Send alarm message. * Send alarm message.
* *
* @param alarmConfigs * @param notifyConfigs
* @param threadPoolExecutor * @param threadPoolExecutor
*/ */
void sendAlarmMessage(List<AlarmConfig> alarmConfigs, CustomThreadPoolExecutor threadPoolExecutor); void sendAlarmMessage(List<NotifyConfig> notifyConfigs, CustomThreadPoolExecutor threadPoolExecutor);
/** /**
* Send change message. * Send change message.
* *
* @param alarmConfigs * @param notifyConfigs
* @param parameter * @param parameter
*/ */
void sendChangeMessage(List<AlarmConfig> alarmConfigs, PoolParameterInfo parameter); void sendChangeMessage(List<NotifyConfig> notifyConfigs, PoolParameterInfo parameter);
} }

@ -1,6 +1,6 @@
package com.github.dynamic.threadpool.starter.config; package com.github.dynamic.threadpool.starter.config;
import com.github.dynamic.threadpool.starter.alarm.AlarmConfig; import com.github.dynamic.threadpool.starter.alarm.NotifyConfig;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -43,8 +43,8 @@ public class BootstrapProperties {
private boolean banner = true; private boolean banner = true;
/** /**
* alarms * notifys
*/ */
private List<AlarmConfig> alarms; private List<NotifyConfig> notifys;
} }

@ -29,7 +29,7 @@ public class MessageAlarmConfig {
@Bean @Bean
@DependsOn("applicationContextHolder") @DependsOn("applicationContextHolder")
public SendMessageService sendMessageService() { public SendMessageService sendMessageService() {
return new BaseSendMessageService(properties.getAlarms()); return new BaseSendMessageService(properties.getNotifys());
} }
@Bean @Bean

Loading…
Cancel
Save