Skip to content

Commit

Permalink
封装log
Browse files Browse the repository at this point in the history
  • Loading branch information
weihubeats committed Feb 28, 2022
1 parent d14a3ff commit 491cdac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.springframework.context.ApplicationListener;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Abstract web thread pool service.
Expand Down Expand Up @@ -49,4 +51,17 @@ public Executor getWebThreadPool() {
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())
);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,11 @@ protected Executor getWebThreadPoolByServer(WebServer webServer) {
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {
ThreadPoolExecutor jettyExecutor = (ThreadPoolExecutor) executor;
int originalCoreSize = jettyExecutor.getCorePoolSize();
int originalMaximumPoolSize = jettyExecutor.getMaximumPoolSize();
long originalKeepAliveTime = jettyExecutor.getKeepAliveTime(TimeUnit.SECONDS);

jettyExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
jettyExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize());
jettyExecutor.setKeepAliveTime(poolParameterInfo.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, jettyExecutor);
} 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 @@ -49,20 +49,12 @@ protected Executor getWebThreadPoolByServer(WebServer webServer) {
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {
ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor;
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()),
String.format("%s => %s", originalMaximumPoolSize, poolParameterInfo.getMaxSize()),
String.format("%s => %s", originalKeepAliveTime, poolParameterInfo.getKeepAliveTime())
);
super.log(poolParameterInfo, tomcatExecutor);

} catch (Exception ex) {
log.error("Failed to modify the Tomcat thread pool parameter.", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,11 @@ protected Executor getWebThreadPoolByServer(WebServer webServer) {
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {
ThreadPoolExecutor undertowExecutor = (ThreadPoolExecutor) executor;
int originalCoreSize = undertowExecutor.getCorePoolSize();
int originalMaximumPoolSize = undertowExecutor.getMaximumPoolSize();
long originalKeepAliveTime = undertowExecutor.getKeepAliveTime(TimeUnit.SECONDS);

undertowExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
undertowExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize());
undertowExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);
super.log(poolParameterInfo, undertowExecutor);

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())
);
} catch (Exception ex) {
log.error("Failed to modify the undertow thread pool parameter.", ex);
}
Expand Down

0 comments on commit 491cdac

Please sign in to comment.