容器线程池支持 Jetty、Undertow. (#113、#114)

pull/131/head
chen.ma 2 years ago
parent 1f51cbfbdb
commit 9397ecab8d

@ -27,6 +27,32 @@
<artifactId>hippo4j-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>-->
</dependencies>
<build>

@ -47,7 +47,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
@ImportAutoConfiguration({HttpClientConfiguration.class, DiscoveryConfiguration.class, MessageNotifyConfiguration.class, UtilAutoConfiguration.class})
public class DynamicThreadPoolAutoConfiguration {
private static final String TOMCAT_SERVLET_WEB_SERVER_FACTORY = "tomcatWebThreadPoolHandler";
private static final String TOMCAT_SERVLET_WEB_SERVER_FACTORY = "tomcatServletWebServerFactory";
private static final String JETTY_SERVLET_WEB_SERVER_FACTORY = "JettyServletWebServerFactory";

@ -16,6 +16,8 @@ import java.util.concurrent.Executor;
/**
* Web thread pool controller.
*
* <p> At present, only Tomcat is well supported, and other web containers need to be improved.
*
* @author chen.ma
* @date 2022/1/19 20:54
*/

@ -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);

@ -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()),

@ -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;
}

Loading…
Cancel
Save