抽象 web 容器线程池组件. (#141)

pull/142/head
chen.ma 3 years ago
parent 69973a8554
commit e5a83a0cfb

@ -53,6 +53,27 @@
</exclusions>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>

@ -1,7 +1,9 @@
package cn.hippo4j.starter.handler.web;
package cn.hippo4j.common.web.executor;
import cn.hippo4j.common.config.ApplicationContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContext;
@ -15,7 +17,7 @@ import java.util.concurrent.Executor;
* @date 2022/1/19 21:20
*/
@Slf4j
public abstract class AbstractWebThreadPoolService implements WebThreadPoolService {
public abstract class AbstractWebThreadPoolService implements WebThreadPoolService, ApplicationRunner {
/**
* Thread pool executor.
@ -25,6 +27,7 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi
/**
* Get web thread pool by server.
*
* @param webServer
* @return
*/
protected abstract Executor getWebThreadPoolByServer(WebServer webServer);
@ -44,4 +47,9 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi
return executor;
}
@Override
public void run(ApplicationArguments args) {
getWebThreadPool();
}
}

@ -1,5 +1,6 @@
package cn.hippo4j.starter.handler.web;
package cn.hippo4j.common.web.executor;
import cn.hippo4j.common.model.PoolParameter;
import cn.hippo4j.common.model.PoolParameterInfo;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
@ -22,6 +23,25 @@ public class JettyWebThreadPoolHandler extends AbstractWebThreadPoolService {
return jettyWebServer.getServer().getThreadPool();
}
@Override
public PoolParameter getWebThreadPoolParameter() {
PoolParameterInfo parameterInfo = null;
try {
parameterInfo = new PoolParameterInfo();
QueuedThreadPool jettyExecutor = (QueuedThreadPool) executor;
int minThreads = jettyExecutor.getMinThreads();
int maxThreads = jettyExecutor.getMaxThreads();
parameterInfo.setCoreSize(minThreads);
parameterInfo.setMaxSize(maxThreads);
} catch (Exception ex) {
log.error("Failed to get the jetty thread pool parameter.", ex);
}
return parameterInfo;
}
@Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {

@ -1,5 +1,6 @@
package cn.hippo4j.starter.handler.web;
package cn.hippo4j.common.web.executor;
import cn.hippo4j.common.model.PoolParameter;
import cn.hippo4j.common.model.PoolParameterInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -44,6 +45,26 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
return tomcatExecutor;
}
@Override
public PoolParameter getWebThreadPoolParameter() {
PoolParameterInfo parameterInfo = null;
try {
parameterInfo = new PoolParameterInfo();
ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor;
int minThreads = tomcatExecutor.getCorePoolSize();
int maxThreads = tomcatExecutor.getMaximumPoolSize();
long keepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS);
parameterInfo.setCoreSize(minThreads);
parameterInfo.setMaxSize(maxThreads);
parameterInfo.setKeepAliveTime((int) keepAliveTime);
} catch (Exception ex) {
log.error("Failed to get the tomcat thread pool parameter.", ex);
}
return parameterInfo;
}
@Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {

@ -1,5 +1,6 @@
package cn.hippo4j.starter.handler.web;
package cn.hippo4j.common.web.executor;
import cn.hippo4j.common.model.PoolParameter;
import cn.hippo4j.common.model.PoolParameterInfo;
import io.undertow.Undertow;
import lombok.extern.slf4j.Slf4j;
@ -30,10 +31,31 @@ public class UndertowWebThreadPoolHandler extends AbstractWebThreadPoolService {
UndertowWebServer undertowWebServer = (UndertowWebServer) webServer;
Field undertowField = ReflectionUtils.findField(UndertowWebServer.class, UNDERTOW_NAME);
ReflectionUtils.makeAccessible(undertowField);
Undertow undertow = (Undertow) ReflectionUtils.getField(undertowField, undertowWebServer);
return Objects.isNull(undertow) ? null : undertow.getWorker();
}
@Override
public PoolParameter getWebThreadPoolParameter() {
PoolParameterInfo parameterInfo = null;
try {
parameterInfo = new PoolParameterInfo();
XnioWorker xnioWorker = (XnioWorker) executor;
int minThreads = xnioWorker.getOption(Options.WORKER_TASK_CORE_THREADS);
int maxThreads = xnioWorker.getOption(Options.WORKER_TASK_MAX_THREADS);
int keepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE);
parameterInfo.setCoreSize(minThreads);
parameterInfo.setMaxSize(maxThreads);
parameterInfo.setKeepAliveTime(keepAliveTime);
} catch (Exception ex) {
log.error("Failed to get the undertow thread pool parameter.", ex);
}
return parameterInfo;
}
@Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {

@ -1,4 +1,4 @@
package cn.hippo4j.starter.handler.web;
package cn.hippo4j.common.web.executor;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.web.exception.ServiceException;

@ -1,5 +1,6 @@
package cn.hippo4j.starter.handler.web;
package cn.hippo4j.common.web.executor;
import cn.hippo4j.common.model.PoolParameter;
import cn.hippo4j.common.model.PoolParameterInfo;
import java.util.concurrent.Executor;
@ -19,6 +20,13 @@ public interface WebThreadPoolService {
*/
Executor getWebThreadPool();
/**
* Get web thread pool parameter.
*
* @return
*/
PoolParameter getWebThreadPoolParameter();
/**
* Update web thread pool.
*

@ -0,0 +1,49 @@
package cn.hippo4j.core.config;
import cn.hippo4j.common.web.executor.JettyWebThreadPoolHandler;
import cn.hippo4j.common.web.executor.TomcatWebThreadPoolHandler;
import cn.hippo4j.common.web.executor.UndertowWebThreadPoolHandler;
import cn.hippo4j.common.web.executor.WebThreadPoolHandlerChoose;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Web thread pool configuration.
*
* @author chen.ma
* @date 2022/3/11 19:09
*/
@Configuration
public class WebThreadPoolConfiguration {
private static final String TOMCAT_SERVLET_WEB_SERVER_FACTORY = "tomcatServletWebServerFactory";
private static final String JETTY_SERVLET_WEB_SERVER_FACTORY = "JettyServletWebServerFactory";
private static final String UNDERTOW_SERVLET_WEB_SERVER_FACTORY = "undertowServletWebServerFactory";
@Bean
@ConditionalOnBean(name = TOMCAT_SERVLET_WEB_SERVER_FACTORY)
public TomcatWebThreadPoolHandler tomcatWebThreadPoolHandler() {
return new TomcatWebThreadPoolHandler();
}
@Bean
@ConditionalOnBean(name = JETTY_SERVLET_WEB_SERVER_FACTORY)
public JettyWebThreadPoolHandler jettyWebThreadPoolHandler() {
return new JettyWebThreadPoolHandler();
}
@Bean
@ConditionalOnBean(name = UNDERTOW_SERVLET_WEB_SERVER_FACTORY)
public UndertowWebThreadPoolHandler undertowWebThreadPoolHandler() {
return new UndertowWebThreadPoolHandler();
}
@Bean
public WebThreadPoolHandlerChoose webThreadPoolServiceChoose() {
return new WebThreadPoolHandlerChoose();
}
}

@ -25,20 +25,6 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
@ -76,6 +62,27 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>

@ -2,7 +2,9 @@ package cn.hippo4j.starter.config;
import cn.hippo4j.common.api.ThreadDetailState;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.web.executor.WebThreadPoolHandlerChoose;
import cn.hippo4j.core.config.UtilAutoConfiguration;
import cn.hippo4j.core.config.WebThreadPoolConfiguration;
import cn.hippo4j.core.enable.MarkerConfiguration;
import cn.hippo4j.core.handler.DynamicThreadPoolBannerHandler;
import cn.hippo4j.core.toolkit.IdentifyUtil;
@ -13,7 +15,7 @@ import cn.hippo4j.starter.core.*;
import cn.hippo4j.starter.event.ApplicationContentPostProcessor;
import cn.hippo4j.starter.handler.BaseThreadDetailStateHandler;
import cn.hippo4j.starter.handler.ThreadPoolRunStateHandler;
import cn.hippo4j.starter.handler.web.*;
import cn.hippo4j.starter.handler.web.WebThreadPoolRunStateHandler;
import cn.hippo4j.starter.monitor.ReportingEventExecutor;
import cn.hippo4j.starter.monitor.collect.RunTimeInfoCollector;
import cn.hippo4j.starter.monitor.send.HttpConnectSender;
@ -44,15 +46,9 @@ import org.springframework.core.env.ConfigurableEnvironment;
@ConditionalOnBean(MarkerConfiguration.Marker.class)
@EnableConfigurationProperties(BootstrapProperties.class)
@ConditionalOnProperty(prefix = BootstrapProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true")
@ImportAutoConfiguration({HttpClientConfiguration.class, DiscoveryConfiguration.class, MessageNotifyConfiguration.class, UtilAutoConfiguration.class})
@ImportAutoConfiguration({HttpClientConfiguration.class, DiscoveryConfiguration.class, MessageNotifyConfiguration.class, UtilAutoConfiguration.class, WebThreadPoolConfiguration.class})
public class DynamicThreadPoolAutoConfiguration {
private static final String TOMCAT_SERVLET_WEB_SERVER_FACTORY = "tomcatServletWebServerFactory";
private static final String JETTY_SERVLET_WEB_SERVER_FACTORY = "JettyServletWebServerFactory";
private static final String UNDERTOW_SERVLET_WEB_SERVER_FACTORY = "undertowServletWebServerFactory";
private final BootstrapProperties properties;
private final ConfigurableEnvironment environment;
@ -140,29 +136,6 @@ public class DynamicThreadPoolAutoConfiguration {
return new WebThreadPoolRunStateHandler();
}
@Bean
@ConditionalOnBean(name = TOMCAT_SERVLET_WEB_SERVER_FACTORY)
public TomcatWebThreadPoolHandler tomcatWebThreadPoolHandler() {
return new TomcatWebThreadPoolHandler();
}
@Bean
@ConditionalOnBean(name = JETTY_SERVLET_WEB_SERVER_FACTORY)
public JettyWebThreadPoolHandler jettyWebThreadPoolHandler() {
return new JettyWebThreadPoolHandler();
}
@Bean
@ConditionalOnBean(name = UNDERTOW_SERVLET_WEB_SERVER_FACTORY)
public UndertowWebThreadPoolHandler undertowWebThreadPoolHandler() {
return new UndertowWebThreadPoolHandler();
}
@Bean
public WebThreadPoolHandlerChoose webThreadPoolServiceChoose() {
return new WebThreadPoolHandlerChoose();
}
@Bean
public WebThreadPoolController webThreadPoolController(WebThreadPoolHandlerChoose webThreadPoolServiceChoose,
WebThreadPoolRunStateHandler webThreadPoolRunStateHandler) {

@ -5,9 +5,9 @@ import cn.hippo4j.common.model.PoolParameterInfo;
import cn.hippo4j.common.model.PoolRunStateInfo;
import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.common.web.base.Results;
import cn.hippo4j.starter.handler.web.WebThreadPoolHandlerChoose;
import cn.hippo4j.common.web.executor.WebThreadPoolHandlerChoose;
import cn.hippo4j.starter.handler.web.WebThreadPoolRunStateHandler;
import cn.hippo4j.starter.handler.web.WebThreadPoolService;
import cn.hippo4j.common.web.executor.WebThreadPoolService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

Loading…
Cancel
Save