From e5a83a0cfb92e35f22e90ff72a09ca9c8c34b930 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Fri, 11 Mar 2022 22:36:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E8=B1=A1=20web=20=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E7=BB=84=E4=BB=B6.=20(#141)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hippo4j-common/pom.xml | 21 ++++++++ .../AbstractWebThreadPoolService.java | 12 ++++- .../executor}/JettyWebThreadPoolHandler.java | 22 ++++++++- .../executor}/TomcatWebThreadPoolHandler.java | 23 ++++++++- .../UndertowWebThreadPoolHandler.java | 24 ++++++++- .../executor}/WebThreadPoolHandlerChoose.java | 2 +- .../web/executor}/WebThreadPoolService.java | 10 +++- .../config/WebThreadPoolConfiguration.java | 49 +++++++++++++++++++ .../hippo4j-spring-boot-starter/pom.xml | 35 +++++++------ .../DynamicThreadPoolAutoConfiguration.java | 35 ++----------- .../controller/WebThreadPoolController.java | 4 +- 11 files changed, 183 insertions(+), 54 deletions(-) rename {hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web => hippo4j-common/src/main/java/cn/hippo4j/common/web/executor}/AbstractWebThreadPoolService.java (80%) rename {hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web => hippo4j-common/src/main/java/cn/hippo4j/common/web/executor}/JettyWebThreadPoolHandler.java (69%) rename {hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web => hippo4j-common/src/main/java/cn/hippo4j/common/web/executor}/TomcatWebThreadPoolHandler.java (75%) rename {hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web => hippo4j-common/src/main/java/cn/hippo4j/common/web/executor}/UndertowWebThreadPoolHandler.java (74%) rename {hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web => hippo4j-common/src/main/java/cn/hippo4j/common/web/executor}/WebThreadPoolHandlerChoose.java (94%) rename {hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web => hippo4j-common/src/main/java/cn/hippo4j/common/web/executor}/WebThreadPoolService.java (71%) create mode 100644 hippo4j-core/src/main/java/cn/hippo4j/core/config/WebThreadPoolConfiguration.java diff --git a/hippo4j-common/pom.xml b/hippo4j-common/pom.xml index 7124e28f..a897c7f0 100644 --- a/hippo4j-common/pom.xml +++ b/hippo4j-common/pom.xml @@ -53,6 +53,27 @@ true + + + org.springframework.boot + spring-boot-starter-tomcat + compile + true + + + + org.springframework.boot + spring-boot-starter-jetty + compile + true + + + + org.springframework.boot + spring-boot-starter-undertow + compile + true + diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/AbstractWebThreadPoolService.java similarity index 80% rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java rename to hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/AbstractWebThreadPoolService.java index ef98c34f..87674dc6 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/AbstractWebThreadPoolService.java @@ -1,7 +1,9 @@ -package cn.hippo4j.starter.handler.web; +package cn.hippo4j.common.web.executor; import cn.hippo4j.common.config.ApplicationContextHolder; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; import org.springframework.boot.web.context.WebServerApplicationContext; import org.springframework.boot.web.server.WebServer; import org.springframework.context.ApplicationContext; @@ -15,7 +17,7 @@ import java.util.concurrent.Executor; * @date 2022/1/19 21:20 */ @Slf4j -public abstract class AbstractWebThreadPoolService implements WebThreadPoolService { +public abstract class AbstractWebThreadPoolService implements WebThreadPoolService, ApplicationRunner { /** * Thread pool executor. @@ -25,6 +27,7 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi /** * Get web thread pool by server. * + * @param webServer * @return */ protected abstract Executor getWebThreadPoolByServer(WebServer webServer); @@ -44,4 +47,9 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi return executor; } + @Override + public void run(ApplicationArguments args) { + getWebThreadPool(); + } + } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/JettyWebThreadPoolHandler.java similarity index 69% rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java rename to hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/JettyWebThreadPoolHandler.java index 443e9265..11518230 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/JettyWebThreadPoolHandler.java @@ -1,5 +1,6 @@ -package cn.hippo4j.starter.handler.web; +package cn.hippo4j.common.web.executor; +import cn.hippo4j.common.model.PoolParameter; import cn.hippo4j.common.model.PoolParameterInfo; import lombok.extern.slf4j.Slf4j; import org.eclipse.jetty.util.thread.QueuedThreadPool; @@ -22,6 +23,25 @@ public class JettyWebThreadPoolHandler extends AbstractWebThreadPoolService { return jettyWebServer.getServer().getThreadPool(); } + @Override + public PoolParameter getWebThreadPoolParameter() { + PoolParameterInfo parameterInfo = null; + try { + parameterInfo = new PoolParameterInfo(); + QueuedThreadPool jettyExecutor = (QueuedThreadPool) executor; + + int minThreads = jettyExecutor.getMinThreads(); + int maxThreads = jettyExecutor.getMaxThreads(); + + parameterInfo.setCoreSize(minThreads); + parameterInfo.setMaxSize(maxThreads); + } catch (Exception ex) { + log.error("Failed to get the jetty thread pool parameter.", ex); + } + + return parameterInfo; + } + @Override public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/TomcatWebThreadPoolHandler.java similarity index 75% rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java rename to hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/TomcatWebThreadPoolHandler.java index b01ad194..61a1c154 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/TomcatWebThreadPoolHandler.java @@ -1,5 +1,6 @@ -package cn.hippo4j.starter.handler.web; +package cn.hippo4j.common.web.executor; +import cn.hippo4j.common.model.PoolParameter; import cn.hippo4j.common.model.PoolParameterInfo; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -44,6 +45,26 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService { return tomcatExecutor; } + @Override + public PoolParameter getWebThreadPoolParameter() { + PoolParameterInfo parameterInfo = null; + try { + parameterInfo = new PoolParameterInfo(); + ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor; + int minThreads = tomcatExecutor.getCorePoolSize(); + int maxThreads = tomcatExecutor.getMaximumPoolSize(); + long keepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS); + + parameterInfo.setCoreSize(minThreads); + parameterInfo.setMaxSize(maxThreads); + parameterInfo.setKeepAliveTime((int) keepAliveTime); + } catch (Exception ex) { + log.error("Failed to get the tomcat thread pool parameter.", ex); + } + + return parameterInfo; + } + @Override public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/UndertowWebThreadPoolHandler.java similarity index 74% rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java rename to hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/UndertowWebThreadPoolHandler.java index 808664a4..b57cdec6 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/UndertowWebThreadPoolHandler.java @@ -1,5 +1,6 @@ -package cn.hippo4j.starter.handler.web; +package cn.hippo4j.common.web.executor; +import cn.hippo4j.common.model.PoolParameter; import cn.hippo4j.common.model.PoolParameterInfo; import io.undertow.Undertow; import lombok.extern.slf4j.Slf4j; @@ -30,10 +31,31 @@ public class UndertowWebThreadPoolHandler extends AbstractWebThreadPoolService { UndertowWebServer undertowWebServer = (UndertowWebServer) webServer; Field undertowField = ReflectionUtils.findField(UndertowWebServer.class, UNDERTOW_NAME); ReflectionUtils.makeAccessible(undertowField); + Undertow undertow = (Undertow) ReflectionUtils.getField(undertowField, undertowWebServer); return Objects.isNull(undertow) ? null : undertow.getWorker(); } + @Override + public PoolParameter getWebThreadPoolParameter() { + PoolParameterInfo parameterInfo = null; + try { + parameterInfo = new PoolParameterInfo(); + XnioWorker xnioWorker = (XnioWorker) executor; + int minThreads = xnioWorker.getOption(Options.WORKER_TASK_CORE_THREADS); + int maxThreads = xnioWorker.getOption(Options.WORKER_TASK_MAX_THREADS); + int keepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE); + + parameterInfo.setCoreSize(minThreads); + parameterInfo.setMaxSize(maxThreads); + parameterInfo.setKeepAliveTime(keepAliveTime); + } catch (Exception ex) { + log.error("Failed to get the undertow thread pool parameter.", ex); + } + + return parameterInfo; + } + @Override public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolHandlerChoose.java b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/WebThreadPoolHandlerChoose.java similarity index 94% rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolHandlerChoose.java rename to hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/WebThreadPoolHandlerChoose.java index bcb8097c..e34cefef 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolHandlerChoose.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/WebThreadPoolHandlerChoose.java @@ -1,4 +1,4 @@ -package cn.hippo4j.starter.handler.web; +package cn.hippo4j.common.web.executor; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.web.exception.ServiceException; diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolService.java b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/WebThreadPoolService.java similarity index 71% rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolService.java rename to hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/WebThreadPoolService.java index 4085a872..7f1097a5 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolService.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/web/executor/WebThreadPoolService.java @@ -1,5 +1,6 @@ -package cn.hippo4j.starter.handler.web; +package cn.hippo4j.common.web.executor; +import cn.hippo4j.common.model.PoolParameter; import cn.hippo4j.common.model.PoolParameterInfo; import java.util.concurrent.Executor; @@ -19,6 +20,13 @@ public interface WebThreadPoolService { */ Executor getWebThreadPool(); + /** + * Get web thread pool parameter. + * + * @return + */ + PoolParameter getWebThreadPoolParameter(); + /** * Update web thread pool. * diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/config/WebThreadPoolConfiguration.java b/hippo4j-core/src/main/java/cn/hippo4j/core/config/WebThreadPoolConfiguration.java new file mode 100644 index 00000000..064eabef --- /dev/null +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/config/WebThreadPoolConfiguration.java @@ -0,0 +1,49 @@ +package cn.hippo4j.core.config; + +import cn.hippo4j.common.web.executor.JettyWebThreadPoolHandler; +import cn.hippo4j.common.web.executor.TomcatWebThreadPoolHandler; +import cn.hippo4j.common.web.executor.UndertowWebThreadPoolHandler; +import cn.hippo4j.common.web.executor.WebThreadPoolHandlerChoose; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Web thread pool configuration. + * + * @author chen.ma + * @date 2022/3/11 19:09 + */ +@Configuration +public class WebThreadPoolConfiguration { + + private static final String TOMCAT_SERVLET_WEB_SERVER_FACTORY = "tomcatServletWebServerFactory"; + + private static final String JETTY_SERVLET_WEB_SERVER_FACTORY = "JettyServletWebServerFactory"; + + private static final String UNDERTOW_SERVLET_WEB_SERVER_FACTORY = "undertowServletWebServerFactory"; + + @Bean + @ConditionalOnBean(name = TOMCAT_SERVLET_WEB_SERVER_FACTORY) + public TomcatWebThreadPoolHandler tomcatWebThreadPoolHandler() { + return new TomcatWebThreadPoolHandler(); + } + + @Bean + @ConditionalOnBean(name = JETTY_SERVLET_WEB_SERVER_FACTORY) + public JettyWebThreadPoolHandler jettyWebThreadPoolHandler() { + return new JettyWebThreadPoolHandler(); + } + + @Bean + @ConditionalOnBean(name = UNDERTOW_SERVLET_WEB_SERVER_FACTORY) + public UndertowWebThreadPoolHandler undertowWebThreadPoolHandler() { + return new UndertowWebThreadPoolHandler(); + } + + @Bean + public WebThreadPoolHandlerChoose webThreadPoolServiceChoose() { + return new WebThreadPoolHandlerChoose(); + } + +} diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml b/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml index fd99608e..f63bacef 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml @@ -25,20 +25,6 @@ spring-boot-starter-web - - org.springframework.boot - spring-boot-starter-jetty - compile - true - - - - org.springframework.boot - spring-boot-starter-undertow - compile - true - - com.squareup.okhttp3 okhttp @@ -76,6 +62,27 @@ + + + org.springframework.boot + spring-boot-starter-tomcat + compile + true + + + + org.springframework.boot + spring-boot-starter-jetty + compile + true + + + + org.springframework.boot + spring-boot-starter-undertow + compile + true + diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java index ca98697a..d338e02b 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -2,7 +2,9 @@ package cn.hippo4j.starter.config; import cn.hippo4j.common.api.ThreadDetailState; import cn.hippo4j.common.config.ApplicationContextHolder; +import cn.hippo4j.common.web.executor.WebThreadPoolHandlerChoose; import cn.hippo4j.core.config.UtilAutoConfiguration; +import cn.hippo4j.core.config.WebThreadPoolConfiguration; import cn.hippo4j.core.enable.MarkerConfiguration; import cn.hippo4j.core.handler.DynamicThreadPoolBannerHandler; import cn.hippo4j.core.toolkit.IdentifyUtil; @@ -13,7 +15,7 @@ import cn.hippo4j.starter.core.*; import cn.hippo4j.starter.event.ApplicationContentPostProcessor; import cn.hippo4j.starter.handler.BaseThreadDetailStateHandler; import cn.hippo4j.starter.handler.ThreadPoolRunStateHandler; -import cn.hippo4j.starter.handler.web.*; +import cn.hippo4j.starter.handler.web.WebThreadPoolRunStateHandler; import cn.hippo4j.starter.monitor.ReportingEventExecutor; import cn.hippo4j.starter.monitor.collect.RunTimeInfoCollector; import cn.hippo4j.starter.monitor.send.HttpConnectSender; @@ -44,15 +46,9 @@ import org.springframework.core.env.ConfigurableEnvironment; @ConditionalOnBean(MarkerConfiguration.Marker.class) @EnableConfigurationProperties(BootstrapProperties.class) @ConditionalOnProperty(prefix = BootstrapProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true") -@ImportAutoConfiguration({HttpClientConfiguration.class, DiscoveryConfiguration.class, MessageNotifyConfiguration.class, UtilAutoConfiguration.class}) +@ImportAutoConfiguration({HttpClientConfiguration.class, DiscoveryConfiguration.class, MessageNotifyConfiguration.class, UtilAutoConfiguration.class, WebThreadPoolConfiguration.class}) public class DynamicThreadPoolAutoConfiguration { - private static final String TOMCAT_SERVLET_WEB_SERVER_FACTORY = "tomcatServletWebServerFactory"; - - private static final String JETTY_SERVLET_WEB_SERVER_FACTORY = "JettyServletWebServerFactory"; - - private static final String UNDERTOW_SERVLET_WEB_SERVER_FACTORY = "undertowServletWebServerFactory"; - private final BootstrapProperties properties; private final ConfigurableEnvironment environment; @@ -140,29 +136,6 @@ public class DynamicThreadPoolAutoConfiguration { return new WebThreadPoolRunStateHandler(); } - @Bean - @ConditionalOnBean(name = TOMCAT_SERVLET_WEB_SERVER_FACTORY) - public TomcatWebThreadPoolHandler tomcatWebThreadPoolHandler() { - return new TomcatWebThreadPoolHandler(); - } - - @Bean - @ConditionalOnBean(name = JETTY_SERVLET_WEB_SERVER_FACTORY) - public JettyWebThreadPoolHandler jettyWebThreadPoolHandler() { - return new JettyWebThreadPoolHandler(); - } - - @Bean - @ConditionalOnBean(name = UNDERTOW_SERVLET_WEB_SERVER_FACTORY) - public UndertowWebThreadPoolHandler undertowWebThreadPoolHandler() { - return new UndertowWebThreadPoolHandler(); - } - - @Bean - public WebThreadPoolHandlerChoose webThreadPoolServiceChoose() { - return new WebThreadPoolHandlerChoose(); - } - @Bean public WebThreadPoolController webThreadPoolController(WebThreadPoolHandlerChoose webThreadPoolServiceChoose, WebThreadPoolRunStateHandler webThreadPoolRunStateHandler) { diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/controller/WebThreadPoolController.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/controller/WebThreadPoolController.java index 959fe58d..ba36582f 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/controller/WebThreadPoolController.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/controller/WebThreadPoolController.java @@ -5,9 +5,9 @@ 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.common.web.executor.WebThreadPoolHandlerChoose; import cn.hippo4j.starter.handler.web.WebThreadPoolRunStateHandler; -import cn.hippo4j.starter.handler.web.WebThreadPoolService; +import cn.hippo4j.common.web.executor.WebThreadPoolService; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.*;