update jetty、Undertow ThreadPool

pull/117/head
weihu 3 years ago
parent 491cdacf74
commit 4558594b41

@ -51,17 +51,5 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi
return executor; 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())
);
}
} }

@ -2,12 +2,11 @@ package cn.hippo4j.starter.handler.web;
import cn.hippo4j.common.model.PoolParameterInfo; import cn.hippo4j.common.model.PoolParameterInfo;
import lombok.extern.slf4j.Slf4j; 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.embedded.jetty.JettyWebServer;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* @author : wh * @author : wh
@ -28,15 +27,22 @@ public class JettyWebThreadPoolHandler extends AbstractWebThreadPoolService{
@Override @Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try { try {
ThreadPoolExecutor jettyExecutor = (ThreadPoolExecutor) executor; ThreadPool.SizedThreadPool jettyExecutor = (ThreadPool.SizedThreadPool) executor;
jettyExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
jettyExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); Integer coreSize = poolParameterInfo.getCoreSize();
jettyExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); Integer maxSize = poolParameterInfo.getMaxSize();
jettyExecutor.setMinThreads(coreSize);
super.log(poolParameterInfo, jettyExecutor); 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) { } catch (Exception ex) {
log.error("Failed to modify the jetty thread pool parameter.", ex); log.error("Failed to modify the jetty thread pool parameter.", ex);
} }
} }
} }

@ -52,8 +52,16 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService{
tomcatExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); tomcatExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
tomcatExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); tomcatExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize());
tomcatExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); 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) { } catch (Exception ex) {
log.error("Failed to modify the Tomcat thread pool parameter.", ex); log.error("Failed to modify the Tomcat thread pool parameter.", ex);

@ -2,18 +2,16 @@ package cn.hippo4j.starter.handler.web;
import cn.hippo4j.common.model.PoolParameterInfo; import cn.hippo4j.common.model.PoolParameterInfo;
import io.undertow.Undertow; import io.undertow.Undertow;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.embedded.undertow.UndertowWebServer; import org.springframework.boot.web.embedded.undertow.UndertowWebServer;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.xnio.Options;
import org.xnio.XnioWorker;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* Undertow web thread pool handler. * Undertow web thread pool handler.
@ -42,11 +40,25 @@ public class UndertowWebThreadPoolHandler extends AbstractWebThreadPoolService {
@Override @Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try { try {
ThreadPoolExecutor undertowExecutor = (ThreadPoolExecutor) executor; XnioWorker xnioWorker = (XnioWorker) executor;
undertowExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
undertowExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); Integer coreSize = poolParameterInfo.getCoreSize();
undertowExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); Integer maxSize = poolParameterInfo.getMaxSize();
super.log(poolParameterInfo, undertowExecutor); 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) { } catch (Exception ex) {
log.error("Failed to modify the undertow thread pool parameter.", ex); log.error("Failed to modify the undertow thread pool parameter.", ex);

Loading…
Cancel
Save