Optimize Tomcat thread pool adaptation

pull/275/head
chen.ma 2 years ago
parent 3014bf36f1
commit 7425dbbe90

@ -71,32 +71,16 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
@Override @Override
public ThreadPoolBaseInfo simpleInfo() { public ThreadPoolBaseInfo simpleInfo() {
ThreadPoolBaseInfo poolBaseInfo = new ThreadPoolBaseInfo(); ThreadPoolBaseInfo poolBaseInfo = new ThreadPoolBaseInfo();
int corePoolSize, maximumPoolSize, queueCapacity; org.apache.tomcat.util.threads.ThreadPoolExecutor tomcatThreadPoolExecutor = (org.apache.tomcat.util.threads.ThreadPoolExecutor) executor;
long keepAliveTime; int corePoolSize = tomcatThreadPoolExecutor.getCorePoolSize();
String rejectedExecutionHandlerName; int maximumPoolSize = tomcatThreadPoolExecutor.getMaximumPoolSize();
BlockingQueue<Runnable> blockingQueue; long keepAliveTime = tomcatThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
if (executor instanceof ThreadPoolExecutor) { BlockingQueue<?> blockingQueue = tomcatThreadPoolExecutor.getQueue();
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; int queueSize = blockingQueue.size();
corePoolSize = threadPoolExecutor.getCorePoolSize(); int remainingCapacity = blockingQueue.remainingCapacity();
maximumPoolSize = threadPoolExecutor.getMaximumPoolSize(); int queueCapacity = queueSize + remainingCapacity;
keepAliveTime = threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS); String rejectedExecutionHandlerName = executor instanceof ThreadPoolExecutor ? ((ThreadPoolExecutor) executor).getRejectedExecutionHandler().getClass().getSimpleName()
blockingQueue = threadPoolExecutor.getQueue(); : tomcatThreadPoolExecutor.getRejectedExecutionHandler().getClass().getSimpleName();
int queueSize = blockingQueue.size();
int remainingCapacity = blockingQueue.remainingCapacity();
queueCapacity = queueSize + remainingCapacity;
RejectedExecutionHandler rejectedExecutionHandler = threadPoolExecutor.getRejectedExecutionHandler();
rejectedExecutionHandlerName = rejectedExecutionHandler.getClass().getSimpleName();
} else {
org.apache.tomcat.util.threads.ThreadPoolExecutor tomcatThreadPoolExecutor = (org.apache.tomcat.util.threads.ThreadPoolExecutor) executor;
corePoolSize = tomcatThreadPoolExecutor.getCorePoolSize();
maximumPoolSize = tomcatThreadPoolExecutor.getMaximumPoolSize();
keepAliveTime = tomcatThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
blockingQueue = tomcatThreadPoolExecutor.getQueue();
int queueSize = blockingQueue.size();
int remainingCapacity = blockingQueue.remainingCapacity();
queueCapacity = queueSize + remainingCapacity;
rejectedExecutionHandlerName = tomcatThreadPoolExecutor.getRejectedExecutionHandler().getClass().getSimpleName();
}
poolBaseInfo.setCoreSize(corePoolSize); poolBaseInfo.setCoreSize(corePoolSize);
poolBaseInfo.setMaximumSize(maximumPoolSize); poolBaseInfo.setMaximumSize(maximumPoolSize);
poolBaseInfo.setKeepAliveTime(keepAliveTime); poolBaseInfo.setKeepAliveTime(keepAliveTime);
@ -109,20 +93,11 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
@Override @Override
public ThreadPoolParameter getWebThreadPoolParameter() { public ThreadPoolParameter getWebThreadPoolParameter() {
ThreadPoolParameterInfo parameterInfo = new ThreadPoolParameterInfo(); ThreadPoolParameterInfo parameterInfo = new ThreadPoolParameterInfo();
int minThreads, maxThreads;
long keepAliveTime;
try { try {
if (executor instanceof ThreadPoolExecutor) { org.apache.tomcat.util.threads.ThreadPoolExecutor tomcatThreadPoolExecutor = (org.apache.tomcat.util.threads.ThreadPoolExecutor) executor;
ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor; int minThreads = tomcatThreadPoolExecutor.getCorePoolSize();
minThreads = tomcatExecutor.getCorePoolSize(); int maxThreads = tomcatThreadPoolExecutor.getMaximumPoolSize();
maxThreads = tomcatExecutor.getMaximumPoolSize(); long keepAliveTime = tomcatThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
keepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS);
} else {
org.apache.tomcat.util.threads.ThreadPoolExecutor tomcatThreadPoolExecutor = (org.apache.tomcat.util.threads.ThreadPoolExecutor) executor;
minThreads = tomcatThreadPoolExecutor.getCorePoolSize();
maxThreads = tomcatThreadPoolExecutor.getMaximumPoolSize();
keepAliveTime = tomcatThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
}
parameterInfo.setCoreSize(minThreads); parameterInfo.setCoreSize(minThreads);
parameterInfo.setMaxSize(maxThreads); parameterInfo.setMaxSize(maxThreads);
parameterInfo.setKeepAliveTime((int) keepAliveTime); parameterInfo.setKeepAliveTime((int) keepAliveTime);
@ -183,26 +158,14 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
@Override @Override
public void updateWebThreadPool(ThreadPoolParameterInfo threadPoolParameterInfo) { public void updateWebThreadPool(ThreadPoolParameterInfo threadPoolParameterInfo) {
int originalCoreSize, originalMaximumPoolSize;
long originalKeepAliveTime;
try { try {
if (executor instanceof ThreadPoolExecutor) { org.apache.tomcat.util.threads.ThreadPoolExecutor tomcatThreadPoolExecutor = (org.apache.tomcat.util.threads.ThreadPoolExecutor) executor;
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; int originalCoreSize = tomcatThreadPoolExecutor.getCorePoolSize();
originalCoreSize = threadPoolExecutor.getCorePoolSize(); int originalMaximumPoolSize = tomcatThreadPoolExecutor.getMaximumPoolSize();
originalMaximumPoolSize = threadPoolExecutor.getMaximumPoolSize(); long originalKeepAliveTime = tomcatThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
originalKeepAliveTime = threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS); tomcatThreadPoolExecutor.setCorePoolSize(threadPoolParameterInfo.corePoolSizeAdapt());
threadPoolExecutor.setCorePoolSize(threadPoolParameterInfo.corePoolSizeAdapt()); tomcatThreadPoolExecutor.setMaximumPoolSize(threadPoolParameterInfo.maximumPoolSizeAdapt());
threadPoolExecutor.setMaximumPoolSize(threadPoolParameterInfo.maximumPoolSizeAdapt()); tomcatThreadPoolExecutor.setKeepAliveTime(threadPoolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);
threadPoolExecutor.setKeepAliveTime(threadPoolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);
} else {
org.apache.tomcat.util.threads.ThreadPoolExecutor tomcatThreadPoolExecutor = (org.apache.tomcat.util.threads.ThreadPoolExecutor) executor;
originalCoreSize = tomcatThreadPoolExecutor.getCorePoolSize();
originalMaximumPoolSize = tomcatThreadPoolExecutor.getMaximumPoolSize();
originalKeepAliveTime = tomcatThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
tomcatThreadPoolExecutor.setCorePoolSize(threadPoolParameterInfo.corePoolSizeAdapt());
tomcatThreadPoolExecutor.setMaximumPoolSize(threadPoolParameterInfo.maximumPoolSizeAdapt());
tomcatThreadPoolExecutor.setKeepAliveTime(threadPoolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);
}
log.info("[TOMCAT] Changed web thread pool. corePoolSize :: [{}], maximumPoolSize :: [{}], keepAliveTime :: [{}]", log.info("[TOMCAT] Changed web thread pool. corePoolSize :: [{}], maximumPoolSize :: [{}], keepAliveTime :: [{}]",
String.format(CHANGE_DELIMITER, originalCoreSize, threadPoolParameterInfo.corePoolSizeAdapt()), String.format(CHANGE_DELIMITER, originalCoreSize, threadPoolParameterInfo.corePoolSizeAdapt()),
String.format(CHANGE_DELIMITER, originalMaximumPoolSize, threadPoolParameterInfo.maximumPoolSizeAdapt()), String.format(CHANGE_DELIMITER, originalMaximumPoolSize, threadPoolParameterInfo.maximumPoolSizeAdapt()),

Loading…
Cancel
Save