优化 hippo4j-core 报警通知配置 (#158)

pull/157/merge
chen.ma 2 years ago
parent 29f0d1c4a8
commit c7ac968277

@ -41,9 +41,16 @@ public class ThreadPoolNotifyAlarm {
*/
private Integer interval;
/**
* receive
*/
private String receive;
/**
* receives
* ps
*/
@Deprecated
private Map<String, String> receives;
}

@ -83,6 +83,31 @@ public class BootstrapCoreProperties implements BootstrapPropertiesInterface {
*/
private List<NotifyPlatformProperties> notifyPlatforms;
/**
* Is alarm.
*/
private Boolean isAlarm;
/**
* Active alarm.
*/
private Integer activeAlarm;
/**
* Capacity alarm.
*/
private Integer capacityAlarm;
/**
* Interval.
*/
private Integer interval;
/**
* Receive.
*/
private String receive;
/**
* Executors.
*/

@ -1,12 +1,9 @@
package cn.hippo4j.core.starter.config;
import cn.hippo4j.common.notify.ThreadPoolNotifyAlarm;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Map;
/**
* Executor properties.
*
@ -72,9 +69,4 @@ public class ExecutorProperties {
*/
private ThreadPoolNotifyAlarm notify;
public Map<String, String> receives() {
return this.notify.getReceives() == null ? Maps.newHashMap() : this.notify.getReceives();
}
}

@ -12,18 +12,13 @@ import lombok.Data;
public class NotifyPlatformProperties {
/**
* platform
* Platform.
*/
private String platform;
/**
* secretKey
* Secret key.
*/
private String secretKey;
/**
* Default configuration
*/
private String receives;
}

@ -1,7 +1,7 @@
package cn.hippo4j.core.starter.notify;
import cn.hippo4j.common.notify.AlarmControlHandler;
import cn.hippo4j.common.api.NotifyConfigBuilder;
import cn.hippo4j.common.notify.AlarmControlHandler;
import cn.hippo4j.common.notify.NotifyConfigDTO;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import cn.hippo4j.core.starter.config.ExecutorProperties;
@ -13,6 +13,7 @@ import lombok.AllArgsConstructor;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Core notify config builder.
@ -60,16 +61,13 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder {
notifyConfig.setThreadPoolId(threadPoolId);
notifyConfig.setType("ALARM");
notifyConfig.setSecretKey(platformProperties.getSecretKey());
notifyConfig.setInterval(executor.getNotify().getInterval());
Map<String, String> receives = executor.receives();
String receive = receives.get(platformProperties.getPlatform());
if (StrUtil.isBlank(receive)) {
receive = platformProperties.getReceives();
}
notifyConfig.setReceives(receive);
int interval = Optional.ofNullable(executor.getNotify())
.map(each -> each.getInterval())
.orElseGet(() -> bootstrapCoreProperties.getInterval() != null ? bootstrapCoreProperties.getInterval() : 5);
notifyConfig.setInterval(interval);
notifyConfig.setReceives(buildReceive(executor, platformProperties));
alarmNotifyConfigs.add(notifyConfig);
}
resultMap.put(alarmBuildKey, alarmNotifyConfigs);
String changeBuildKey = threadPoolId + "+CONFIG";
@ -81,16 +79,9 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder {
notifyConfig.setThreadPoolId(threadPoolId);
notifyConfig.setType("CONFIG");
notifyConfig.setSecretKey(platformProperties.getSecretKey());
Map<String, String> receives = executor.receives();
String receive = receives.get(platformProperties.getPlatform());
if (StrUtil.isBlank(receive)) {
receive = platformProperties.getReceives();
}
notifyConfig.setReceives(receive);
notifyConfig.setReceives(buildReceive(executor, platformProperties));
changeNotifyConfigs.add(notifyConfig);
}
resultMap.put(changeBuildKey, changeNotifyConfigs);
resultMap.forEach(
@ -102,4 +93,26 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder {
return resultMap;
}
private String buildReceive(ExecutorProperties executor, NotifyPlatformProperties platformProperties) {
String receive;
if (executor.getNotify() != null) {
receive = executor.getNotify().getReceive();
if (StrUtil.isBlank(receive)) {
receive = bootstrapCoreProperties.getReceive();
if (StrUtil.isBlank(receive)) {
Map<String, String> receives = executor.getNotify().getReceives();
receive = receives.get(platformProperties.getPlatform());
}
}
} else {
receive = bootstrapCoreProperties.getReceive();
if (StrUtil.isBlank(receive)) {
Map<String, String> receives = executor.getNotify().getReceives();
receive = receives.get(platformProperties.getPlatform());
}
}
return receive;
}
}

@ -118,28 +118,31 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor {
if (Objects.isNull(dynamicThreadPoolWrap.getExecutor())) {
dynamicThreadPoolWrap.setExecutor(CommonDynamicThreadPool.getInstance(threadPoolId));
}
dynamicThreadPoolWrap.setInitFlag(Boolean.TRUE);
}
}
// 设置动态线程池增强参数
ThreadPoolNotifyAlarm notify = Optional.ofNullable(executorProperties)
.map(each -> each.getNotify())
.orElseGet(() -> {
ThreadPoolNotifyAlarm threadPoolNotifyAlarm = new ThreadPoolNotifyAlarm(true, 80, 80);
threadPoolNotifyAlarm.setInterval(2);
return threadPoolNotifyAlarm;
});
if (dynamicThreadPoolWrap.getExecutor() instanceof AbstractDynamicExecutorSupport) {
ThreadPoolNotifyAlarm threadPoolNotifyAlarm = new ThreadPoolNotifyAlarm(
notify.getIsAlarm(),
notify.getCapacityAlarm(),
notify.getActiveAlarm()
);
threadPoolNotifyAlarm.setInterval(notify.getInterval());
threadPoolNotifyAlarm.setReceives(notify.getReceives());
// 设置动态线程池增强参数
ThreadPoolNotifyAlarm notify = executorProperties.getNotify();
boolean isAlarm = Optional.ofNullable(notify)
.map(each -> each.getIsAlarm())
.orElseGet(() -> bootstrapCoreProperties.getIsAlarm() != null ? bootstrapCoreProperties.getIsAlarm() : true);
int activeAlarm = Optional.ofNullable(notify)
.map(each -> each.getActiveAlarm())
.orElseGet(() -> bootstrapCoreProperties.getActiveAlarm() != null ? bootstrapCoreProperties.getActiveAlarm() : 80);
int capacityAlarm = Optional.ofNullable(notify)
.map(each -> each.getActiveAlarm())
.orElseGet(() -> bootstrapCoreProperties.getCapacityAlarm() != null ? bootstrapCoreProperties.getCapacityAlarm() : 80);
int interval = Optional.ofNullable(notify)
.map(each -> each.getInterval())
.orElseGet(() -> bootstrapCoreProperties.getInterval() != null ? bootstrapCoreProperties.getInterval() : 5);
String receive = Optional.ofNullable(notify)
.map(each -> each.getReceive())
.orElseGet(() -> bootstrapCoreProperties.getReceive() != null ? bootstrapCoreProperties.getReceive() : null);
ThreadPoolNotifyAlarm threadPoolNotifyAlarm = new ThreadPoolNotifyAlarm(isAlarm, activeAlarm, capacityAlarm);
threadPoolNotifyAlarm.setInterval(interval);
threadPoolNotifyAlarm.setReceive(receive);
GlobalNotifyAlarmManage.put(threadPoolId, threadPoolNotifyAlarm);
TaskDecorator taskDecorator = ((DynamicThreadPoolExecutor) dynamicThreadPoolWrap.getExecutor()).getTaskDecorator();

Loading…
Cancel
Save