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

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

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

@ -83,6 +83,31 @@ public class BootstrapCoreProperties implements BootstrapPropertiesInterface {
*/ */
private List<NotifyPlatformProperties> notifyPlatforms; 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. * Executors.
*/ */

@ -1,12 +1,9 @@
package cn.hippo4j.core.starter.config; package cn.hippo4j.core.starter.config;
import cn.hippo4j.common.notify.ThreadPoolNotifyAlarm; import cn.hippo4j.common.notify.ThreadPoolNotifyAlarm;
import com.google.common.collect.Maps;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.util.Map;
/** /**
* Executor properties. * Executor properties.
* *
@ -72,9 +69,4 @@ public class ExecutorProperties {
*/ */
private ThreadPoolNotifyAlarm notify; 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 { public class NotifyPlatformProperties {
/** /**
* platform * Platform.
*/ */
private String platform; private String platform;
/** /**
* secretKey * Secret key.
*/ */
private String secretKey; private String secretKey;
/**
* Default configuration
*/
private String receives;
} }

@ -1,7 +1,7 @@
package cn.hippo4j.core.starter.notify; package cn.hippo4j.core.starter.notify;
import cn.hippo4j.common.notify.AlarmControlHandler;
import cn.hippo4j.common.api.NotifyConfigBuilder; import cn.hippo4j.common.api.NotifyConfigBuilder;
import cn.hippo4j.common.notify.AlarmControlHandler;
import cn.hippo4j.common.notify.NotifyConfigDTO; import cn.hippo4j.common.notify.NotifyConfigDTO;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties; import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import cn.hippo4j.core.starter.config.ExecutorProperties; import cn.hippo4j.core.starter.config.ExecutorProperties;
@ -13,6 +13,7 @@ import lombok.AllArgsConstructor;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** /**
* Core notify config builder. * Core notify config builder.
@ -60,16 +61,13 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder {
notifyConfig.setThreadPoolId(threadPoolId); notifyConfig.setThreadPoolId(threadPoolId);
notifyConfig.setType("ALARM"); notifyConfig.setType("ALARM");
notifyConfig.setSecretKey(platformProperties.getSecretKey()); notifyConfig.setSecretKey(platformProperties.getSecretKey());
notifyConfig.setInterval(executor.getNotify().getInterval()); int interval = Optional.ofNullable(executor.getNotify())
Map<String, String> receives = executor.receives(); .map(each -> each.getInterval())
String receive = receives.get(platformProperties.getPlatform()); .orElseGet(() -> bootstrapCoreProperties.getInterval() != null ? bootstrapCoreProperties.getInterval() : 5);
if (StrUtil.isBlank(receive)) { notifyConfig.setInterval(interval);
receive = platformProperties.getReceives(); notifyConfig.setReceives(buildReceive(executor, platformProperties));
}
notifyConfig.setReceives(receive);
alarmNotifyConfigs.add(notifyConfig); alarmNotifyConfigs.add(notifyConfig);
} }
resultMap.put(alarmBuildKey, alarmNotifyConfigs); resultMap.put(alarmBuildKey, alarmNotifyConfigs);
String changeBuildKey = threadPoolId + "+CONFIG"; String changeBuildKey = threadPoolId + "+CONFIG";
@ -81,16 +79,9 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder {
notifyConfig.setThreadPoolId(threadPoolId); notifyConfig.setThreadPoolId(threadPoolId);
notifyConfig.setType("CONFIG"); notifyConfig.setType("CONFIG");
notifyConfig.setSecretKey(platformProperties.getSecretKey()); notifyConfig.setSecretKey(platformProperties.getSecretKey());
notifyConfig.setReceives(buildReceive(executor, platformProperties));
Map<String, String> receives = executor.receives();
String receive = receives.get(platformProperties.getPlatform());
if (StrUtil.isBlank(receive)) {
receive = platformProperties.getReceives();
}
notifyConfig.setReceives(receive);
changeNotifyConfigs.add(notifyConfig); changeNotifyConfigs.add(notifyConfig);
} }
resultMap.put(changeBuildKey, changeNotifyConfigs); resultMap.put(changeBuildKey, changeNotifyConfigs);
resultMap.forEach( resultMap.forEach(
@ -102,4 +93,26 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder {
return resultMap; 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())) { if (Objects.isNull(dynamicThreadPoolWrap.getExecutor())) {
dynamicThreadPoolWrap.setExecutor(CommonDynamicThreadPool.getInstance(threadPoolId)); dynamicThreadPoolWrap.setExecutor(CommonDynamicThreadPool.getInstance(threadPoolId));
} }
dynamicThreadPoolWrap.setInitFlag(Boolean.TRUE); 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) { if (dynamicThreadPoolWrap.getExecutor() instanceof AbstractDynamicExecutorSupport) {
ThreadPoolNotifyAlarm threadPoolNotifyAlarm = new ThreadPoolNotifyAlarm( // 设置动态线程池增强参数
notify.getIsAlarm(), ThreadPoolNotifyAlarm notify = executorProperties.getNotify();
notify.getCapacityAlarm(), boolean isAlarm = Optional.ofNullable(notify)
notify.getActiveAlarm() .map(each -> each.getIsAlarm())
); .orElseGet(() -> bootstrapCoreProperties.getIsAlarm() != null ? bootstrapCoreProperties.getIsAlarm() : true);
int activeAlarm = Optional.ofNullable(notify)
threadPoolNotifyAlarm.setInterval(notify.getInterval()); .map(each -> each.getActiveAlarm())
threadPoolNotifyAlarm.setReceives(notify.getReceives()); .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); GlobalNotifyAlarmManage.put(threadPoolId, threadPoolNotifyAlarm);
TaskDecorator taskDecorator = ((DynamicThreadPoolExecutor) dynamicThreadPoolWrap.getExecutor()).getTaskDecorator(); TaskDecorator taskDecorator = ((DynamicThreadPoolExecutor) dynamicThreadPoolWrap.getExecutor()).getTaskDecorator();

Loading…
Cancel
Save