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-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-common/src/main/java/cn/hippo4j/common/api/IExecutorProperties.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/IExecutorProperties.java new file mode 100644 index 00000000000..7df4dd8c3be --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/IExecutorProperties.java @@ -0,0 +1,49 @@ +/* + * 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.api; + +/** + * Interface for thread pool configuration. + */ +public interface IExecutorProperties { + + /** + * Thread pool id + */ + String getThreadPoolId(); + + /** + * Core pool size + */ + Integer getCorePoolSize(); + + /** + * Maximum pool size + */ + Integer getMaximumPoolSize(); + + /** + * Keep alive time + */ + Long getKeepAliveTime(); + + /** + * Notify configs + */ + ExecutorNotifyProperties getNotify(); +} 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-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.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 message for web. + * + * @param requestParam change parameter notify request + */ + @Override + public void sendPoolConfigChange(WebChangeParameterNotifyRequest requestParam) { + try { + requestParam.setActive(active.toUpperCase()); + String appName = StringUtil.isBlank(itemId) ? applicationName : itemId; + requestParam.setAppName(appName); + 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-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/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 5b76bc9f568..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 @@ -17,9 +17,11 @@ 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; +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; @@ -34,11 +36,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; @@ -54,6 +52,8 @@ import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; +import java.util.Optional; + /** * Dynamic thread-pool auto-configuration. * @@ -82,8 +82,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 +94,21 @@ 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(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); + } + @Bean public DynamicThreadPoolPostProcessor dynamicThreadPoolPostProcessor(ApplicationContextHolder hippo4JApplicationContextHolder) { return new DynamicThreadPoolPostProcessor(bootstrapConfigProperties); @@ -110,7 +121,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); @@ -127,8 +138,9 @@ public PlatformsRefreshListener hippo4jPlatformsListener() { } @Bean - public WebExecutorRefreshListener hippo4jWebExecutorListener() { - return new WebExecutorRefreshListener(); + @SuppressWarnings("all") + public WebExecutorRefreshListener hippo4jWebExecutorListener(WebThreadPoolConfigChangeHandler 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/ExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java index 58754275188..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; @@ -31,7 +33,7 @@ @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) -public class ExecutorProperties { +public class ExecutorProperties implements IExecutorProperties { /** * Thread pool id @@ -101,7 +103,7 @@ public class ExecutorProperties { /** * 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/WebThreadPoolProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebExecutorProperties.java similarity index 76% 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 f2d1ff024d0..95569405b7f 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 @@ -17,13 +17,20 @@ package cn.hippo4j.config.springboot.starter.config; +import cn.hippo4j.common.api.ExecutorNotifyProperties; +import cn.hippo4j.common.api.IExecutorProperties; import lombok.Data; /** - * Web thread pool properties. + * Web thread pool executor properties. */ @Data -public class WebThreadPoolProperties { +public class WebExecutorProperties implements IExecutorProperties { + + /** + * Thread pool id + */ + private String threadPoolId; /** * Core pool size @@ -38,7 +45,7 @@ public class WebThreadPoolProperties { /** * Keep alive time */ - private Integer keepAliveTime; + private Long keepAliveTime; /** * Nodes, application startup is not affect, change properties is effect @@ -49,4 +56,9 @@ public class WebThreadPoolProperties { * these propertied is enabled? */ private Boolean enable = true; + + /** + * Notify config + */ + 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 7a01ae4e166..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 @@ -17,11 +17,12 @@ 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.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,16 +43,23 @@ 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; } @@ -60,6 +68,16 @@ public Map> buildNotify() { 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(webProperties); + initCacheAndLock(webSingleNotifyConfigMap); + resultMap.putAll(webSingleNotifyConfigMap); + return resultMap; } @@ -69,9 +87,9 @@ public Map> buildNotify() { * @param executorProperties * @return */ - public Map> buildSingleNotifyConfig(ExecutorProperties executorProperties) { - Map> resultMap = new HashMap<>(); + public Map> buildSingleNotifyConfig(IExecutorProperties executorProperties) { String threadPoolId = executorProperties.getThreadPoolId(); + Map> resultMap = new HashMap<>(); String alarmBuildKey = threadPoolId + "+ALARM"; List alarmNotifyConfigs = new ArrayList<>(); List notifyPlatforms = configProperties.getNotifyPlatforms(); @@ -82,10 +100,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 +128,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(ExecutorNotifyProperties::getInterval) + .orElse(Optional.ofNullable(configProperties.getDefaultExecutor()) + .map(ExecutorProperties::getNotify) + .map(ExecutorNotifyProperties::getInterval) + .orElse(5)); + } + + private String buildReceive(IExecutorProperties executorProperties) { + return Optional.ofNullable(executorProperties.getNotify()) + .map(ExecutorNotifyProperties::getReceives) + .orElse(Optional.ofNullable(configProperties.getDefaultExecutor()) + .map(ExecutorProperties::getNotify) + .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/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..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 @@ -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); 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/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..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 @@ -19,16 +19,17 @@ import cn.hippo4j.adapter.web.WebThreadPoolHandlerChoose; import cn.hippo4j.adapter.web.WebThreadPoolService; +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.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; @@ -37,10 +38,17 @@ */ @Slf4j @Order(WEB_EXECUTOR_LISTENER) -public class WebExecutorRefreshListener extends AbstractRefreshListener { +@SuppressWarnings("all") +public class WebExecutorRefreshListener extends AbstractRefreshListener { + + private final ThreadPoolConfigChange configChange; + + public WebExecutorRefreshListener(ThreadPoolConfigChange configChange) { + this.configChange = configChange; + } @Override - public String getNodes(WebThreadPoolProperties properties) { + public String getNodes(WebExecutorProperties properties) { return properties.getNodes(); } @@ -71,6 +79,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, webThreadPoolService)); } } } catch (Exception ex) { @@ -78,9 +87,28 @@ public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynami } } + /** + * Constructing a request for web thread pool parameter change notification + * @param before + * @param now + * @return + */ + 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() 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);