From 61952160712b1c50f3679f4c39923159e979590d Mon Sep 17 00:00:00 2001 From: wulang Date: Mon, 19 Dec 2022 19:53:22 +0800 Subject: [PATCH] fix:default fill config --- .../config/BootstrapConfigProperties.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java index 047bab28..e71c9d4c 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java @@ -17,6 +17,8 @@ package cn.hippo4j.config.springboot.starter.config; +import cn.hippo4j.common.toolkit.BeanUtil; +import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.config.springboot.starter.parser.ConfigFileTypeEnum; import cn.hippo4j.core.config.BootstrapPropertiesInterface; import lombok.Getter; @@ -25,6 +27,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; /** * Bootstrap core properties. @@ -36,6 +40,16 @@ public class BootstrapConfigProperties implements BootstrapPropertiesInterface { public static final String PREFIX = "spring.dynamic.thread-pool"; + /** + * Fill in default values only at initialization + */ + private final AtomicBoolean defaultFillBoolean; + + /** + * Copy once only + */ + private final AtomicInteger atomicInteger; + /** * Enable dynamic thread pool. */ @@ -140,4 +154,76 @@ public class BootstrapConfigProperties implements BootstrapPropertiesInterface { * Tripartite framework thread pool adaptation set. */ private List adapterExecutors; + + public BootstrapConfigProperties() { + this.defaultFillBoolean = new AtomicBoolean(false); + this.atomicInteger = new AtomicInteger(0); + } + + public void setDefaultExecutor(ExecutorProperties defaultExecutor) { + this.defaultExecutor = defaultExecutor; + defaultFill(); + } + + public void setExecutors(List executors) { + this.executors = executors; + defaultFill(); + } + + private void defaultFill() { + if (defaultFillBoolean.get() && atomicInteger.get() == 1) { + if (CollectionUtil.isNotEmpty(executors) && defaultExecutor != null) { + for (ExecutorProperties executor : executors) { + if (executor.getCorePoolSize() == null) { + executor.setCorePoolSize(defaultExecutor.getCorePoolSize()); + } + if (executor.getMaximumPoolSize() == null) { + executor.setMaximumPoolSize(defaultExecutor.getMaximumPoolSize()); + } + if (executor.getQueueCapacity() == null) { + executor.setQueueCapacity(defaultExecutor.getQueueCapacity()); + } + if (executor.getBlockingQueue() == null) { + executor.setBlockingQueue(defaultExecutor.getBlockingQueue()); + } + if (executor.getRejectedHandler() == null) { + executor.setRejectedHandler(defaultExecutor.getRejectedHandler()); + } + if (executor.getKeepAliveTime() == null) { + executor.setKeepAliveTime(defaultExecutor.getKeepAliveTime()); + } + if (executor.getExecuteTimeOut() == null) { + executor.setExecuteTimeOut(defaultExecutor.getExecuteTimeOut()); + } + if (executor.getAllowCoreThreadTimeOut() == null) { + executor.setAllowCoreThreadTimeOut(defaultExecutor.getAllowCoreThreadTimeOut()); + } + if (executor.getThreadNamePrefix() == null) { + executor.setThreadNamePrefix(defaultExecutor.getThreadNamePrefix()); + } + if (executor.getAlarm() == null) { + executor.setAlarm(defaultExecutor.getAlarm()); + } + if (executor.getActiveAlarm() == null) { + executor.setActiveAlarm(defaultExecutor.getActiveAlarm()); + } + if (executor.getCapacityAlarm() == null) { + executor.setCapacityAlarm(defaultExecutor.getCapacityAlarm()); + } + if (executor.getNotify() == null) { + DynamicThreadPoolNotifyProperties dynamicThreadPoolNotifyProperties = new DynamicThreadPoolNotifyProperties(); + BeanUtil.convert(defaultExecutor.getNotify(), dynamicThreadPoolNotifyProperties); + executor.setNotify(dynamicThreadPoolNotifyProperties); + } + if (executor.getNodes() == null) { + executor.setNodes(defaultExecutor.getNodes()); + } + } + } + } else { + defaultFillBoolean.compareAndSet(false, true); + atomicInteger.getAndIncrement(); + } + } + }