Skip to content

Commit

Permalink
容器线程池支持 Jetty、Undertow. (#113#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Mar 6, 2022
1 parent 1f51cbf commit 9397eca
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
26 changes: 26 additions & 0 deletions hippo4j-example/hippo4j-spring-boot-starter-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
/**
* 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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;

Expand All @@ -25,17 +25,21 @@ protected Executor getWebThreadPoolByServer(WebServer webServer) {
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ protected Executor getWebThreadPoolByServer(WebServer webServer) {
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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

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.*;

/**
Expand All @@ -15,6 +21,7 @@
* @author chen.ma
* @date 2022/1/19 21:05
*/
@Slf4j
public class WebThreadPoolRunStateHandler extends AbstractThreadPoolRuntime {

@Override
Expand All @@ -38,7 +45,35 @@ public PoolBaseInfo simpleInfo(Executor executor) {
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;
}

Expand Down

0 comments on commit 9397eca

Please # to comment.