From bfd5b553345453c4a28d4ddc5dc86542e38f802c Mon Sep 17 00:00:00 2001 From: wulang Date: Wed, 21 Dec 2022 11:30:50 +0800 Subject: [PATCH] fix:default fill config --- .../config/BootstrapConfigProperties.java | 86 ------------------- .../DynamicThreadPoolPostProcessor.java | 22 ++--- 2 files changed, 11 insertions(+), 97 deletions(-) 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 e71c9d4c..047bab28 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,8 +17,6 @@ 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; @@ -27,8 +25,6 @@ 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. @@ -40,16 +36,6 @@ 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. */ @@ -154,76 +140,4 @@ 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(); - } - } - } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java index 44264686..1000304b 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java @@ -107,6 +107,7 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { .findFirst() .orElseThrow(() -> new RuntimeException("The thread pool id does not exist in the configuration.")); try { + executorProperties = buildActualExecutorProperties(executorProperties); threadPoolParamReplace(executor, executorProperties); } catch (Exception ex) { log.error("Failed to initialize thread pool configuration.", ex); @@ -121,7 +122,7 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { threadPoolId, executorProperties == null ? buildDefaultExecutorProperties(threadPoolId, executor) - : buildActualExecutorProperties(executorProperties)); + : executorProperties); return executor; } @@ -188,27 +189,26 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { * @return executor properties */ private ExecutorProperties buildExecutorProperties(ExecutorProperties executorProperties) { - ExecutorProperties newExecutorProperties = ExecutorProperties.builder() + return ExecutorProperties.builder() .corePoolSize(Optional.ofNullable(executorProperties.getCorePoolSize()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getCorePoolSize()).get())) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getCorePoolSize).get())) .maximumPoolSize(Optional.ofNullable(executorProperties.getMaximumPoolSize()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getMaximumPoolSize()).get())) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getMaximumPoolSize).get())) .allowCoreThreadTimeOut(Optional.ofNullable(executorProperties.getAllowCoreThreadTimeOut()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getAllowCoreThreadTimeOut()).get())) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getAllowCoreThreadTimeOut).get())) .keepAliveTime(Optional.ofNullable(executorProperties.getKeepAliveTime()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getKeepAliveTime()).get())) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getKeepAliveTime).get())) .blockingQueue(Optional.ofNullable(executorProperties.getBlockingQueue()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getBlockingQueue()).get())) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getBlockingQueue).get())) .executeTimeOut(Optional.ofNullable(executorProperties.getExecuteTimeOut()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getExecuteTimeOut()).orElse(0L))) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getExecuteTimeOut).orElse(0L))) .queueCapacity(Optional.ofNullable(executorProperties.getQueueCapacity()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getQueueCapacity()).get())) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getQueueCapacity).get())) .rejectedHandler(Optional.ofNullable(executorProperties.getRejectedHandler()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getRejectedHandler()).get())) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getRejectedHandler).get())) .threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ? executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix()) .threadPoolId(executorProperties.getThreadPoolId()) .build(); - return newExecutorProperties; } /**