通过配置文件设置是否启用动态线程池功能. (#43)

pull/84/head
chen.ma 3 years ago
parent 2177b9cb27
commit 68fe059961

@ -19,3 +19,4 @@ spring:
item-id: dynamic-threadpool-example item-id: dynamic-threadpool-example
username: admin username: admin
password: 123456 password: 123456
enable: true

@ -44,6 +44,11 @@ public class BootstrapProperties {
*/ */
private String itemId; private String itemId;
/**
* Whether to enable dynamic thread pool
*/
private Boolean enable = true;
/** /**
* Print dynamic thread pool banner * Print dynamic thread pool banner
*/ */

@ -23,6 +23,7 @@ import cn.hutool.core.util.IdUtil;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -40,6 +41,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
@AllArgsConstructor @AllArgsConstructor
@ConditionalOnBean(MarkerConfiguration.Marker.class) @ConditionalOnBean(MarkerConfiguration.Marker.class)
@EnableConfigurationProperties(BootstrapProperties.class) @EnableConfigurationProperties(BootstrapProperties.class)
@ConditionalOnProperty(prefix = BootstrapProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true")
@ImportAutoConfiguration({HttpClientConfig.class, DiscoveryConfig.class, MessageAlarmConfig.class, UtilAutoConfiguration.class}) @ImportAutoConfiguration({HttpClientConfig.class, DiscoveryConfig.class, MessageAlarmConfig.class, UtilAutoConfiguration.class})
public class DynamicThreadPoolAutoConfiguration { public class DynamicThreadPoolAutoConfiguration {

@ -4,6 +4,7 @@ import cn.hippo4j.starter.config.BootstrapProperties;
import cn.hippo4j.starter.core.ConfigEmptyException; import cn.hippo4j.starter.core.ConfigEmptyException;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
@ -19,53 +20,56 @@ import org.springframework.core.env.ConfigurableEnvironment;
public class BeforeCheckConfiguration { public class BeforeCheckConfiguration {
@Bean @Bean
public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(BootstrapProperties properties, ConfigurableEnvironment environment) { public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Autowired(required = false) BootstrapProperties properties,
String username = properties.getUsername(); ConfigurableEnvironment environment) {
if (StrUtil.isBlank(username)) { if (properties != null) {
throw new ConfigEmptyException( String username = properties.getUsername();
"Web server failed to start. The dynamic thread pool username is empty.", if (StrUtil.isBlank(username)) {
"Please check whether the [spring.dynamic.thread-pool.username] configuration is empty or an empty string." throw new ConfigEmptyException(
); "Web server failed to start. The dynamic thread pool username is empty.",
} "Please check whether the [spring.dynamic.thread-pool.username] configuration is empty or an empty string."
);
}
String password = properties.getPassword(); String password = properties.getPassword();
if (StrUtil.isBlank(password)) { if (StrUtil.isBlank(password)) {
throw new ConfigEmptyException( throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool password is empty.", "Web server failed to start. The dynamic thread pool password is empty.",
"Please check whether the [spring.dynamic.thread-pool.password] configuration is empty or an empty string." "Please check whether the [spring.dynamic.thread-pool.password] configuration is empty or an empty string."
); );
} }
String namespace = properties.getNamespace(); String namespace = properties.getNamespace();
if (StrUtil.isBlank(namespace)) { if (StrUtil.isBlank(namespace)) {
throw new ConfigEmptyException( throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool namespace is empty.", "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." "Please check whether the [spring.dynamic.thread-pool.namespace] configuration is empty or an empty string."
); );
} }
String itemId = properties.getItemId(); String itemId = properties.getItemId();
if (StrUtil.isBlank(itemId)) { if (StrUtil.isBlank(itemId)) {
throw new ConfigEmptyException( throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool item id is empty.", "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." "Please check whether the [spring.dynamic.thread-pool.item-id] configuration is empty or an empty string."
); );
} }
String serverAddr = properties.getServerAddr(); String serverAddr = properties.getServerAddr();
if (StrUtil.isBlank(serverAddr)) { if (StrUtil.isBlank(serverAddr)) {
throw new ConfigEmptyException( throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool server addr is empty.", "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." "Please check whether the [spring.dynamic.thread-pool.server-addr] configuration is empty or an empty string."
); );
} }
String applicationName = environment.getProperty("spring.application.name"); String applicationName = environment.getProperty("spring.application.name");
if (StrUtil.isBlank(applicationName)) { if (StrUtil.isBlank(applicationName)) {
throw new ConfigEmptyException( throw new ConfigEmptyException(
"Web server failed to start. The dynamic thread pool application name is empty.", "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." "Please check whether the [spring.application.name] configuration is empty or an empty string."
); );
}
} }
return new BeforeCheckConfiguration.BeforeCheck(); return new BeforeCheckConfiguration.BeforeCheck();

Loading…
Cancel
Save