fix:default fill config

pull/1041/head
wulang 3 years ago
parent 6195216071
commit bfd5b55334

@ -17,8 +17,6 @@
package cn.hippo4j.config.springboot.starter.config; 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.config.springboot.starter.parser.ConfigFileTypeEnum;
import cn.hippo4j.core.config.BootstrapPropertiesInterface; import cn.hippo4j.core.config.BootstrapPropertiesInterface;
import lombok.Getter; import lombok.Getter;
@ -27,8 +25,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Bootstrap core properties. * Bootstrap core properties.
@ -40,16 +36,6 @@ public class BootstrapConfigProperties implements BootstrapPropertiesInterface {
public static final String PREFIX = "spring.dynamic.thread-pool"; 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. * Enable dynamic thread pool.
*/ */
@ -154,76 +140,4 @@ public class BootstrapConfigProperties implements BootstrapPropertiesInterface {
* Tripartite framework thread pool adaptation set. * Tripartite framework thread pool adaptation set.
*/ */
private List<AdapterExecutorProperties> adapterExecutors; private List<AdapterExecutorProperties> 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<ExecutorProperties> 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();
}
}
} }

@ -107,6 +107,7 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor {
.findFirst() .findFirst()
.orElseThrow(() -> new RuntimeException("The thread pool id does not exist in the configuration.")); .orElseThrow(() -> new RuntimeException("The thread pool id does not exist in the configuration."));
try { try {
executorProperties = buildActualExecutorProperties(executorProperties);
threadPoolParamReplace(executor, executorProperties); threadPoolParamReplace(executor, executorProperties);
} catch (Exception ex) { } catch (Exception ex) {
log.error("Failed to initialize thread pool configuration.", ex); log.error("Failed to initialize thread pool configuration.", ex);
@ -121,7 +122,7 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor {
threadPoolId, threadPoolId,
executorProperties == null executorProperties == null
? buildDefaultExecutorProperties(threadPoolId, executor) ? buildDefaultExecutorProperties(threadPoolId, executor)
: buildActualExecutorProperties(executorProperties)); : executorProperties);
return executor; return executor;
} }
@ -188,27 +189,26 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor {
* @return executor properties * @return executor properties
*/ */
private ExecutorProperties buildExecutorProperties(ExecutorProperties executorProperties) { private ExecutorProperties buildExecutorProperties(ExecutorProperties executorProperties) {
ExecutorProperties newExecutorProperties = ExecutorProperties.builder() return ExecutorProperties.builder()
.corePoolSize(Optional.ofNullable(executorProperties.getCorePoolSize()) .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()) .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()) .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()) .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()) .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()) .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()) .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()) .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()) .threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ? executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix())
.threadPoolId(executorProperties.getThreadPoolId()) .threadPoolId(executorProperties.getThreadPoolId())
.build(); .build();
return newExecutorProperties;
} }
/** /**

Loading…
Cancel
Save