diff --git a/threadpool/core/pom.xml b/threadpool/core/pom.xml index 903c73deef7a3ad95f5e77e8a385102363903237..faf0f64cc93845acfcb1e5df0a8f6668eef15496 100644 --- a/threadpool/core/pom.xml +++ b/threadpool/core/pom.xml @@ -44,5 +44,10 @@ mockito-inline test + + cn.hippo4j + hippo4j-threadpool-dynamic-mode-config + ${project.version} + diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java b/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java index c6640d93ac64120e2977899395383edc602a727d..061e1b48608c106dda1e49330f764242daf787a2 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java @@ -17,16 +17,20 @@ package cn.hippo4j.core.enable; +import cn.hippo4j.common.toolkit.MapUtil; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.core.config.ConfigEmptyException; import cn.hippo4j.threadpool.dynamic.api.BootstrapPropertiesInterface; +import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; import lombok.AllArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.ConfigurableEnvironment; +import java.util.Map; import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; /** * Before check configuration. @@ -36,6 +40,7 @@ import java.util.Objects; public class BeforeCheckConfiguration { private final String bootstrapPropertiesClassName = "cn.hippo4j.springboot.starter.config.BootstrapProperties"; + private final String bootstrapConfigPropertiesClassName = "cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties"; @Bean public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Autowired(required = false) BootstrapPropertiesInterface properties, @@ -71,6 +76,59 @@ public class BeforeCheckConfiguration { return new BeforeCheckConfiguration.BeforeCheck(); } + @Bean + public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolConfigBeforeCheckBean(@Autowired(required = false) BootstrapConfigProperties properties, + ConfigurableEnvironment environment) { + boolean checkFlag = properties != null && Objects.equals(bootstrapConfigPropertiesClassName, properties.getClass().getName()) && properties.getEnable(); + if (checkFlag) { + String namespace = properties.getNamespace(); + if (StringUtil.isBlank(namespace)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool namespace is empty.", + "Please check whether the [spring.dynamic.thread-pool.namespace] configuration is empty or an empty string."); + } + String itemId = properties.getItemId(); + if (StringUtil.isBlank(itemId)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool item id is empty.", + "Please check whether the [spring.dynamic.thread-pool.item-id] configuration is empty or an empty string."); + } + String serverAddr = properties.getServerAddr(); + if (StringUtil.isBlank(serverAddr)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool server addr is empty.", + "Please check whether the [spring.dynamic.thread-pool.server-addr] configuration is empty or an empty string."); + } + String applicationName = environment.getProperty("spring.application.name"); + if (StringUtil.isBlank(applicationName)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool application name is empty.", + "Please check whether the [spring.application.name] configuration is empty or an empty string."); + } + AtomicBoolean flag = new AtomicBoolean(false); + Map nacosConfigMap = properties.getNacos(); + if (MapUtil.isNotEmpty(nacosConfigMap)) { + flag.set(true); + String dataId = nacosConfigMap.get("data-id"); + if (StringUtil.isBlank(dataId)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool config nacos data-id is empty.", + "Please check whether the [spring.dynamic.thread-pool.nacos.data-id] configuration is empty or an empty string."); + } + String group = nacosConfigMap.get("group"); + if (StringUtil.isBlank(group)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool config nacos group is empty.", + "Please check whether the [spring.dynamic.thread-pool.nacos.group] configuration is empty or an empty string."); + } + } + if (!flag.get()) { + // TODO throw ConfigEmptyException description field fill + } + } + return new BeforeCheckConfiguration.BeforeCheck(); + } + /** * Before check. */