diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java index 101ce524..76b5ea85 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -14,8 +14,8 @@ import cn.hippo4j.starter.handler.BaseThreadDetailStateHandler; import cn.hippo4j.starter.handler.DynamicThreadPoolBannerHandler; import cn.hippo4j.starter.handler.ThreadPoolRunStateHandler; import cn.hippo4j.starter.handler.web.TomcatWebThreadPoolHandler; +import cn.hippo4j.starter.handler.web.WebThreadPoolHandlerChoose; import cn.hippo4j.starter.handler.web.WebThreadPoolRunStateHandler; -import cn.hippo4j.starter.handler.web.WebThreadPoolService; import cn.hippo4j.starter.monitor.ReportingEventExecutor; import cn.hippo4j.starter.monitor.collect.RunTimeInfoCollector; import cn.hippo4j.starter.monitor.send.HttpConnectSender; @@ -143,15 +143,21 @@ public class DynamicThreadPoolAutoConfiguration { } @Bean + @ConditionalOnBean(name = "tomcatServletWebServerFactory") public TomcatWebThreadPoolHandler tomcatWebThreadPoolHandler() { return new TomcatWebThreadPoolHandler(applicationContext); } @Bean - public WebThreadPoolController webThreadPoolController(WebThreadPoolService webThreadPoolService, + public WebThreadPoolHandlerChoose webThreadPoolServiceChoose() { + return new WebThreadPoolHandlerChoose(); + } + + @Bean + public WebThreadPoolController webThreadPoolController(WebThreadPoolHandlerChoose webThreadPoolServiceChoose, ThreadDetailState threadDetailState, WebThreadPoolRunStateHandler webThreadPoolRunStateHandler) { - return new WebThreadPoolController(webThreadPoolService, threadDetailState, webThreadPoolRunStateHandler); + return new WebThreadPoolController(webThreadPoolServiceChoose, threadDetailState, webThreadPoolRunStateHandler); } } diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/controller/WebThreadPoolController.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/controller/WebThreadPoolController.java index 8ad3b7b8..05ad4fb2 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/controller/WebThreadPoolController.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/controller/WebThreadPoolController.java @@ -5,6 +5,7 @@ import cn.hippo4j.common.model.PoolParameterInfo; import cn.hippo4j.common.model.PoolRunStateInfo; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Results; +import cn.hippo4j.starter.handler.web.WebThreadPoolHandlerChoose; import cn.hippo4j.starter.handler.web.WebThreadPoolRunStateHandler; import cn.hippo4j.starter.handler.web.WebThreadPoolService; import lombok.AllArgsConstructor; @@ -23,7 +24,7 @@ import java.util.concurrent.Executor; @AllArgsConstructor public class WebThreadPoolController { - private final WebThreadPoolService webThreadPoolService; + private final WebThreadPoolHandlerChoose webThreadPoolServiceChoose; private final ThreadDetailState threadDetailState; @@ -31,6 +32,7 @@ public class WebThreadPoolController { @GetMapping("/web/run/state") public Result getPoolRunState() { + WebThreadPoolService webThreadPoolService = webThreadPoolServiceChoose.choose(); Executor webThreadPool = webThreadPoolService.getWebThreadPool(); PoolRunStateInfo poolRunState = webThreadPoolRunStateHandler.getPoolRunState(null, webThreadPool); return Results.success(poolRunState); @@ -38,6 +40,7 @@ public class WebThreadPoolController { @PostMapping("/web/update/pool") public Result updateWebThreadPool(@RequestBody PoolParameterInfo poolParameterInfo) { + WebThreadPoolService webThreadPoolService = webThreadPoolServiceChoose.choose(); webThreadPoolService.updateWebThreadPool(poolParameterInfo); return Results.success(); } diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolHandlerChoose.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolHandlerChoose.java new file mode 100644 index 00000000..bcb8097c --- /dev/null +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolHandlerChoose.java @@ -0,0 +1,32 @@ +package cn.hippo4j.starter.handler.web; + +import cn.hippo4j.common.config.ApplicationContextHolder; +import cn.hippo4j.common.web.exception.ServiceException; +import lombok.extern.slf4j.Slf4j; + +/** + * Web thread pool handler choose. + * + * @author chen.ma + * @date 2022/1/20 20:15 + */ +@Slf4j +public class WebThreadPoolHandlerChoose { + + /** + * Choose the web thread pool service bean. + * + * @return + */ + public WebThreadPoolService choose() { + WebThreadPoolService webThreadPoolService; + try { + webThreadPoolService = ApplicationContextHolder.getBean(WebThreadPoolService.class); + } catch (Exception ex) { + throw new ServiceException("Web thread pool service bean not found.", ex); + } + + return webThreadPoolService; + } + +}