diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java index 8168610f..71531a79 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java @@ -51,17 +51,5 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi return executor; } - public void log(PoolParameterInfo poolParameterInfo, ThreadPoolExecutor threadPoolExecutor) { - int originalCoreSize = threadPoolExecutor.getCorePoolSize(); - int originalMaximumPoolSize = threadPoolExecutor.getMaximumPoolSize(); - long originalKeepAliveTime = threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS); - log.info( - "🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]", - String.format("%s => %s", originalCoreSize, poolParameterInfo.getCoreSize()), - String.format("%s => %s", originalMaximumPoolSize, poolParameterInfo.getMaxSize()), - String.format("%s => %s", originalKeepAliveTime, poolParameterInfo.getKeepAliveTime()) - ); - - } } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java index 1fe1798e..0bf43750 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java @@ -2,12 +2,11 @@ package cn.hippo4j.starter.handler.web; import cn.hippo4j.common.model.PoolParameterInfo; import lombok.extern.slf4j.Slf4j; +import org.eclipse.jetty.util.thread.ThreadPool; import org.springframework.boot.web.embedded.jetty.JettyWebServer; import org.springframework.boot.web.server.WebServer; import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; /** * @author : wh @@ -28,15 +27,22 @@ public class JettyWebThreadPoolHandler extends AbstractWebThreadPoolService{ @Override public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { - ThreadPoolExecutor jettyExecutor = (ThreadPoolExecutor) executor; - jettyExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); - jettyExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); - jettyExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); - - super.log(poolParameterInfo, jettyExecutor); + ThreadPool.SizedThreadPool jettyExecutor = (ThreadPool.SizedThreadPool) executor; + + Integer coreSize = poolParameterInfo.getCoreSize(); + Integer maxSize = poolParameterInfo.getMaxSize(); + jettyExecutor.setMinThreads(coreSize); + jettyExecutor.setMaxThreads(maxSize); + log.info( + "🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}]", + String.format("%s => %s", jettyExecutor.getMinThreads(), coreSize), + String.format("%s => %s", jettyExecutor.getMaxThreads(), maxSize) + ); } catch (Exception ex) { log.error("Failed to modify the jetty thread pool parameter.", ex); } } + + } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java index 263eb119..178a14f3 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java @@ -52,8 +52,16 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService{ tomcatExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); tomcatExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); tomcatExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); + int originalCoreSize = tomcatExecutor.getCorePoolSize(); + int originalMaximumPoolSize = tomcatExecutor.getMaximumPoolSize(); + long originalKeepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS); + log.info( + "🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]", + String.format("%s => %s", originalCoreSize, poolParameterInfo.getCoreSize()), + String.format("%s => %s", originalMaximumPoolSize, poolParameterInfo.getMaxSize()), + String.format("%s => %s", originalKeepAliveTime, poolParameterInfo.getKeepAliveTime()) + ); - super.log(poolParameterInfo, tomcatExecutor); } catch (Exception ex) { log.error("Failed to modify the Tomcat thread pool parameter.", ex); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java index ee432452..b0b2262e 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java @@ -2,18 +2,16 @@ package cn.hippo4j.starter.handler.web; import cn.hippo4j.common.model.PoolParameterInfo; import io.undertow.Undertow; -import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.embedded.undertow.UndertowWebServer; import org.springframework.boot.web.server.WebServer; -import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; import org.springframework.util.ReflectionUtils; +import org.xnio.Options; +import org.xnio.XnioWorker; import java.lang.reflect.Field; import java.util.Objects; import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; /** * Undertow web thread pool handler. @@ -42,11 +40,25 @@ public class UndertowWebThreadPoolHandler extends AbstractWebThreadPoolService { @Override public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { - ThreadPoolExecutor undertowExecutor = (ThreadPoolExecutor) executor; - undertowExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); - undertowExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); - undertowExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); - super.log(poolParameterInfo, undertowExecutor); + XnioWorker xnioWorker = (XnioWorker) executor; + + Integer coreSize = poolParameterInfo.getCoreSize(); + Integer maxSize = poolParameterInfo.getMaxSize(); + Integer keepAliveTime = poolParameterInfo.getKeepAliveTime(); + + int originalCoreSize = xnioWorker.getOption(Options.WORKER_TASK_CORE_THREADS); + int originalMaximumPoolSize = xnioWorker.getOption(Options.WORKER_TASK_MAX_THREADS); + int originalKeepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE); + + xnioWorker.setOption(Options.WORKER_TASK_CORE_THREADS, coreSize); + xnioWorker.setOption(Options.WORKER_TASK_MAX_THREADS, maxSize); + xnioWorker.setOption(Options.WORKER_TASK_KEEPALIVE, keepAliveTime); + log.info( + "🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]", + String.format("%s => %s", originalCoreSize, coreSize), + String.format("%s => %s", originalMaximumPoolSize, maxSize), + String.format("%s => %s", originalKeepAliveTime, keepAliveTime) + ); } catch (Exception ex) { log.error("Failed to modify the undertow thread pool parameter.", ex);