At present, only Tomcat is well supported, and other web containers need to be improved. + * * @author chen.ma * @date 2022/1/19 20:54 */ 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 6a5d9d9f..443e9265 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,7 +2,7 @@ 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.eclipse.jetty.util.thread.QueuedThreadPool; import org.springframework.boot.web.embedded.jetty.JettyWebServer; import org.springframework.boot.web.server.WebServer; @@ -25,17 +25,21 @@ public class JettyWebThreadPoolHandler extends AbstractWebThreadPoolService { @Override public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { - ThreadPool.SizedThreadPool jettyExecutor = (ThreadPool.SizedThreadPool) executor; + QueuedThreadPool jettyExecutor = (QueuedThreadPool) executor; + + int minThreads = jettyExecutor.getMinThreads(); + int maxThreads = jettyExecutor.getMaxThreads(); 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) + String.format("%s => %s", minThreads, jettyExecutor.getMinThreads()), + String.format("%s => %s", maxThreads, jettyExecutor.getMaxThreads()) ); } 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 7aa8ba7e..b01ad194 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 @@ -48,14 +48,13 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService { public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor; - 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); + tomcatExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); + tomcatExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); + tomcatExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); log.info( "🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]", String.format("%s => %s", originalCoreSize, poolParameterInfo.getCoreSize()), diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolRunStateHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolRunStateHandler.java index bf7d2553..dfea233b 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolRunStateHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/WebThreadPoolRunStateHandler.java @@ -2,11 +2,17 @@ package cn.hippo4j.starter.handler.web; import cn.hippo4j.common.model.PoolBaseInfo; import cn.hippo4j.common.model.PoolRunStateInfo; +import cn.hippo4j.common.toolkit.ReflectUtil; import cn.hippo4j.starter.handler.AbstractThreadPoolRuntime; import cn.hippo4j.starter.toolkit.ByteConvertUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.system.RuntimeInfo; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.xnio.Options; +import org.xnio.XnioWorker; +import java.util.Objects; import java.util.concurrent.*; /** @@ -15,6 +21,7 @@ import java.util.concurrent.*; * @author chen.ma * @date 2022/1/19 21:05 */ +@Slf4j public class WebThreadPoolRunStateHandler extends AbstractThreadPoolRuntime { @Override @@ -38,7 +45,35 @@ public class WebThreadPoolRunStateHandler extends AbstractThreadPoolRuntime { poolBaseInfo.setQueueType(queue.getClass().getSimpleName()); poolBaseInfo.setQueueCapacity(queueCapacity); poolBaseInfo.setRejectedName(rejectedExecutionHandler.getClass().getSimpleName()); + } else if (Objects.equals("QueuedThreadPool", executor.getClass().getSimpleName())) { + QueuedThreadPool queuedThreadPool = (QueuedThreadPool) executor; + poolBaseInfo.setCoreSize(queuedThreadPool.getMinThreads()); + poolBaseInfo.setMaximumSize(queuedThreadPool.getMaxThreads()); + + BlockingQueue jobs = (BlockingQueue) ReflectUtil.getFieldValue(queuedThreadPool, "_jobs"); + int queueCapacity = jobs.remainingCapacity() + jobs.size(); + + poolBaseInfo.setQueueCapacity(queueCapacity); + poolBaseInfo.setQueueType(jobs.getClass().getSimpleName()); + poolBaseInfo.setKeepAliveTime((long) queuedThreadPool.getIdleTimeout()); + poolBaseInfo.setRejectedName("RejectedExecutionException"); + } else if (Objects.equals("NioXnioWorker", executor.getClass().getSimpleName())) { + XnioWorker xnioWorker = (XnioWorker) executor; + try { + int coreSize = xnioWorker.getOption(Options.WORKER_TASK_CORE_THREADS); + int maximumPoolSize = xnioWorker.getOption(Options.WORKER_TASK_MAX_THREADS); + int keepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE); + + poolBaseInfo.setCoreSize(coreSize); + poolBaseInfo.setMaximumSize(maximumPoolSize); + poolBaseInfo.setKeepAliveTime((long) keepAliveTime); + poolBaseInfo.setRejectedName("-"); + poolBaseInfo.setQueueType("-"); + } catch (Exception ex) { + log.error("The undertow container failed to get thread pool parameters.", ex); + } } + return poolBaseInfo; }