From 05557ddbf9b0d2d5c12c7a70c3831ba40d30ecdf Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Thu, 8 Jul 2021 23:54:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E7=BA=BF=E7=A8=8B=E6=B1=A0=E8=87=AA=E5=8A=A8=E8=A3=85?= =?UTF-8?q?=E9=85=8D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DynamicThreadPoolAutoConfiguration.java | 15 ++++++++++-- .../config/DynamicThreadPoolProperties.java | 5 ---- .../starter/config/OkHttpClientConfig.java | 2 -- .../controller/PoolRunStateController.java | 6 +---- .../DynamicThreadPoolMarkerConfiguration.java | 23 +++++++++++++++++++ .../enable/EnableDynamicThreadPool.java | 19 +++++++++++++++ .../handler/ThreadPoolRunStateHandler.java | 4 +--- .../starter/wrap/DynamicThreadPoolWrap.java | 2 +- ...itional-spring-configuration-metadata.json | 6 ----- .../main/resources/META-INF/spring.factories | 3 +-- .../example/ExampleApplication.java | 4 +++- 11 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/enable/DynamicThreadPoolMarkerConfiguration.java create mode 100644 dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/enable/EnableDynamicThreadPool.java diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/DynamicThreadPoolAutoConfiguration.java b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/DynamicThreadPoolAutoConfiguration.java index 4207c516..6c298354 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -1,13 +1,17 @@ package io.dynamic.threadpool.starter.config; +import io.dynamic.threadpool.common.config.CommonConfiguration; import io.dynamic.threadpool.starter.adapter.ThreadPoolConfigAdapter; +import io.dynamic.threadpool.starter.controller.PoolRunStateController; import io.dynamic.threadpool.starter.core.ConfigService; import io.dynamic.threadpool.starter.core.ThreadPoolConfigService; +import io.dynamic.threadpool.starter.enable.DynamicThreadPoolMarkerConfiguration; import io.dynamic.threadpool.starter.listener.ThreadPoolRunListener; import io.dynamic.threadpool.starter.operation.ThreadPoolOperation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -22,7 +26,8 @@ import org.springframework.context.annotation.Configuration; @Configuration @AllArgsConstructor @EnableConfigurationProperties(DynamicThreadPoolProperties.class) -@ConditionalOnProperty(prefix = DynamicThreadPoolProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnBean(DynamicThreadPoolMarkerConfiguration.Marker.class) +@ImportAutoConfiguration({OkHttpClientConfig.class, CommonConfiguration.class}) public class DynamicThreadPoolAutoConfiguration { private final DynamicThreadPoolProperties properties; @@ -46,4 +51,10 @@ public class DynamicThreadPoolAutoConfiguration { public ThreadPoolOperation threadPoolOperation() { return new ThreadPoolOperation(properties); } + + @Bean + public PoolRunStateController poolRunStateController() { + return new PoolRunStateController(); + } + } diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/DynamicThreadPoolProperties.java b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/DynamicThreadPoolProperties.java index d43f48d3..53d66f46 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/DynamicThreadPoolProperties.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/DynamicThreadPoolProperties.java @@ -34,11 +34,6 @@ public class DynamicThreadPoolProperties { */ private String itemId; - /** - * 是否开启动态线程池 - */ - private boolean enabled; - /** * 是否开启 banner */ diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/OkHttpClientConfig.java b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/OkHttpClientConfig.java index 722d2e90..7c4eb5b1 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/OkHttpClientConfig.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/config/OkHttpClientConfig.java @@ -5,7 +5,6 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import okhttp3.OkHttpClient; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; @@ -20,7 +19,6 @@ import java.util.concurrent.TimeUnit; * @date 2021/6/10 13:28 */ @Slf4j -@Configuration public class OkHttpClientConfig { /** diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/controller/PoolRunStateController.java b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/controller/PoolRunStateController.java index 60ccfa68..667b8d9a 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/controller/PoolRunStateController.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/controller/PoolRunStateController.java @@ -4,7 +4,6 @@ import io.dynamic.threadpool.common.model.PoolRunStateInfo; import io.dynamic.threadpool.common.web.base.Result; import io.dynamic.threadpool.common.web.base.Results; import io.dynamic.threadpool.starter.handler.ThreadPoolRunStateHandler; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @@ -18,12 +17,9 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class PoolRunStateController { - @Autowired - private ThreadPoolRunStateHandler threadPoolRunStateHandler; - @GetMapping("/run/state/{tpId}") public Result getPoolRunState(@PathVariable("tpId") String tpId) { - PoolRunStateInfo poolRunState = threadPoolRunStateHandler.getPoolRunState(tpId); + PoolRunStateInfo poolRunState = ThreadPoolRunStateHandler.getPoolRunState(tpId); return Results.success(poolRunState); } } diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/enable/DynamicThreadPoolMarkerConfiguration.java b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/enable/DynamicThreadPoolMarkerConfiguration.java new file mode 100644 index 00000000..371f2b4d --- /dev/null +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/enable/DynamicThreadPoolMarkerConfiguration.java @@ -0,0 +1,23 @@ +package io.dynamic.threadpool.starter.enable; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 动态线程池标记配置类 + * + * @author chen.ma + * @date 2021/7/8 23:30 + */ +@Configuration(proxyBeanMethods = false) +public class DynamicThreadPoolMarkerConfiguration { + + @Bean + public Marker dynamicThreadPoolMarkerBean() { + return new Marker(); + } + + public class Marker { + + } +} diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/enable/EnableDynamicThreadPool.java b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/enable/EnableDynamicThreadPool.java new file mode 100644 index 00000000..7659b691 --- /dev/null +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/enable/EnableDynamicThreadPool.java @@ -0,0 +1,19 @@ +package io.dynamic.threadpool.starter.enable; + +import org.springframework.context.annotation.Import; + +import java.lang.annotation.*; + +/** + * Annotation to activate Dynamic ThreadPool related configuration. + * + * @author chen.ma + * @date 2021/7/8 23:28 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Import(DynamicThreadPoolMarkerConfiguration.class) +public @interface EnableDynamicThreadPool { + +} diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/handler/ThreadPoolRunStateHandler.java b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/handler/ThreadPoolRunStateHandler.java index 6148ae16..d77fb11d 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/handler/ThreadPoolRunStateHandler.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/handler/ThreadPoolRunStateHandler.java @@ -4,7 +4,6 @@ import io.dynamic.threadpool.common.model.PoolRunStateInfo; import io.dynamic.threadpool.starter.core.GlobalThreadPoolManage; import io.dynamic.threadpool.starter.wrap.CustomThreadPoolExecutor; import io.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; -import org.springframework.stereotype.Component; import java.net.InetAddress; import java.net.UnknownHostException; @@ -17,7 +16,6 @@ import java.util.concurrent.ThreadPoolExecutor; * @author chen.ma * @date 2021/7/7 19:37 */ -@Component public class ThreadPoolRunStateHandler { private static InetAddress addr; @@ -31,7 +29,7 @@ public class ThreadPoolRunStateHandler { } } - public PoolRunStateInfo getPoolRunState(String tpId) { + public static PoolRunStateInfo getPoolRunState(String tpId) { DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(tpId); ThreadPoolExecutor pool = executorService.getPool(); diff --git a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrap.java b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrap.java index 81fdb8ef..1955d899 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrap.java +++ b/dynamic-threadpool-spring-boot-starter/src/main/java/io/dynamic/threadpool/starter/wrap/DynamicThreadPoolWrap.java @@ -30,7 +30,7 @@ public class DynamicThreadPoolWrap { * @param threadPoolId */ public DynamicThreadPoolWrap(String threadPoolId) { - this(threadPoolId, null); + this(threadPoolId, CommonThreadPool.getInstance(threadPoolId)); } /** diff --git a/dynamic-threadpool-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/dynamic-threadpool-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 81afb960..53fd8076 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/dynamic-threadpool-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -6,12 +6,6 @@ "defaultValue": "localhost:6691", "description": "dynamic thread-pool server address." }, - { - "name": "spring.dynamic.thread-pool.enabled", - "type": "java.lang.Boolean", - "defaultValue": false, - "description": "dynamic thread-pool enabled." - }, { "name": "spring.dynamic.thread-pool.namespace", "type": "java.lang.String", diff --git a/dynamic-threadpool-spring-boot-starter/src/main/resources/META-INF/spring.factories b/dynamic-threadpool-spring-boot-starter/src/main/resources/META-INF/spring.factories index fa29f7ef..e2fb8591 100644 --- a/dynamic-threadpool-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/dynamic-threadpool-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1,3 +1,2 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=io.dynamic.threadpool.common.config.CommonConfiguration, \ - io.dynamic.threadpool.starter.config.OkHttpClientConfig,\ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ io.dynamic.threadpool.starter.config.DynamicThreadPoolAutoConfiguration \ No newline at end of file diff --git a/example/src/main/java/io/dynamic/threadpool/example/ExampleApplication.java b/example/src/main/java/io/dynamic/threadpool/example/ExampleApplication.java index 347e2d3d..a489b3c4 100644 --- a/example/src/main/java/io/dynamic/threadpool/example/ExampleApplication.java +++ b/example/src/main/java/io/dynamic/threadpool/example/ExampleApplication.java @@ -1,9 +1,11 @@ package io.dynamic.threadpool.example; +import io.dynamic.threadpool.starter.enable.EnableDynamicThreadPool; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -@SpringBootApplication(scanBasePackages = "io.dynamic.threadpool") +@SpringBootApplication +@EnableDynamicThreadPool public class ExampleApplication { public static void main(String[] args) {