From 0550dbc3a1791905623df7908a12c25fb6394e25 Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Mon, 20 Mar 2023 16:29:23 +0800 Subject: [PATCH 01/10] Add notification logic after modifying web thread pool parameters. --- .../DefaultJettyWebThreadPoolHandler.java | 10 ------- .../TomcatWebThreadPoolHandlerSupport.java | 2 +- .../DefaultUndertowWebThreadPoolHandler.java | 6 ----- .../UndertowWebThreadPoolHandlerSupport.java | 4 +-- .../common/model/ThreadPoolParameter.java | 2 +- .../common/model/ThreadPoolParameterInfo.java | 2 +- .../common/toolkit/ContentUtilTest.java | 2 +- .../hippo4j/common/toolkit/Md5UtilTest.java | 2 +- .../hippo4j/common/toolkit/SingletonTest.java | 2 +- .../hippo4j/config/model/ConfigInfoBase.java | 2 +- .../jetty/JettyWebThreadPoolHandler1x.java | 7 ----- .../DynamicThreadPoolAutoConfiguration.java | 5 ++-- .../config/WebThreadPoolProperties.java | 2 +- .../event/WebExecutorRefreshListener.java | 27 +++++++++++++++++++ .../WebThreadPoolHandlerConfiguration.java | 1 + 15 files changed, 41 insertions(+), 35 deletions(-) diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/jetty/DefaultJettyWebThreadPoolHandler.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/jetty/DefaultJettyWebThreadPoolHandler.java index 407221c99f3..c0cee096936 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/jetty/DefaultJettyWebThreadPoolHandler.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/jetty/DefaultJettyWebThreadPoolHandler.java @@ -18,20 +18,10 @@ package cn.hippo4j.adapter.web.jetty; import cn.hippo4j.adapter.web.DefaultAbstractWebThreadPoolService; -import cn.hippo4j.adapter.web.IWebThreadPoolHandlerSupport; -import cn.hippo4j.common.constant.ChangeThreadPoolConstants; -import cn.hippo4j.common.enums.WebContainerEnum; -import cn.hippo4j.common.model.ThreadPoolBaseInfo; -import cn.hippo4j.common.model.ThreadPoolParameter; -import cn.hippo4j.common.model.ThreadPoolParameterInfo; -import cn.hippo4j.common.model.ThreadPoolRunStateInfo; -import cn.hippo4j.common.toolkit.ReflectUtil; import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.springframework.boot.web.embedded.jetty.JettyWebServer; import org.springframework.boot.web.server.WebServer; -import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executor; /** diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/tomcat/TomcatWebThreadPoolHandlerSupport.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/tomcat/TomcatWebThreadPoolHandlerSupport.java index 5c8b359c87e..446a4b4385f 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/tomcat/TomcatWebThreadPoolHandlerSupport.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/tomcat/TomcatWebThreadPoolHandlerSupport.java @@ -91,7 +91,7 @@ public ThreadPoolParameter getWebThreadPoolParameter() { long keepAliveTime = tomcatThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS); parameterInfo.setCoreSize(minThreads); parameterInfo.setMaxSize(maxThreads); - parameterInfo.setKeepAliveTime((int) keepAliveTime); + parameterInfo.setKeepAliveTime(keepAliveTime); } catch (Exception ex) { log.error("Failed to get the tomcat thread pool parameter.", ex); } diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/DefaultUndertowWebThreadPoolHandler.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/DefaultUndertowWebThreadPoolHandler.java index 5d874549fe2..5b2f81aedce 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/DefaultUndertowWebThreadPoolHandler.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/DefaultUndertowWebThreadPoolHandler.java @@ -22,12 +22,6 @@ import java.util.concurrent.Executor; import cn.hippo4j.adapter.web.DefaultAbstractWebThreadPoolService; -import cn.hippo4j.adapter.web.IWebThreadPoolHandlerSupport; -import cn.hippo4j.common.enums.WebContainerEnum; -import cn.hippo4j.common.model.ThreadPoolBaseInfo; -import cn.hippo4j.common.model.ThreadPoolParameter; -import cn.hippo4j.common.model.ThreadPoolParameterInfo; -import cn.hippo4j.common.model.ThreadPoolRunStateInfo; import io.undertow.Undertow; import lombok.extern.slf4j.Slf4j; diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/UndertowWebThreadPoolHandlerSupport.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/UndertowWebThreadPoolHandlerSupport.java index e37abe0fa34..6917a26a746 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/UndertowWebThreadPoolHandlerSupport.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/UndertowWebThreadPoolHandlerSupport.java @@ -82,7 +82,7 @@ public ThreadPoolParameter getWebThreadPoolParameter() { 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); + long keepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE); parameterInfo.setCoreSize(minThreads); parameterInfo.setMaxSize(maxThreads); parameterInfo.setKeepAliveTime(keepAliveTime); @@ -135,7 +135,7 @@ public void updateWebThreadPool(ThreadPoolParameterInfo threadPoolParameterInfo) XnioWorker xnioWorker = (XnioWorker) executor; Integer coreSize = threadPoolParameterInfo.corePoolSizeAdapt(); Integer maxSize = threadPoolParameterInfo.maximumPoolSizeAdapt(); - Integer keepAliveTime = threadPoolParameterInfo.getKeepAliveTime(); + int keepAliveTime = threadPoolParameterInfo.getKeepAliveTime().intValue(); 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); diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameter.java b/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameter.java index be74b4786fe..26101088c5f 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameter.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameter.java @@ -76,7 +76,7 @@ public interface ThreadPoolParameter { * * @return */ - Integer getKeepAliveTime(); + Long getKeepAliveTime(); /** * Get execute time out diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameterInfo.java b/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameterInfo.java index 48bf529d63b..1a912c2cd43 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameterInfo.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameterInfo.java @@ -94,7 +94,7 @@ public class ThreadPoolParameterInfo implements ThreadPoolParameter, Serializabl /** * Keep alive time */ - private Integer keepAliveTime; + private Long keepAliveTime; /** * Execute time out diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java index a8cd74c9f24..02baa272172 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java @@ -29,7 +29,7 @@ public void assertGetPoolContent() { ":1,\"capacityAlarm\":80,\"livenessAlarm\":80,\"allowCoreThreadTimeOut\":1}"; ThreadPoolParameterInfo threadPoolParameterInfo = ThreadPoolParameterInfo.builder().tenantId("prescription") .itemId("dynamic-threadpool-example").tpId("message-consume").content("描述信息").corePoolSize(1) - .maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513).executeTimeOut(null).rejectedType(4) + .maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513L).executeTimeOut(null).rejectedType(4) .isAlarm(1).capacityAlarm(80).livenessAlarm(80).allowCoreThreadTimeOut(1).build(); Assert.isTrue(testText.equals(ContentUtil.getPoolContent(threadPoolParameterInfo))); } diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/Md5UtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/Md5UtilTest.java index 0c9191b13f3..adf128edab2 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/Md5UtilTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/Md5UtilTest.java @@ -53,7 +53,7 @@ public void assetGetTpContentMd5() { String md5Result = "ef5ea7cb47377fb9fb85a7125e76715d"; ThreadPoolParameterInfo threadPoolParameterInfo = ThreadPoolParameterInfo.builder().tenantId("prescription") .itemId("dynamic-threadpool-example").tpId("message-consume").content("描述信息").corePoolSize(1) - .maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513).executeTimeOut(null).rejectedType(4) + .maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513L).executeTimeOut(null).rejectedType(4) .isAlarm(1).capacityAlarm(80).livenessAlarm(80).allowCoreThreadTimeOut(1).build(); Assert.isTrue(md5Result.equals(Md5Util.getTpContentMd5(threadPoolParameterInfo))); } diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/SingletonTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/SingletonTest.java index 1f111e82807..2e3c49c7fed 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/SingletonTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/SingletonTest.java @@ -30,7 +30,7 @@ public void assertSingletonGet() { Assert.assertEquals("hippo4j", Singleton.get("userName")); ThreadPoolParameterInfo threadPoolParameterInfo = ThreadPoolParameterInfo.builder().tenantId("prescription") .itemId("dynamic-threadpool-example").tpId("message-consume").content("描述信息").corePoolSize(1) - .maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513).executeTimeOut(null).rejectedType(4) + .maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513L).executeTimeOut(null).rejectedType(4) .isAlarm(1).capacityAlarm(80).livenessAlarm(80).allowCoreThreadTimeOut(1).build(); Singleton.put(threadPoolParameterInfo); Assert.assertEquals(threadPoolParameterInfo, Singleton.get(ThreadPoolParameterInfo.class.getName())); diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigInfoBase.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigInfoBase.java index fe7fbcc2f18..08612eddc83 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigInfoBase.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigInfoBase.java @@ -81,7 +81,7 @@ public class ConfigInfoBase implements Serializable { /** * keepAliveTime */ - private Integer keepAliveTime; + private Long keepAliveTime; /** * executeTimeOut diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/jetty/JettyWebThreadPoolHandler1x.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/jetty/JettyWebThreadPoolHandler1x.java index be7bac20edf..4f300e2b14d 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/jetty/JettyWebThreadPoolHandler1x.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/jetty/JettyWebThreadPoolHandler1x.java @@ -17,14 +17,7 @@ package cn.hippo4j.config.springboot1x.starter.web.jetty; -import cn.hippo4j.adapter.web.IWebThreadPoolHandlerSupport; import cn.hippo4j.adapter.web.jetty.JettyWebThreadPoolHandlerSupport; -import cn.hippo4j.common.enums.WebContainerEnum; -import cn.hippo4j.common.model.ThreadPoolBaseInfo; -import cn.hippo4j.common.model.ThreadPoolParameter; -import cn.hippo4j.common.model.ThreadPoolParameterInfo; -import cn.hippo4j.common.model.ThreadPoolRunStateInfo; -import cn.hippo4j.common.web.exception.ServiceException; import cn.hippo4j.config.springboot1x.starter.web.AbstractWebThreadPoolService1x; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer; diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index 5b76bc9f568..7e47c419376 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -127,8 +127,9 @@ public PlatformsRefreshListener hippo4jPlatformsListener() { } @Bean - public WebExecutorRefreshListener hippo4jWebExecutorListener() { - return new WebExecutorRefreshListener(); + @SuppressWarnings("all") + public WebExecutorRefreshListener hippo4jWebExecutorListener(ThreadPoolConfigChange threadPoolConfigChange) { + return new WebExecutorRefreshListener(threadPoolConfigChange); } @Bean diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java index f2d1ff024d0..c4154d657f7 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java @@ -38,7 +38,7 @@ public class WebThreadPoolProperties { /** * Keep alive time */ - private Integer keepAliveTime; + private Long keepAliveTime; /** * Nodes, application startup is not affect, change properties is effect diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java index a7f9e60de75..e741cfc50ea 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java @@ -19,11 +19,14 @@ import cn.hippo4j.adapter.web.WebThreadPoolHandlerChoose; import cn.hippo4j.adapter.web.WebThreadPoolService; +import cn.hippo4j.common.api.NotifyRequest; +import cn.hippo4j.common.api.ThreadPoolConfigChange; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.model.ThreadPoolParameter; import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; import cn.hippo4j.config.springboot.starter.config.WebThreadPoolProperties; +import cn.hippo4j.message.request.ChangeParameterNotifyRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.Order; @@ -37,8 +40,15 @@ */ @Slf4j @Order(WEB_EXECUTOR_LISTENER) +@SuppressWarnings("all") public class WebExecutorRefreshListener extends AbstractRefreshListener { + private final ThreadPoolConfigChange configChange; + + public WebExecutorRefreshListener(ThreadPoolConfigChange configChange) { + this.configChange = configChange; + } + @Override public String getNodes(WebThreadPoolProperties properties) { return properties.getNodes(); @@ -71,6 +81,7 @@ public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynami || !Objects.equals(beforeParameter.getMaxSize(), nowParameter.getMaxSize()) || !Objects.equals(beforeParameter.getKeepAliveTime(), nowParameter.getKeepAliveTime())) { webThreadPoolService.updateWebThreadPool(nowParameter); + configChange.sendPoolConfigChange(buildChangeRequest(beforeParameter, nowParameter)); } } } catch (Exception ex) { @@ -78,6 +89,22 @@ public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynami } } + /** + * Constructing a request for thread pool parameter change notification + * @param before + * @param now + * @return + */ + private ChangeParameterNotifyRequest buildChangeRequest(ThreadPoolParameter before, ThreadPoolParameter now) { + return ChangeParameterNotifyRequest.builder() + .beforeCorePoolSize(before.getCoreSize()) + .nowCorePoolSize(now.getCoreSize()) + .beforeMaximumPoolSize(before.getMaxSize()) + .nowMaximumPoolSize(now.getMaxSize()) + .beforeKeepAliveTime(before.getKeepAliveTime()) + .nowKeepAliveTime(now.getKeepAliveTime()).build(); + } + private ThreadPoolParameterInfo buildWebPoolParameter(BootstrapConfigProperties bindableCoreProperties) { ThreadPoolParameterInfo threadPoolParameterInfo = null; WebThreadPoolProperties webThreadPoolProperties = bindableCoreProperties.getWeb(); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter-adapter/hippo4j-spring-boot-starter-adapter-web/src/main/java/cn/hippo4j/springboot/starter/adapter/web/WebThreadPoolHandlerConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter-adapter/hippo4j-spring-boot-starter-adapter-web/src/main/java/cn/hippo4j/springboot/starter/adapter/web/WebThreadPoolHandlerConfiguration.java index 2027668b208..a4f033ed48c 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter-adapter/hippo4j-spring-boot-starter-adapter-web/src/main/java/cn/hippo4j/springboot/starter/adapter/web/WebThreadPoolHandlerConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter-adapter/hippo4j-spring-boot-starter-adapter-web/src/main/java/cn/hippo4j/springboot/starter/adapter/web/WebThreadPoolHandlerConfiguration.java @@ -24,6 +24,7 @@ import cn.hippo4j.adapter.web.undertow.DefaultUndertowWebThreadPoolHandler; import cn.hippo4j.adapter.web.WebThreadPoolRunStateHandler; import cn.hippo4j.adapter.web.undertow.UndertowWebThreadPoolHandlerAdapt; +import cn.hippo4j.common.api.ThreadPoolConfigChange; import io.undertow.Undertow; import org.apache.catalina.startup.Tomcat; import org.apache.coyote.UpgradeProtocol; From 382ff3a0ec2a8c3113f0255485cf7e11c41389b2 Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Tue, 21 Mar 2023 11:07:29 +0800 Subject: [PATCH 02/10] Refactor the notification module to enable notification capability for the web container. --- .../web/AbstractWebThreadPoolService.java | 7 +++ .../DefaultAbstractWebThreadPoolService.java | 20 +++++++ .../adapter/web/WebThreadPoolService.java | 5 ++ .../platform/LarkSendMessageHandler.java | 2 +- .../base/AbstractRobotSendMessageHandler.java | 2 +- .../WebChangeParameterNotifyRequest.java | 53 ++++++++++++++++++ .../request/base/BaseNotifyRequest.java | 1 + .../Hippo4jBaseSendMessageService.java | 52 +++++++++++++---- .../service/Hippo4jSendMessageService.java | 8 +++ .../message/service/SendMessageHandler.java | 19 ++++++- .../WebThreadPoolConfigChangeHandler.java | 56 +++++++++++++++++++ .../web/AbstractWebThreadPoolService1x.java | 20 +++++++ .../config/BootstrapConfigProperties.java | 3 +- .../DynamicThreadPoolAutoConfiguration.java | 23 ++++---- .../starter/config/ExecutorProperties.java | 2 +- .../starter/config/IExecutorProperties.java | 44 +++++++++++++++ ...erties.java => WebExecutorProperties.java} | 9 ++- .../notify/ConfigModeNotifyConfigBuilder.java | 55 ++++++++++++------ .../DynamicThreadPoolRefreshListener.java | 4 +- .../event/PlatformsRefreshListener.java | 2 +- .../event/WebExecutorRefreshListener.java | 26 +++++---- 21 files changed, 353 insertions(+), 60 deletions(-) create mode 100644 hippo4j-message/src/main/java/cn/hippo4j/message/request/WebChangeParameterNotifyRequest.java create mode 100644 hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java create mode 100644 hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java rename hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/{WebThreadPoolProperties.java => WebExecutorProperties.java} (86%) diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java index 910ff0f326b..d8cfbe7e2c5 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java @@ -22,15 +22,22 @@ import cn.hippo4j.common.model.ThreadPoolParameter; import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; +import io.undertow.Undertow; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.util.ReflectionUtils; + +import java.lang.reflect.Field; import java.util.concurrent.Executor; /** * Abstract web thread pool service. */ @Slf4j +@Order(Ordered.HIGHEST_PRECEDENCE) public abstract class AbstractWebThreadPoolService implements WebThreadPoolService, ApplicationRunner { private final IWebThreadPoolHandlerSupport support; diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/DefaultAbstractWebThreadPoolService.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/DefaultAbstractWebThreadPoolService.java index ebb98f0eaad..6f14b28a4aa 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/DefaultAbstractWebThreadPoolService.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/DefaultAbstractWebThreadPoolService.java @@ -18,22 +18,29 @@ package cn.hippo4j.adapter.web; import cn.hippo4j.common.config.ApplicationContextHolder; +import io.undertow.Undertow; +import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.context.WebServerApplicationContext; import org.springframework.boot.web.server.WebServer; import org.springframework.context.ApplicationContext; +import org.springframework.util.ReflectionUtils; +import java.lang.reflect.Field; import java.util.concurrent.Executor; /** * Default WebThreadPoolService abstract class, * reuses common capabilities for web container operations. */ +@Slf4j public abstract class DefaultAbstractWebThreadPoolService extends AbstractWebThreadPoolService { public DefaultAbstractWebThreadPoolService(IWebThreadPoolHandlerSupport support) { super(support); } + private static final String STARTED_FIELD_NAME = "started"; + /** * Get the internal abstract method of the web container thread pool, * to be implemented by subclasses. @@ -53,6 +60,19 @@ public Integer getPort() { return getWebServer().getPort(); } + @Override + public boolean isContainerStarted() { + try { + WebServer container = getWebServer(); + Field field = ReflectionUtils.findField(WebServer.class, STARTED_FIELD_NAME); + ReflectionUtils.makeAccessible(field); + return (boolean) ReflectionUtils.getField(field, container); + } catch (Throwable th) { + log.error("Failed to get isStarted flag.", th); + return false; + } + } + /** * Get the thread pool object of the current web container based on the WebServer. * @param webServer current Web-Server. diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java index 082c890f350..f8cfccb80b1 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java @@ -30,6 +30,11 @@ */ public interface WebThreadPoolService { + /** + * Determine if the web container has started. + */ + boolean isContainerStarted(); + /** * Get web thread pool. * diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java index 991862222cd..ab32e0c3c49 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java @@ -44,7 +44,7 @@ */ @Slf4j @RequiredArgsConstructor -public class LarkSendMessageHandler implements SendMessageHandler { +public class LarkSendMessageHandler implements SendMessageHandler { @Override public String getType() { diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java index f8acca947cb..aefe572c093 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java @@ -32,7 +32,7 @@ /** * Abstract robot send message handler. */ -public abstract class AbstractRobotSendMessageHandler implements SendMessageHandler { +public abstract class AbstractRobotSendMessageHandler implements SendMessageHandler { /** * Build message actual content. diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/request/WebChangeParameterNotifyRequest.java b/hippo4j-message/src/main/java/cn/hippo4j/message/request/WebChangeParameterNotifyRequest.java new file mode 100644 index 00000000000..25497484d9e --- /dev/null +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/request/WebChangeParameterNotifyRequest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.message.request; + +import cn.hippo4j.message.request.base.BaseNotifyRequest; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Change parameter notify request for web thread pool. + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class WebChangeParameterNotifyRequest extends BaseNotifyRequest { + + private String active; + + private String appName; + + private String identify; + + private Integer beforeCorePoolSize; + + private Integer nowCorePoolSize; + + private Integer beforeMaximumPoolSize; + + private Integer nowMaximumPoolSize; + + private Long beforeKeepAliveTime; + + private Long nowKeepAliveTime; + +} diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/request/base/BaseNotifyRequest.java b/hippo4j-message/src/main/java/cn/hippo4j/message/request/base/BaseNotifyRequest.java index 5db9a63cb8c..4e0f6a7bc78 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/request/base/BaseNotifyRequest.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/request/base/BaseNotifyRequest.java @@ -18,6 +18,7 @@ package cn.hippo4j.message.request.base; import cn.hippo4j.common.api.NotifyRequest; +import cn.hippo4j.message.enums.NotifyTypeEnum; import lombok.Data; /** diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java b/hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java index 2e3a9691153..10b131ce9ef 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java @@ -25,6 +25,7 @@ import cn.hippo4j.message.enums.NotifyTypeEnum; import cn.hippo4j.message.request.AlarmNotifyRequest; import cn.hippo4j.message.request.ChangeParameterNotifyRequest; +import cn.hippo4j.message.request.WebChangeParameterNotifyRequest; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -53,11 +54,7 @@ public class Hippo4jBaseSendMessageService implements Hippo4jSendMessageService, @Override public void sendAlarmMessage(NotifyTypeEnum typeEnum, AlarmNotifyRequest alarmNotifyRequest) { String threadPoolId = alarmNotifyRequest.getThreadPoolId(); - String buildKey = new StringBuilder() - .append(threadPoolId) - .append("+") - .append("ALARM") - .toString(); + String buildKey = generateAlarmKey(threadPoolId); List notifyList = notifyConfigs.get(buildKey); if (CollectionUtil.isEmpty(notifyList)) { return; @@ -82,11 +79,7 @@ public void sendAlarmMessage(NotifyTypeEnum typeEnum, AlarmNotifyRequest alarmNo @Override public void sendChangeMessage(ChangeParameterNotifyRequest changeParameterNotifyRequest) { String threadPoolId = changeParameterNotifyRequest.getThreadPoolId(); - String buildKey = new StringBuilder() - .append(threadPoolId) - .append("+") - .append("CONFIG") - .toString(); + String buildKey = generateConfigKey(threadPoolId); List notifyList = notifyConfigs.get(buildKey); if (CollectionUtil.isEmpty(notifyList)) { log.warn("[{}] Please configure alarm notification on the server.", threadPoolId); @@ -106,6 +99,45 @@ public void sendChangeMessage(ChangeParameterNotifyRequest changeParameterNotify }); } + @Override + public void sendChangeMessage(WebChangeParameterNotifyRequest webChangeParameterNotifyRequest) { + String threadPoolId = webChangeParameterNotifyRequest.getThreadPoolId(); + String buildKey = generateConfigKey(threadPoolId); + List notifyList = notifyConfigs.get(buildKey); + if (CollectionUtil.isEmpty(notifyList)) { + log.warn("[{}] Please configure alarm notification on the server.", threadPoolId); + return; + } + notifyList.forEach(each -> { + try { + SendMessageHandler messageHandler = sendMessageHandlers.get(each.getPlatform()); + if (messageHandler == null) { + log.warn("[{}] Please configure alarm notification on the server.", threadPoolId); + return; + } + messageHandler.sendWebChangeMessage(each, webChangeParameterNotifyRequest); + } catch (Exception ex) { + log.warn("Failed to send thread pool change notification. key: [{}]", threadPoolId, ex); + } + }); + } + + private String generateConfigKey(String threadPoolId) { + return new StringBuilder() + .append(threadPoolId) + .append("+") + .append("CONFIG") + .toString(); + } + + private String generateAlarmKey(String threadPoolId) { + return new StringBuilder() + .append(threadPoolId) + .append("+") + .append("ALARM") + .toString(); + } + /** * Is send alarm. * diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java b/hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java index 52a63ecc49d..1878cbc77cd 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java @@ -20,6 +20,7 @@ import cn.hippo4j.message.enums.NotifyTypeEnum; import cn.hippo4j.message.request.AlarmNotifyRequest; import cn.hippo4j.message.request.ChangeParameterNotifyRequest; +import cn.hippo4j.message.request.WebChangeParameterNotifyRequest; /** * Hippo-4j send message service. @@ -40,4 +41,11 @@ public interface Hippo4jSendMessageService { * @param changeParameterNotifyRequest change parameter notify request */ void sendChangeMessage(ChangeParameterNotifyRequest changeParameterNotifyRequest); + + /** + * Send web thread pool parameter change notification. + * + * @param webChangeParameterNotifyRequest change parameter notify request + */ + void sendChangeMessage(WebChangeParameterNotifyRequest webChangeParameterNotifyRequest); } diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/service/SendMessageHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/service/SendMessageHandler.java index 37e939bf7d7..25d6f27d50b 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/service/SendMessageHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/service/SendMessageHandler.java @@ -19,11 +19,14 @@ import cn.hippo4j.message.dto.NotifyConfigDTO; import cn.hippo4j.common.api.NotifyRequest; +import cn.hippo4j.message.request.AlarmNotifyRequest; +import cn.hippo4j.message.request.ChangeParameterNotifyRequest; +import cn.hippo4j.message.request.WebChangeParameterNotifyRequest; /** * Send message handler. */ -public interface SendMessageHandler { +public interface SendMessageHandler { /** * Get the message send type. @@ -38,7 +41,7 @@ public interface SendMessageHandler { + + @Value("${spring.application.name:UNKNOWN}") + private String applicationName; + + private final Hippo4jSendMessageService hippo4jSendMessageService; + + /** + * Send pool config change. + * + * @param requestParam change parameter notify request + */ + @Override + public void sendPoolConfigChange(WebChangeParameterNotifyRequest requestParam) { + try { + requestParam.setAppName(applicationName); + requestParam.setIdentify(IdentifyUtil.getIdentify()); + hippo4jSendMessageService.sendChangeMessage(requestParam); + } catch (Throwable th) { + log.error("send web thread pool config change message failed.", th); + } + } +} diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/AbstractWebThreadPoolService1x.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/AbstractWebThreadPoolService1x.java index 333a4d07f02..d3d5c8ab75e 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/AbstractWebThreadPoolService1x.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/AbstractWebThreadPoolService1x.java @@ -20,18 +20,25 @@ import cn.hippo4j.adapter.web.AbstractWebThreadPoolService; import cn.hippo4j.adapter.web.IWebThreadPoolHandlerSupport; import cn.hippo4j.common.config.ApplicationContextHolder; +import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; +import org.springframework.util.ReflectionUtils; + +import java.lang.reflect.Field; /** * Abstract class for adapting WebThreadPoolService to Spring 1.x version. */ +@Slf4j public abstract class AbstractWebThreadPoolService1x extends AbstractWebThreadPoolService { public AbstractWebThreadPoolService1x(IWebThreadPoolHandlerSupport support) { super(support); } + private static final String STARTED_FIELD_NAME = "started"; + /** * Get the embedded Servlet container from the Spring application context. */ @@ -46,4 +53,17 @@ protected EmbeddedServletContainer getContainer() { public Integer getPort() { return getContainer().getPort(); } + + @Override + public boolean isContainerStarted() { + try { + EmbeddedServletContainer container = getContainer(); + Field field = ReflectionUtils.findField(EmbeddedServletContainer.class, STARTED_FIELD_NAME); + ReflectionUtils.makeAccessible(field); + return (boolean) ReflectionUtils.getField(field, container); + } catch (Throwable th) { + log.error("Failed to get isStarted flag.", th); + return false; + } + } } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java index a5a435d21be..4d4ee17c85f 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java @@ -78,8 +78,9 @@ public class BootstrapConfigProperties implements BootstrapPropertiesInterface { /** * web config + * @since 1.5.0 */ - private WebThreadPoolProperties web; + private WebExecutorProperties web; /** * Notify platforms. diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index 7e47c419376..de2f7313473 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -17,6 +17,7 @@ package cn.hippo4j.config.springboot.starter.config; +import cn.hippo4j.adapter.web.WebThreadPoolService; import cn.hippo4j.common.api.ThreadPoolCheckAlarm; import cn.hippo4j.common.api.ThreadPoolConfigChange; import cn.hippo4j.common.config.ApplicationContextHolder; @@ -34,11 +35,7 @@ import cn.hippo4j.core.handler.DynamicThreadPoolBannerHandler; import cn.hippo4j.message.api.NotifyConfigBuilder; import cn.hippo4j.message.config.MessageConfiguration; -import cn.hippo4j.message.service.AlarmControlHandler; -import cn.hippo4j.message.service.DefaultThreadPoolCheckAlarmHandler; -import cn.hippo4j.message.service.DefaultThreadPoolConfigChangeHandler; -import cn.hippo4j.message.service.Hippo4jBaseSendMessageService; -import cn.hippo4j.message.service.Hippo4jSendMessageService; +import cn.hippo4j.message.service.*; import cn.hippo4j.springboot.starter.adapter.web.WebAdapterConfiguration; import lombok.AllArgsConstructor; import org.springframework.beans.factory.ObjectProvider; @@ -82,8 +79,8 @@ public ApplicationContextHolder hippo4JApplicationContextHolder() { } @Bean - public NotifyConfigBuilder notifyConfigBuilder(AlarmControlHandler alarmControlHandler) { - return new ConfigModeNotifyConfigBuilder(alarmControlHandler, bootstrapConfigProperties); + public NotifyConfigBuilder notifyConfigBuilder(AlarmControlHandler alarmControlHandler, WebThreadPoolService webThreadPoolService) { + return new ConfigModeNotifyConfigBuilder(alarmControlHandler, bootstrapConfigProperties, webThreadPoolService); } @Bean @@ -94,10 +91,16 @@ public ThreadPoolCheckAlarm defaultThreadPoolCheckAlarmHandler(Hippo4jSendMessag @Bean @ConditionalOnMissingBean - public ThreadPoolConfigChange defaultThreadPoolConfigChangeHandler(Hippo4jSendMessageService hippo4jSendMessageService) { + public DefaultThreadPoolConfigChangeHandler defaultThreadPoolConfigChangeHandler(Hippo4jSendMessageService hippo4jSendMessageService) { return new DefaultThreadPoolConfigChangeHandler(hippo4jSendMessageService); } + @Bean + @ConditionalOnMissingBean + public WebThreadPoolConfigChangeHandler webThreadPoolConfigChangeHandler(Hippo4jSendMessageService hippo4jSendMessageService) { + return new WebThreadPoolConfigChangeHandler(hippo4jSendMessageService); + } + @Bean public DynamicThreadPoolPostProcessor dynamicThreadPoolPostProcessor(ApplicationContextHolder hippo4JApplicationContextHolder) { return new DynamicThreadPoolPostProcessor(bootstrapConfigProperties); @@ -110,7 +113,7 @@ public ThreadPoolMonitorExecutor hippo4jDynamicThreadPoolMonitorExecutor() { @Bean @SuppressWarnings("all") - public DynamicThreadPoolRefreshListener hippo4jExecutorsListener(ThreadPoolConfigChange threadPoolConfigChange, + public DynamicThreadPoolRefreshListener hippo4jExecutorsListener(DefaultThreadPoolConfigChangeHandler threadPoolConfigChange, ConfigModeNotifyConfigBuilder configModeNotifyConfigBuilder, Hippo4jBaseSendMessageService hippoBaseSendMessageService) { return new DynamicThreadPoolRefreshListener(threadPoolConfigChange, configModeNotifyConfigBuilder, hippoBaseSendMessageService); @@ -128,7 +131,7 @@ public PlatformsRefreshListener hippo4jPlatformsListener() { @Bean @SuppressWarnings("all") - public WebExecutorRefreshListener hippo4jWebExecutorListener(ThreadPoolConfigChange threadPoolConfigChange) { + public WebExecutorRefreshListener hippo4jWebExecutorListener(WebThreadPoolConfigChangeHandler threadPoolConfigChange) { return new WebExecutorRefreshListener(threadPoolConfigChange); } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java index 58754275188..ec65971081f 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java @@ -31,7 +31,7 @@ @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) -public class ExecutorProperties { +public class ExecutorProperties implements IExecutorProperties { /** * Thread pool id diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java new file mode 100644 index 00000000000..d13fbee4679 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.config.springboot.starter.config; + +/** + * Interface for thread pool configuration. + */ +public interface IExecutorProperties { + + /** + * Core pool size + */ + Integer getCorePoolSize(); + + /** + * Maximum pool size + */ + Integer getMaximumPoolSize(); + + /** + * Keep alive time + */ + Long getKeepAliveTime(); + + /** + * Notify configs + */ + DynamicThreadPoolNotifyProperties getNotify(); +} diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java similarity index 86% rename from hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java rename to hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java index c4154d657f7..918503a9a1d 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java @@ -20,10 +20,10 @@ import lombok.Data; /** - * Web thread pool properties. + * Web thread pool executor properties. */ @Data -public class WebThreadPoolProperties { +public class WebExecutorProperties implements IExecutorProperties { /** * Core pool size @@ -49,4 +49,9 @@ public class WebThreadPoolProperties { * these propertied is enabled? */ private Boolean enable = true; + + /** + * Notify config + */ + private DynamicThreadPoolNotifyProperties notify; } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java index 7a01ae4e166..3531e715046 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java @@ -17,11 +17,10 @@ package cn.hippo4j.config.springboot.starter.notify; +import cn.hippo4j.adapter.web.WebThreadPoolService; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; -import cn.hippo4j.config.springboot.starter.config.ExecutorProperties; -import cn.hippo4j.config.springboot.starter.config.NotifyPlatformProperties; +import cn.hippo4j.config.springboot.starter.config.*; import cn.hippo4j.message.api.NotifyConfigBuilder; import cn.hippo4j.message.dto.NotifyConfigDTO; import cn.hippo4j.message.service.AlarmControlHandler; @@ -42,24 +41,39 @@ public class ConfigModeNotifyConfigBuilder implements NotifyConfigBuilder { private final BootstrapConfigProperties configProperties; + private final WebThreadPoolService webThreadPoolService; + @Override public Map> buildNotify() { Map> resultMap = new HashMap<>(); - boolean globalAlarm = Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getAlarm()).orElse(true); + boolean globalAlarm = Optional.ofNullable(configProperties.getDefaultExecutor()) + .map(ExecutorProperties::getAlarm) + .orElse(true); List executors = configProperties.getExecutors(); if (CollectionUtil.isEmpty(executors)) { log.warn("Failed to build notify, executors configuration is empty."); return resultMap; } - List actual = executors.stream().filter(each -> Optional.ofNullable(each.getAlarm()).orElse(false)).collect(Collectors.toList()); + List actual = executors.stream() + .filter(each -> Optional.ofNullable(each.getAlarm()) + .orElse(false)) + .collect(Collectors.toList()); if (!globalAlarm && CollectionUtil.isEmpty(actual)) { return resultMap; } for (ExecutorProperties executorProperties : executors) { - Map> buildSingleNotifyConfig = buildSingleNotifyConfig(executorProperties); + Map> buildSingleNotifyConfig = + buildSingleNotifyConfig(executorProperties.getThreadPoolId(), executorProperties); initCacheAndLock(buildSingleNotifyConfig); resultMap.putAll(buildSingleNotifyConfig); } + // register notify config for web + WebExecutorProperties webProperties = configProperties.getWeb(); + Map> webSingleNotifyConfigMap = + buildSingleNotifyConfig(webThreadPoolService.getWebContainerType().name(), webProperties); + initCacheAndLock(webSingleNotifyConfigMap); + resultMap.putAll(webSingleNotifyConfigMap); + return resultMap; } @@ -69,9 +83,8 @@ public Map> buildNotify() { * @param executorProperties * @return */ - public Map> buildSingleNotifyConfig(ExecutorProperties executorProperties) { + public Map> buildSingleNotifyConfig(String threadPoolId, IExecutorProperties executorProperties) { Map> resultMap = new HashMap<>(); - String threadPoolId = executorProperties.getThreadPoolId(); String alarmBuildKey = threadPoolId + "+ALARM"; List alarmNotifyConfigs = new ArrayList<>(); List notifyPlatforms = configProperties.getNotifyPlatforms(); @@ -82,10 +95,7 @@ public Map> buildSingleNotifyConfig(ExecutorProper notifyConfig.setType("ALARM"); notifyConfig.setSecret(platformProperties.getSecret()); notifyConfig.setSecretKey(getToken(platformProperties)); - int interval = Optional.ofNullable(executorProperties.getNotify()) - .map(each -> each.getInterval()) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getNotify()).map(each -> each.getInterval()).orElse(5)); - notifyConfig.setInterval(interval); + notifyConfig.setInterval(buildInterval(executorProperties)); notifyConfig.setReceives(buildReceive(executorProperties)); alarmNotifyConfigs.add(notifyConfig); } @@ -113,12 +123,21 @@ public void initCacheAndLock(Map> buildSingleNotif .forEach(each -> alarmControlHandler.initCacheAndLock(each.getTpId(), each.getPlatform(), each.getInterval()))); } - private String buildReceive(ExecutorProperties executorProperties) { - String receives = Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getNotify()).map(each -> each.getReceives()).orElse(""); - if (executorProperties.getNotify() != null && StringUtil.isNotEmpty(executorProperties.getNotify().getReceives())) { - receives = executorProperties.getNotify().getReceives(); - } - return receives; + private int buildInterval(IExecutorProperties executorProperties) { + return Optional.ofNullable(executorProperties.getNotify()) + .map(DynamicThreadPoolNotifyProperties::getInterval) + .orElse(Optional.ofNullable(configProperties.getDefaultExecutor()) + .map(ExecutorProperties::getNotify) + .map(DynamicThreadPoolNotifyProperties::getInterval) + .orElse(5)); + } + + private String buildReceive(IExecutorProperties executorProperties) { + return Optional.ofNullable(executorProperties.getNotify()) + .map(DynamicThreadPoolNotifyProperties::getReceives) + .orElse(Optional.ofNullable(configProperties.getDefaultExecutor()) + .map(ExecutorProperties::getNotify) + .map(DynamicThreadPoolNotifyProperties::getReceives).orElse("")); } private String getToken(NotifyPlatformProperties platformProperties) { diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java index ee212de43c1..9370cbfbdf1 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java @@ -30,6 +30,7 @@ import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.message.dto.NotifyConfigDTO; +import cn.hippo4j.message.enums.NotifyTypeEnum; import cn.hippo4j.message.request.ChangeParameterNotifyRequest; import cn.hippo4j.message.service.GlobalNotifyAlarmManage; import cn.hippo4j.message.service.Hippo4jBaseSendMessageService; @@ -166,7 +167,8 @@ private void checkNotifyConsistencyAndReplace(ExecutorProperties executorPropert boolean checkNotifyConfig = false; boolean checkNotifyAlarm = false; List changeKeys = new ArrayList<>(); - Map> newDynamicThreadPoolNotifyMap = configModeNotifyConfigBuilder.buildSingleNotifyConfig(executorProperties); + Map> newDynamicThreadPoolNotifyMap = + configModeNotifyConfigBuilder.buildSingleNotifyConfig(executorProperties.getThreadPoolId(), executorProperties); Map> notifyConfigs = hippo4jBaseSendMessageService.getNotifyConfigs(); if (CollectionUtil.isNotEmpty(notifyConfigs)) { for (Map.Entry> each : newDynamicThreadPoolNotifyMap.entrySet()) { diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java index f117082fc02..2f417953fee 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java @@ -48,7 +48,7 @@ public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynami if (wrapper != null && !wrapper.isInitFlag()) { Hippo4jBaseSendMessageService sendMessageService = ApplicationContextHolder.getBean(Hippo4jBaseSendMessageService.class); ConfigModeNotifyConfigBuilder configBuilder = ApplicationContextHolder.getBean(ConfigModeNotifyConfigBuilder.class); - Map> notifyConfig = configBuilder.buildSingleNotifyConfig(executorProperties); + Map> notifyConfig = configBuilder.buildSingleNotifyConfig(threadPoolId, executorProperties); sendMessageService.putPlatform(notifyConfig); wrapper.setInitFlag(Boolean.TRUE); } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java index e741cfc50ea..bfe1257bca4 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java @@ -19,19 +19,19 @@ import cn.hippo4j.adapter.web.WebThreadPoolHandlerChoose; import cn.hippo4j.adapter.web.WebThreadPoolService; -import cn.hippo4j.common.api.NotifyRequest; import cn.hippo4j.common.api.ThreadPoolConfigChange; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.model.ThreadPoolParameter; import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; -import cn.hippo4j.config.springboot.starter.config.WebThreadPoolProperties; +import cn.hippo4j.config.springboot.starter.config.WebExecutorProperties; +import cn.hippo4j.message.enums.NotifyTypeEnum; import cn.hippo4j.message.request.ChangeParameterNotifyRequest; +import cn.hippo4j.message.request.WebChangeParameterNotifyRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.Order; import java.util.Objects; -import java.util.Optional; import static cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfigDynamicRefreshEventOrder.WEB_EXECUTOR_LISTENER; @@ -41,7 +41,7 @@ @Slf4j @Order(WEB_EXECUTOR_LISTENER) @SuppressWarnings("all") -public class WebExecutorRefreshListener extends AbstractRefreshListener { +public class WebExecutorRefreshListener extends AbstractRefreshListener { private final ThreadPoolConfigChange configChange; @@ -50,7 +50,7 @@ public WebExecutorRefreshListener(ThreadPoolConfigChange configChange) { } @Override - public String getNodes(WebThreadPoolProperties properties) { + public String getNodes(WebExecutorProperties properties) { return properties.getNodes(); } @@ -79,9 +79,10 @@ public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynami } if (!Objects.equals(beforeParameter.getCoreSize(), nowParameter.getCoreSize()) || !Objects.equals(beforeParameter.getMaxSize(), nowParameter.getMaxSize()) - || !Objects.equals(beforeParameter.getKeepAliveTime(), nowParameter.getKeepAliveTime())) { + || !Objects.equals(beforeParameter.getKeepAliveTime(), nowParameter.getKeepAliveTime()) + || !webThreadPoolService.isContainerStarted()) { webThreadPoolService.updateWebThreadPool(nowParameter); - configChange.sendPoolConfigChange(buildChangeRequest(beforeParameter, nowParameter)); + configChange.sendPoolConfigChange(buildChangeRequest(beforeParameter, nowParameter, webThreadPoolService)); } } } catch (Exception ex) { @@ -90,24 +91,27 @@ public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynami } /** - * Constructing a request for thread pool parameter change notification + * Constructing a request for web thread pool parameter change notification * @param before * @param now * @return */ - private ChangeParameterNotifyRequest buildChangeRequest(ThreadPoolParameter before, ThreadPoolParameter now) { - return ChangeParameterNotifyRequest.builder() + private WebChangeParameterNotifyRequest buildChangeRequest(ThreadPoolParameter before, ThreadPoolParameter now, + WebThreadPoolService webThreadPoolService) { + WebChangeParameterNotifyRequest changeNotifyRequest = WebChangeParameterNotifyRequest.builder() .beforeCorePoolSize(before.getCoreSize()) .nowCorePoolSize(now.getCoreSize()) .beforeMaximumPoolSize(before.getMaxSize()) .nowMaximumPoolSize(now.getMaxSize()) .beforeKeepAliveTime(before.getKeepAliveTime()) .nowKeepAliveTime(now.getKeepAliveTime()).build(); + changeNotifyRequest.setThreadPoolId(webThreadPoolService.getWebContainerType().name()); + return changeNotifyRequest; } private ThreadPoolParameterInfo buildWebPoolParameter(BootstrapConfigProperties bindableCoreProperties) { ThreadPoolParameterInfo threadPoolParameterInfo = null; - WebThreadPoolProperties webThreadPoolProperties = bindableCoreProperties.getWeb(); + WebExecutorProperties webThreadPoolProperties = bindableCoreProperties.getWeb(); if (webThreadPoolProperties != null && webThreadPoolProperties.getEnable() && match(webThreadPoolProperties)) { threadPoolParameterInfo = ThreadPoolParameterInfo.builder() From 5a9a16d1e406275a9ab6e7c0d8f3d95980898ff4 Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Tue, 21 Mar 2023 15:22:42 +0800 Subject: [PATCH 03/10] Add application profile enum. --- .../config/ApplicationContextHolder.java | 9 +++++ .../cn/hippo4j/common/enums/ProfileEnum.java | 39 +++++++++++++++++++ .../WebThreadPoolConfigChangeHandler.java | 12 +++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java b/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java index 5c2eb3fe4cf..e75df41ad63 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java @@ -17,6 +17,7 @@ package cn.hippo4j.common.config; +import cn.hippo4j.common.enums.ProfileEnum; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -90,4 +91,12 @@ public static A findAnnotationOnBean(String beanName, Cla public static ApplicationContext getInstance() { return CONTEXT; } + + /** + * Get current application profile. + */ + public static ProfileEnum getActiveProfile() { + return ProfileEnum.of(CONTEXT.getEnvironment().getActiveProfiles()[0]); + } + } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java b/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java new file mode 100644 index 00000000000..4d08040ca5e --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.enums; + +import java.util.Arrays; + +/** + * Application profile enum. + */ +public enum ProfileEnum { + + UNKNOWN, + DEV, + TEST01, + PRE, + PROD; + + public static ProfileEnum of(String profileStr) { + return Arrays.stream(values()) + .filter(v -> v.name().equalsIgnoreCase(profileStr)) + .findFirst() + .orElse(UNKNOWN); + } +} diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java index 56b8c448d63..70e8c9f1cde 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java @@ -33,20 +33,28 @@ @Slf4j public class WebThreadPoolConfigChangeHandler implements ThreadPoolConfigChange { + @Value("${spring.profiles.active:UNKNOWN}") + private String active; + + @Value("${spring.dynamic.thread-pool.item-id:}") + private String itemId; + @Value("${spring.application.name:UNKNOWN}") private String applicationName; private final Hippo4jSendMessageService hippo4jSendMessageService; /** - * Send pool config change. + * Send pool config change message for web. * * @param requestParam change parameter notify request */ @Override public void sendPoolConfigChange(WebChangeParameterNotifyRequest requestParam) { try { - requestParam.setAppName(applicationName); + requestParam.setActive(active.toUpperCase()); + String appName = StringUtil.isBlank(itemId) ? applicationName : itemId; + requestParam.setAppName(appName); requestParam.setIdentify(IdentifyUtil.getIdentify()); hippo4jSendMessageService.sendChangeMessage(requestParam); } catch (Throwable th) { From aee8f5326c04e6ab36bbe31f1a57162fc15d4416 Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Tue, 21 Mar 2023 16:26:21 +0800 Subject: [PATCH 04/10] Revert the bugfix changes and split them into issue #1134. --- .../DefaultAbstractWebThreadPoolService.java | 20 ------------------- .../adapter/web/WebThreadPoolService.java | 5 ----- .../web/AbstractWebThreadPoolService1x.java | 20 ------------------- .../event/WebExecutorRefreshListener.java | 5 +---- 4 files changed, 1 insertion(+), 49 deletions(-) diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/DefaultAbstractWebThreadPoolService.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/DefaultAbstractWebThreadPoolService.java index 6f14b28a4aa..ebb98f0eaad 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/DefaultAbstractWebThreadPoolService.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/DefaultAbstractWebThreadPoolService.java @@ -18,29 +18,22 @@ package cn.hippo4j.adapter.web; import cn.hippo4j.common.config.ApplicationContextHolder; -import io.undertow.Undertow; -import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.context.WebServerApplicationContext; import org.springframework.boot.web.server.WebServer; import org.springframework.context.ApplicationContext; -import org.springframework.util.ReflectionUtils; -import java.lang.reflect.Field; import java.util.concurrent.Executor; /** * Default WebThreadPoolService abstract class, * reuses common capabilities for web container operations. */ -@Slf4j public abstract class DefaultAbstractWebThreadPoolService extends AbstractWebThreadPoolService { public DefaultAbstractWebThreadPoolService(IWebThreadPoolHandlerSupport support) { super(support); } - private static final String STARTED_FIELD_NAME = "started"; - /** * Get the internal abstract method of the web container thread pool, * to be implemented by subclasses. @@ -60,19 +53,6 @@ public Integer getPort() { return getWebServer().getPort(); } - @Override - public boolean isContainerStarted() { - try { - WebServer container = getWebServer(); - Field field = ReflectionUtils.findField(WebServer.class, STARTED_FIELD_NAME); - ReflectionUtils.makeAccessible(field); - return (boolean) ReflectionUtils.getField(field, container); - } catch (Throwable th) { - log.error("Failed to get isStarted flag.", th); - return false; - } - } - /** * Get the thread pool object of the current web container based on the WebServer. * @param webServer current Web-Server. diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java index f8cfccb80b1..082c890f350 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java @@ -30,11 +30,6 @@ */ public interface WebThreadPoolService { - /** - * Determine if the web container has started. - */ - boolean isContainerStarted(); - /** * Get web thread pool. * diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/AbstractWebThreadPoolService1x.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/AbstractWebThreadPoolService1x.java index d3d5c8ab75e..333a4d07f02 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/AbstractWebThreadPoolService1x.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-1x-starter/src/main/java/cn/hippo4j/config/springboot1x/starter/web/AbstractWebThreadPoolService1x.java @@ -20,25 +20,18 @@ import cn.hippo4j.adapter.web.AbstractWebThreadPoolService; import cn.hippo4j.adapter.web.IWebThreadPoolHandlerSupport; import cn.hippo4j.common.config.ApplicationContextHolder; -import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; -import org.springframework.util.ReflectionUtils; - -import java.lang.reflect.Field; /** * Abstract class for adapting WebThreadPoolService to Spring 1.x version. */ -@Slf4j public abstract class AbstractWebThreadPoolService1x extends AbstractWebThreadPoolService { public AbstractWebThreadPoolService1x(IWebThreadPoolHandlerSupport support) { super(support); } - private static final String STARTED_FIELD_NAME = "started"; - /** * Get the embedded Servlet container from the Spring application context. */ @@ -53,17 +46,4 @@ protected EmbeddedServletContainer getContainer() { public Integer getPort() { return getContainer().getPort(); } - - @Override - public boolean isContainerStarted() { - try { - EmbeddedServletContainer container = getContainer(); - Field field = ReflectionUtils.findField(EmbeddedServletContainer.class, STARTED_FIELD_NAME); - ReflectionUtils.makeAccessible(field); - return (boolean) ReflectionUtils.getField(field, container); - } catch (Throwable th) { - log.error("Failed to get isStarted flag.", th); - return false; - } - } } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java index bfe1257bca4..38205b45edf 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java @@ -25,8 +25,6 @@ import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; import cn.hippo4j.config.springboot.starter.config.WebExecutorProperties; -import cn.hippo4j.message.enums.NotifyTypeEnum; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; import cn.hippo4j.message.request.WebChangeParameterNotifyRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.Order; @@ -79,8 +77,7 @@ public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynami } if (!Objects.equals(beforeParameter.getCoreSize(), nowParameter.getCoreSize()) || !Objects.equals(beforeParameter.getMaxSize(), nowParameter.getMaxSize()) - || !Objects.equals(beforeParameter.getKeepAliveTime(), nowParameter.getKeepAliveTime()) - || !webThreadPoolService.isContainerStarted()) { + || !Objects.equals(beforeParameter.getKeepAliveTime(), nowParameter.getKeepAliveTime())) { webThreadPoolService.updateWebThreadPool(nowParameter); configChange.sendPoolConfigChange(buildChangeRequest(beforeParameter, nowParameter, webThreadPoolService)); } From e1aa223a8fd87ab87bb8bf25fb7612a5bcd13e50 Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Tue, 21 Mar 2023 19:19:54 +0800 Subject: [PATCH 05/10] Revert @Order annotation in AbstractWebThreadPoolService. --- .../cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java index d8cfbe7e2c5..e50840922ce 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java @@ -37,7 +37,6 @@ * Abstract web thread pool service. */ @Slf4j -@Order(Ordered.HIGHEST_PRECEDENCE) public abstract class AbstractWebThreadPoolService implements WebThreadPoolService, ApplicationRunner { private final IWebThreadPoolHandlerSupport support; From c8c779dbfe2466ffe9351d9bed48e14b6a2be57b Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Wed, 5 Apr 2023 21:40:02 +0800 Subject: [PATCH 06/10] fix profile enum name with test environment. --- .../src/main/java/cn/hippo4j/common/enums/ProfileEnum.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java b/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java index 4d08040ca5e..d69888a9de0 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java @@ -26,7 +26,7 @@ public enum ProfileEnum { UNKNOWN, DEV, - TEST01, + TEST, PRE, PROD; From 596af93328eb8ee4247bc556105212fcbc1e5bdd Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Thu, 6 Apr 2023 10:57:54 +0800 Subject: [PATCH 07/10] Fix: remove unused imports. --- .../starter/adapter/web/WebThreadPoolHandlerConfiguration.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter-adapter/hippo4j-spring-boot-starter-adapter-web/src/main/java/cn/hippo4j/springboot/starter/adapter/web/WebThreadPoolHandlerConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter-adapter/hippo4j-spring-boot-starter-adapter-web/src/main/java/cn/hippo4j/springboot/starter/adapter/web/WebThreadPoolHandlerConfiguration.java index a4f033ed48c..2027668b208 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter-adapter/hippo4j-spring-boot-starter-adapter-web/src/main/java/cn/hippo4j/springboot/starter/adapter/web/WebThreadPoolHandlerConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter-adapter/hippo4j-spring-boot-starter-adapter-web/src/main/java/cn/hippo4j/springboot/starter/adapter/web/WebThreadPoolHandlerConfiguration.java @@ -24,7 +24,6 @@ import cn.hippo4j.adapter.web.undertow.DefaultUndertowWebThreadPoolHandler; import cn.hippo4j.adapter.web.WebThreadPoolRunStateHandler; import cn.hippo4j.adapter.web.undertow.UndertowWebThreadPoolHandlerAdapt; -import cn.hippo4j.common.api.ThreadPoolConfigChange; import io.undertow.Undertow; import org.apache.catalina.startup.Tomcat; import org.apache.coyote.UpgradeProtocol; From 22b4d0a9845bf7af24b3d09287e03f40bee1bce8 Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Sun, 9 Apr 2023 22:38:18 +0800 Subject: [PATCH 08/10] Delete ProfileEnum class. --- .../config/ApplicationContextHolder.java | 9 ----- .../cn/hippo4j/common/enums/ProfileEnum.java | 39 ------------------- 2 files changed, 48 deletions(-) delete mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java b/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java index e75df41ad63..5c2eb3fe4cf 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java @@ -17,7 +17,6 @@ package cn.hippo4j.common.config; -import cn.hippo4j.common.enums.ProfileEnum; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -91,12 +90,4 @@ public static A findAnnotationOnBean(String beanName, Cla public static ApplicationContext getInstance() { return CONTEXT; } - - /** - * Get current application profile. - */ - public static ProfileEnum getActiveProfile() { - return ProfileEnum.of(CONTEXT.getEnvironment().getActiveProfiles()[0]); - } - } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java b/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java deleted file mode 100644 index d69888a9de0..00000000000 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package cn.hippo4j.common.enums; - -import java.util.Arrays; - -/** - * Application profile enum. - */ -public enum ProfileEnum { - - UNKNOWN, - DEV, - TEST, - PRE, - PROD; - - public static ProfileEnum of(String profileStr) { - return Arrays.stream(values()) - .filter(v -> v.name().equalsIgnoreCase(profileStr)) - .findFirst() - .orElse(UNKNOWN); - } -} From c0b1bb6dceca931e4a0ef799a416b3b4fa0fdffa Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Mon, 10 Apr 2023 18:41:30 +0800 Subject: [PATCH 09/10] Modify the way of obtaining the ID of the web thread pool. --- .../config/DynamicThreadPoolAutoConfiguration.java | 10 +++++++++- .../starter/config/IExecutorProperties.java | 5 +++++ .../starter/config/WebExecutorProperties.java | 5 +++++ .../starter/notify/ConfigModeNotifyConfigBuilder.java | 11 +++++++---- .../event/DynamicThreadPoolRefreshListener.java | 2 +- .../refresher/event/PlatformsRefreshListener.java | 2 +- 6 files changed, 28 insertions(+), 7 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index de2f7313473..f277f6c4775 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -21,6 +21,7 @@ import cn.hippo4j.common.api.ThreadPoolCheckAlarm; import cn.hippo4j.common.api.ThreadPoolConfigChange; import cn.hippo4j.common.config.ApplicationContextHolder; +import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.config.springboot.starter.monitor.ThreadPoolMonitorExecutor; import cn.hippo4j.config.springboot.starter.notify.ConfigModeNotifyConfigBuilder; import cn.hippo4j.config.springboot.starter.refresher.event.AdapterExecutorsRefreshListener; @@ -51,6 +52,8 @@ import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; +import java.util.Optional; + /** * Dynamic thread-pool auto-configuration. * @@ -97,7 +100,12 @@ public DefaultThreadPoolConfigChangeHandler defaultThreadPoolConfigChangeHandler @Bean @ConditionalOnMissingBean - public WebThreadPoolConfigChangeHandler webThreadPoolConfigChangeHandler(Hippo4jSendMessageService hippo4jSendMessageService) { + public WebThreadPoolConfigChangeHandler webThreadPoolConfigChangeHandler(BootstrapConfigProperties bootstrapConfigProperties, + WebThreadPoolService webThreadPoolService, + Hippo4jSendMessageService hippo4jSendMessageService) { + if (bootstrapConfigProperties.getWeb() != null && StringUtil.isBlank(bootstrapConfigProperties.getWeb().getThreadPoolId())) { + bootstrapConfigProperties.getWeb().setThreadPoolId(webThreadPoolService.getWebContainerType().name()); + } return new WebThreadPoolConfigChangeHandler(hippo4jSendMessageService); } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java index d13fbee4679..7bda01cf5ee 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java @@ -22,6 +22,11 @@ */ public interface IExecutorProperties { + /** + * Thread pool id + */ + String getThreadPoolId(); + /** * Core pool size */ diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java index 918503a9a1d..01a850b7dac 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java @@ -25,6 +25,11 @@ @Data public class WebExecutorProperties implements IExecutorProperties { + /** + * Thread pool id + */ + private String threadPoolId; + /** * Core pool size */ diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java index 3531e715046..85369aa08aa 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java @@ -62,15 +62,17 @@ public Map> buildNotify() { return resultMap; } for (ExecutorProperties executorProperties : executors) { - Map> buildSingleNotifyConfig = - buildSingleNotifyConfig(executorProperties.getThreadPoolId(), executorProperties); + Map> buildSingleNotifyConfig = buildSingleNotifyConfig(executorProperties); initCacheAndLock(buildSingleNotifyConfig); resultMap.putAll(buildSingleNotifyConfig); } // register notify config for web WebExecutorProperties webProperties = configProperties.getWeb(); + if (StringUtil.isBlank(webProperties.getThreadPoolId())) { + webProperties.setThreadPoolId(webThreadPoolService.getWebContainerType().name()); + } Map> webSingleNotifyConfigMap = - buildSingleNotifyConfig(webThreadPoolService.getWebContainerType().name(), webProperties); + buildSingleNotifyConfig(webProperties); initCacheAndLock(webSingleNotifyConfigMap); resultMap.putAll(webSingleNotifyConfigMap); @@ -83,7 +85,8 @@ public Map> buildNotify() { * @param executorProperties * @return */ - public Map> buildSingleNotifyConfig(String threadPoolId, IExecutorProperties executorProperties) { + public Map> buildSingleNotifyConfig(IExecutorProperties executorProperties) { + String threadPoolId = executorProperties.getThreadPoolId(); Map> resultMap = new HashMap<>(); String alarmBuildKey = threadPoolId + "+ALARM"; List alarmNotifyConfigs = new ArrayList<>(); diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java index 9370cbfbdf1..f802c27b2d6 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java @@ -168,7 +168,7 @@ private void checkNotifyConsistencyAndReplace(ExecutorProperties executorPropert boolean checkNotifyAlarm = false; List changeKeys = new ArrayList<>(); Map> newDynamicThreadPoolNotifyMap = - configModeNotifyConfigBuilder.buildSingleNotifyConfig(executorProperties.getThreadPoolId(), executorProperties); + configModeNotifyConfigBuilder.buildSingleNotifyConfig(executorProperties); Map> notifyConfigs = hippo4jBaseSendMessageService.getNotifyConfigs(); if (CollectionUtil.isNotEmpty(notifyConfigs)) { for (Map.Entry> each : newDynamicThreadPoolNotifyMap.entrySet()) { diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java index 2f417953fee..f117082fc02 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java @@ -48,7 +48,7 @@ public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynami if (wrapper != null && !wrapper.isInitFlag()) { Hippo4jBaseSendMessageService sendMessageService = ApplicationContextHolder.getBean(Hippo4jBaseSendMessageService.class); ConfigModeNotifyConfigBuilder configBuilder = ApplicationContextHolder.getBean(ConfigModeNotifyConfigBuilder.class); - Map> notifyConfig = configBuilder.buildSingleNotifyConfig(threadPoolId, executorProperties); + Map> notifyConfig = configBuilder.buildSingleNotifyConfig(executorProperties); sendMessageService.putPlatform(notifyConfig); wrapper.setInitFlag(Boolean.TRUE); } From 17b62f78532b645d4dce86077351878f86f64afc Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Mon, 10 Apr 2023 19:41:29 +0800 Subject: [PATCH 10/10] Move the IExecutorProperties class to the common module. --- .../hippo4j/common/api/ExecutorNotifyProperties.java | 4 ++-- .../cn/hippo4j/common/api}/IExecutorProperties.java | 4 ++-- .../starter/config/ExecutorProperties.java | 4 +++- .../starter/config/WebExecutorProperties.java | 4 +++- .../notify/ConfigModeNotifyConfigBuilder.java | 10 ++++++---- .../support/DynamicThreadPoolPostProcessor.java | 12 ++++++------ 6 files changed, 22 insertions(+), 16 deletions(-) rename hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolNotifyProperties.java => hippo4j-common/src/main/java/cn/hippo4j/common/api/ExecutorNotifyProperties.java (91%) rename {hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config => hippo4j-common/src/main/java/cn/hippo4j/common/api}/IExecutorProperties.java (92%) diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolNotifyProperties.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/ExecutorNotifyProperties.java similarity index 91% rename from hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolNotifyProperties.java rename to hippo4j-common/src/main/java/cn/hippo4j/common/api/ExecutorNotifyProperties.java index 8f94c1a576f..9334e9db9e9 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolNotifyProperties.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/ExecutorNotifyProperties.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.config.springboot.starter.config; +package cn.hippo4j.common.api; import lombok.AllArgsConstructor; import lombok.Data; @@ -27,7 +27,7 @@ @Data @NoArgsConstructor @AllArgsConstructor -public class DynamicThreadPoolNotifyProperties { +public class ExecutorNotifyProperties { /** * Thread pool run alarm interval. unit: s diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/IExecutorProperties.java similarity index 92% rename from hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java rename to hippo4j-common/src/main/java/cn/hippo4j/common/api/IExecutorProperties.java index 7bda01cf5ee..7df4dd8c3be 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/IExecutorProperties.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/IExecutorProperties.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.config.springboot.starter.config; +package cn.hippo4j.common.api; /** * Interface for thread pool configuration. @@ -45,5 +45,5 @@ public interface IExecutorProperties { /** * Notify configs */ - DynamicThreadPoolNotifyProperties getNotify(); + ExecutorNotifyProperties getNotify(); } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java index ec65971081f..9d179d220c6 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java @@ -17,6 +17,8 @@ package cn.hippo4j.config.springboot.starter.config; +import cn.hippo4j.common.api.ExecutorNotifyProperties; +import cn.hippo4j.common.api.IExecutorProperties; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -101,7 +103,7 @@ public class ExecutorProperties implements IExecutorProperties { /** * Notify */ - private DynamicThreadPoolNotifyProperties notify; + private ExecutorNotifyProperties notify; /** * Nodes, application startup is not affect, change properties is effect diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java index 01a850b7dac..95569405b7f 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java @@ -17,6 +17,8 @@ package cn.hippo4j.config.springboot.starter.config; +import cn.hippo4j.common.api.ExecutorNotifyProperties; +import cn.hippo4j.common.api.IExecutorProperties; import lombok.Data; /** @@ -58,5 +60,5 @@ public class WebExecutorProperties implements IExecutorProperties { /** * Notify config */ - private DynamicThreadPoolNotifyProperties notify; + private ExecutorNotifyProperties notify; } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java index 85369aa08aa..e37935c376a 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java @@ -18,6 +18,8 @@ package cn.hippo4j.config.springboot.starter.notify; import cn.hippo4j.adapter.web.WebThreadPoolService; +import cn.hippo4j.common.api.ExecutorNotifyProperties; +import cn.hippo4j.common.api.IExecutorProperties; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.config.springboot.starter.config.*; @@ -128,19 +130,19 @@ public void initCacheAndLock(Map> buildSingleNotif private int buildInterval(IExecutorProperties executorProperties) { return Optional.ofNullable(executorProperties.getNotify()) - .map(DynamicThreadPoolNotifyProperties::getInterval) + .map(ExecutorNotifyProperties::getInterval) .orElse(Optional.ofNullable(configProperties.getDefaultExecutor()) .map(ExecutorProperties::getNotify) - .map(DynamicThreadPoolNotifyProperties::getInterval) + .map(ExecutorNotifyProperties::getInterval) .orElse(5)); } private String buildReceive(IExecutorProperties executorProperties) { return Optional.ofNullable(executorProperties.getNotify()) - .map(DynamicThreadPoolNotifyProperties::getReceives) + .map(ExecutorNotifyProperties::getReceives) .orElse(Optional.ofNullable(configProperties.getDefaultExecutor()) .map(ExecutorProperties::getNotify) - .map(DynamicThreadPoolNotifyProperties::getReceives).orElse("")); + .map(ExecutorNotifyProperties::getReceives).orElse("")); } private String getToken(NotifyPlatformProperties platformProperties) { diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java index 5cc557e82f9..70137141824 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java @@ -23,7 +23,7 @@ import cn.hippo4j.common.toolkit.ReflectUtil; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; -import cn.hippo4j.config.springboot.starter.config.DynamicThreadPoolNotifyProperties; +import cn.hippo4j.common.api.ExecutorNotifyProperties; import cn.hippo4j.config.springboot.starter.config.ExecutorProperties; import cn.hippo4j.core.executor.DynamicThreadPool; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; @@ -229,7 +229,7 @@ private ExecutorProperties buildExecutorProperties(ExecutorProperties executorPr * @return thread-pool notify alarm */ private ThreadPoolNotifyAlarm buildThreadPoolNotifyAlarm(ExecutorProperties executorProperties) { - DynamicThreadPoolNotifyProperties notify = Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null); + ExecutorNotifyProperties notify = Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null); boolean isAlarm = Optional.ofNullable(executorProperties.getAlarm()) .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getAlarm).orElse(true)); int activeAlarm = Optional.ofNullable(executorProperties.getActiveAlarm()) @@ -237,11 +237,11 @@ private ThreadPoolNotifyAlarm buildThreadPoolNotifyAlarm(ExecutorProperties exec int capacityAlarm = Optional.ofNullable(executorProperties.getCapacityAlarm()) .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getCapacityAlarm).orElse(80)); int interval = Optional.ofNullable(notify) - .map(DynamicThreadPoolNotifyProperties::getInterval) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getNotify).map(DynamicThreadPoolNotifyProperties::getInterval).orElse(5)); + .map(ExecutorNotifyProperties::getInterval) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getNotify).map(ExecutorNotifyProperties::getInterval).orElse(5)); String receive = Optional.ofNullable(notify) - .map(DynamicThreadPoolNotifyProperties::getReceives) - .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getNotify).map(DynamicThreadPoolNotifyProperties::getReceives).orElse("")); + .map(ExecutorNotifyProperties::getReceives) + .orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getNotify).map(ExecutorNotifyProperties::getReceives).orElse("")); ThreadPoolNotifyAlarm threadPoolNotifyAlarm = new ThreadPoolNotifyAlarm(isAlarm, activeAlarm, capacityAlarm); threadPoolNotifyAlarm.setInterval(interval); threadPoolNotifyAlarm.setReceives(receive);