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

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

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

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

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

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

@ -23,17 +23,17 @@ public interface SendMessageHandler {
/**
* Send alarm message.
*
* @param alarmConfigs
* @param notifyConfigs
* @param threadPoolExecutor
*/
void sendAlarmMessage(List<AlarmConfig> alarmConfigs, CustomThreadPoolExecutor threadPoolExecutor);
void sendAlarmMessage(List<NotifyConfig> notifyConfigs, CustomThreadPoolExecutor threadPoolExecutor);
/**
* Send change message.
*
* @param alarmConfigs
* @param notifyConfigs
* @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;
import com.github.dynamic.threadpool.starter.alarm.AlarmConfig;
import com.github.dynamic.threadpool.starter.alarm.NotifyConfig;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@ -43,8 +43,8 @@ public class BootstrapProperties {
private boolean banner = true;
/**
* alarms
* notifys
*/
private List<AlarmConfig> alarms;
private List<NotifyConfig> notifys;
}

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

Loading…
Cancel
Save