|
|
@ -22,11 +22,14 @@ import cn.hippo4j.common.model.ThreadPoolParameter;
|
|
|
|
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
|
|
|
|
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
|
|
|
|
import cn.hippo4j.common.model.ThreadPoolRunStateInfo;
|
|
|
|
import cn.hippo4j.common.model.ThreadPoolRunStateInfo;
|
|
|
|
import cn.hippo4j.core.executor.state.AbstractThreadPoolRuntime;
|
|
|
|
import cn.hippo4j.core.executor.state.AbstractThreadPoolRuntime;
|
|
|
|
|
|
|
|
import cn.hippo4j.core.toolkit.CalculateUtil;
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
|
|
|
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
|
|
|
|
import org.springframework.boot.web.server.WebServer;
|
|
|
|
import org.springframework.boot.web.server.WebServer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
|
|
|
|
|
@ -68,36 +71,58 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public ThreadPoolBaseInfo simpleInfo() {
|
|
|
|
public ThreadPoolBaseInfo simpleInfo() {
|
|
|
|
ThreadPoolBaseInfo poolBaseInfo = new ThreadPoolBaseInfo();
|
|
|
|
ThreadPoolBaseInfo poolBaseInfo = new ThreadPoolBaseInfo();
|
|
|
|
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
|
|
|
|
int corePoolSize, maximumPoolSize, queueCapacity;
|
|
|
|
int corePoolSize = threadPoolExecutor.getCorePoolSize();
|
|
|
|
long keepAliveTime;
|
|
|
|
int maximumPoolSize = threadPoolExecutor.getMaximumPoolSize();
|
|
|
|
String rejectedExecutionHandlerName;
|
|
|
|
RejectedExecutionHandler rejectedExecutionHandler = threadPoolExecutor.getRejectedExecutionHandler();
|
|
|
|
BlockingQueue<Runnable> blockingQueue;
|
|
|
|
long keepAliveTime = threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
|
|
|
|
if (executor instanceof ThreadPoolExecutor) {
|
|
|
|
|
|
|
|
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
|
|
|
|
BlockingQueue<Runnable> queue = threadPoolExecutor.getQueue();
|
|
|
|
corePoolSize = threadPoolExecutor.getCorePoolSize();
|
|
|
|
int queueSize = queue.size();
|
|
|
|
maximumPoolSize = threadPoolExecutor.getMaximumPoolSize();
|
|
|
|
int remainingCapacity = queue.remainingCapacity();
|
|
|
|
keepAliveTime = threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
|
|
|
|
int queueCapacity = queueSize + remainingCapacity;
|
|
|
|
blockingQueue = threadPoolExecutor.getQueue();
|
|
|
|
|
|
|
|
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);
|
|
|
|
poolBaseInfo.setQueueType(queue.getClass().getSimpleName());
|
|
|
|
poolBaseInfo.setQueueType(blockingQueue.getClass().getSimpleName());
|
|
|
|
poolBaseInfo.setQueueCapacity(queueCapacity);
|
|
|
|
poolBaseInfo.setQueueCapacity(queueCapacity);
|
|
|
|
poolBaseInfo.setRejectedName(rejectedExecutionHandler.getClass().getSimpleName());
|
|
|
|
poolBaseInfo.setRejectedName(rejectedExecutionHandlerName);
|
|
|
|
return poolBaseInfo;
|
|
|
|
return poolBaseInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public ThreadPoolParameter getWebThreadPoolParameter() {
|
|
|
|
public ThreadPoolParameter getWebThreadPoolParameter() {
|
|
|
|
ThreadPoolParameterInfo parameterInfo = null;
|
|
|
|
ThreadPoolParameterInfo parameterInfo = new ThreadPoolParameterInfo();
|
|
|
|
|
|
|
|
int minThreads, maxThreads;
|
|
|
|
|
|
|
|
long keepAliveTime;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
parameterInfo = new ThreadPoolParameterInfo();
|
|
|
|
if (executor instanceof ThreadPoolExecutor) {
|
|
|
|
ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor;
|
|
|
|
ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor;
|
|
|
|
int minThreads = tomcatExecutor.getCorePoolSize();
|
|
|
|
minThreads = tomcatExecutor.getCorePoolSize();
|
|
|
|
int maxThreads = tomcatExecutor.getMaximumPoolSize();
|
|
|
|
maxThreads = tomcatExecutor.getMaximumPoolSize();
|
|
|
|
long keepAliveTime = tomcatExecutor.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);
|
|
|
@ -109,19 +134,75 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public ThreadPoolRunStateInfo getWebRunStateInfo() {
|
|
|
|
public ThreadPoolRunStateInfo getWebRunStateInfo() {
|
|
|
|
return webThreadPoolRunStateHandler.getPoolRunState(null, executor);
|
|
|
|
if (executor instanceof ThreadPoolExecutor) {
|
|
|
|
|
|
|
|
return webThreadPoolRunStateHandler.getPoolRunState(null, executor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPoolRunStateInfo runStateInfo = new ThreadPoolRunStateInfo();
|
|
|
|
|
|
|
|
org.apache.tomcat.util.threads.ThreadPoolExecutor tomcatThreadPoolExecutor = (org.apache.tomcat.util.threads.ThreadPoolExecutor) executor;
|
|
|
|
|
|
|
|
// 核心线程数
|
|
|
|
|
|
|
|
int corePoolSize = tomcatThreadPoolExecutor.getCorePoolSize();
|
|
|
|
|
|
|
|
// 最大线程数
|
|
|
|
|
|
|
|
int maximumPoolSize = tomcatThreadPoolExecutor.getMaximumPoolSize();
|
|
|
|
|
|
|
|
// 线程池当前线程数 (有锁)
|
|
|
|
|
|
|
|
int poolSize = tomcatThreadPoolExecutor.getPoolSize();
|
|
|
|
|
|
|
|
// 活跃线程数 (有锁)
|
|
|
|
|
|
|
|
int activeCount = tomcatThreadPoolExecutor.getActiveCount();
|
|
|
|
|
|
|
|
// 同时进入池中的最大线程数 (有锁)
|
|
|
|
|
|
|
|
int largestPoolSize = tomcatThreadPoolExecutor.getLargestPoolSize();
|
|
|
|
|
|
|
|
// 线程池中执行任务总数量 (有锁)
|
|
|
|
|
|
|
|
long completedTaskCount = tomcatThreadPoolExecutor.getCompletedTaskCount();
|
|
|
|
|
|
|
|
// 当前负载
|
|
|
|
|
|
|
|
String currentLoad = CalculateUtil.divide(activeCount, maximumPoolSize) + "";
|
|
|
|
|
|
|
|
// 峰值负载
|
|
|
|
|
|
|
|
String peakLoad = CalculateUtil.divide(largestPoolSize, maximumPoolSize) + "";
|
|
|
|
|
|
|
|
BlockingQueue<Runnable> queue = tomcatThreadPoolExecutor.getQueue();
|
|
|
|
|
|
|
|
// 队列元素个数
|
|
|
|
|
|
|
|
int queueSize = queue.size();
|
|
|
|
|
|
|
|
// 队列类型
|
|
|
|
|
|
|
|
String queueType = queue.getClass().getSimpleName();
|
|
|
|
|
|
|
|
// 队列剩余容量
|
|
|
|
|
|
|
|
int remainingCapacity = queue.remainingCapacity();
|
|
|
|
|
|
|
|
// 队列容量
|
|
|
|
|
|
|
|
int queueCapacity = queueSize + remainingCapacity;
|
|
|
|
|
|
|
|
runStateInfo.setCoreSize(corePoolSize);
|
|
|
|
|
|
|
|
runStateInfo.setPoolSize(poolSize);
|
|
|
|
|
|
|
|
runStateInfo.setMaximumSize(maximumPoolSize);
|
|
|
|
|
|
|
|
runStateInfo.setActiveSize(activeCount);
|
|
|
|
|
|
|
|
runStateInfo.setCurrentLoad(currentLoad);
|
|
|
|
|
|
|
|
runStateInfo.setPeakLoad(peakLoad);
|
|
|
|
|
|
|
|
runStateInfo.setQueueType(queueType);
|
|
|
|
|
|
|
|
runStateInfo.setQueueSize(queueSize);
|
|
|
|
|
|
|
|
runStateInfo.setQueueCapacity(queueCapacity);
|
|
|
|
|
|
|
|
runStateInfo.setQueueRemainingCapacity(remainingCapacity);
|
|
|
|
|
|
|
|
runStateInfo.setLargestPoolSize(largestPoolSize);
|
|
|
|
|
|
|
|
runStateInfo.setCompletedTaskCount(completedTaskCount);
|
|
|
|
|
|
|
|
runStateInfo.setClientLastRefreshTime(DateUtil.formatDateTime(new Date()));
|
|
|
|
|
|
|
|
runStateInfo.setTimestamp(System.currentTimeMillis());
|
|
|
|
|
|
|
|
return webThreadPoolRunStateHandler.supplement(runStateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void updateWebThreadPool(ThreadPoolParameterInfo threadPoolParameterInfo) {
|
|
|
|
public void updateWebThreadPool(ThreadPoolParameterInfo threadPoolParameterInfo) {
|
|
|
|
|
|
|
|
int originalCoreSize, originalMaximumPoolSize;
|
|
|
|
|
|
|
|
long originalKeepAliveTime;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor;
|
|
|
|
if (executor instanceof ThreadPoolExecutor) {
|
|
|
|
int originalCoreSize = tomcatExecutor.getCorePoolSize();
|
|
|
|
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
|
|
|
|
int originalMaximumPoolSize = tomcatExecutor.getMaximumPoolSize();
|
|
|
|
originalCoreSize = threadPoolExecutor.getCorePoolSize();
|
|
|
|
long originalKeepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS);
|
|
|
|
originalMaximumPoolSize = threadPoolExecutor.getMaximumPoolSize();
|
|
|
|
tomcatExecutor.setCorePoolSize(threadPoolParameterInfo.corePoolSizeAdapt());
|
|
|
|
originalKeepAliveTime = threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
|
|
|
|
tomcatExecutor.setMaximumPoolSize(threadPoolParameterInfo.maximumPoolSizeAdapt());
|
|
|
|
threadPoolExecutor.setCorePoolSize(threadPoolParameterInfo.corePoolSizeAdapt());
|
|
|
|
tomcatExecutor.setKeepAliveTime(threadPoolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);
|
|
|
|
threadPoolExecutor.setMaximumPoolSize(threadPoolParameterInfo.maximumPoolSizeAdapt());
|
|
|
|
|
|
|
|
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()),
|
|
|
|