From 4248512d98ece040a7aa34683543b4c87d067d96 Mon Sep 17 00:00:00 2001 From: magestack Date: Thu, 8 Jun 2023 23:50:41 +0800 Subject: [PATCH] Abstract alarm and notification kernel modules and refactor existing code (#1376) --- .../adapter/dubbo/DubboThreadPoolAdapter.java | 4 +- .../SpringThreadPoolRegisterSupport.java | 6 +- ...lExecutorConstructorMethodInterceptor.java | 4 +- .../src/main/resources/bootstrap.properties | 10 +- .../core/handler/TaskTraceBuilderHandler.java | 2 +- .../core/inittest/AlarmSendMessageTest.java | 8 +- .../RegisterDynamicThreadPoolTest.java | 2 +- .../core/inittest/TaskDecoratorTest.java | 8 +- .../server/adapter-kafka/pom.xml | 2 +- .../server/adapter-rabbitmq/pom.xml | 2 +- .../server/adapter-rocketmq/pom.xml | 2 +- .../server/adapter-stream-rabbitmq/pom.xml | 2 +- .../server/adapter-stream-rocketmq/pom.xml | 2 +- .../threadpool-example/server/server/pom.xml | 2 +- .../executor/ThreadPoolExecutorHolder.java | 7 + ...y.java => ThreadPoolExecutorRegistry.java} | 27 +++- .../propertie/EnvironmentProperties.java | 44 +++++++ .../common/propertie/IdentifyProperties.java | 32 +++++ .../common/toolkit/MessageConvert.java | 13 +- .../cn/hippo4j/common/toolkit/IoUtilTest.java | 1 + .../common/toolkit/JacksonHandlerTest.java | 3 +- .../common/toolkit/MessageConvertTest.java | 2 +- kernel/alarm/pom.xml | 13 ++ .../alarm}/api/ThreadPoolCheckAlarm.java | 16 +-- .../DefaultThreadPoolCheckAlarmHandler.java | 124 +++++++++--------- .../toolkit/ExecutorTraceContextUtil.java | 2 +- .../manage/GlobalConfigThreadPoolManage.java | 62 --------- .../manage/GlobalThreadPoolManage.java | 70 ---------- kernel/dynamic/mode/config/pom.xml | 4 + .../mode/config/parser/YamlConfigParser.java | 16 ++- .../DynamicThreadPoolRefreshListener.java | 4 +- kernel/message/api/pom.xml | 19 +++ .../message/api}/AlarmControlDTO.java | 4 +- .../message/api/NotifyConfigBuilder.java | 4 +- .../message/api}/NotifyConfigDTO.java | 3 +- .../message/api}/NotifyPlatformEnum.java | 2 +- .../message/api}/NotifyTypeEnum.java | 2 +- .../message/api}/ThreadPoolNotifyDTO.java | 3 +- kernel/message/core/pom.xml | 50 +++++++ .../core}/constant/DingAlarmConstants.java | 2 +- .../core}/constant/LarkAlarmConstants.java | 2 +- .../core}/constant/WeChatAlarmConstants.java | 2 +- .../platform/DingSendMessageHandler.java | 31 ++--- .../platform/LarkSendMessageHandler.java | 37 +++--- .../platform/WeChatSendMessageHandler.java | 26 ++-- .../base/AbstractRobotSendMessageHandler.java | 12 +- .../base/RobotMessageActualContent.java | 2 +- .../platform/base/RobotMessageExecuteDTO.java | 4 +- .../core}/request/AlarmNotifyRequest.java | 6 +- .../request/ChangeParameterNotifyRequest.java | 4 +- .../request/RobotAlarmNotifyRequest.java | 2 +- .../RobotChangeParameterNotifyRequest.java | 2 +- .../request/ThreadPoolNotifyRequest.java | 2 +- .../WebChangeParameterNotifyRequest.java | 5 +- .../core}/request/base/BaseNotifyRequest.java | 2 +- .../core}/service/AlarmControlHandler.java | 4 +- .../DefaultThreadPoolConfigChangeHandler.java | 24 ++-- .../service/GlobalNotifyAlarmManage.java | 2 +- .../core}/service/SendMessageHandler.java | 35 ++++- .../ThreadPoolBaseSendMessageService.java | 31 ++--- .../core}/service/ThreadPoolNotifyAlarm.java | 2 +- .../service/ThreadPoolSendMessageService.java | 12 +- .../WebThreadPoolConfigChangeHandler.java | 26 ++-- .../robot/dynamic-thread-pool/ding-alarm.txt | 0 .../robot/dynamic-thread-pool/ding-config.txt | 0 .../lark-alarm-timeout-replace.json | 0 .../lark-alarm-trace-replace.json | 0 .../robot/dynamic-thread-pool/lark-alarm.json | 0 .../dynamic-thread-pool/lark-config.json | 0 .../dynamic-thread-pool/wechat-alarm.txt | 0 .../dynamic-thread-pool/wechat-config.txt | 0 .../service/AlarmControlHandlerTest.java | 5 +- kernel/message/mode/config/pom.xml | 12 ++ kernel/message/mode/pom.xml | 18 +++ kernel/message/mode/server/pom.xml | 12 ++ kernel/message/pom.xml | 7 + starters/threadpool/adapter/pom.xml | 2 +- .../threadpool/config-spring-boot-1x/pom.xml | 2 +- starters/threadpool/config/pom.xml | 2 +- .../DynamicThreadPoolAutoConfiguration.java | 34 +++-- .../monitor/ThreadPoolMonitorExecutor.java | 29 ++-- .../notify/ConfigModeNotifyConfigBuilder.java | 6 +- ...bstractConfigThreadPoolDynamicRefresh.java | 4 +- .../refresher/ZookeeperRefresherHandler.java | 4 +- .../event/AbstractRefreshListener.java | 2 +- .../AdapterExecutorsRefreshListener.java | 4 +- .../DynamicThreadPoolRefreshListener.java | 42 +++--- .../event/PlatformsRefreshListener.java | 32 +++-- ... ThreadPoolConfigDynamicRefreshEvent.java} | 4 +- ...adPoolConfigDynamicRefreshEventOrder.java} | 2 +- .../event/WebExecutorRefreshListener.java | 9 +- .../DynamicThreadPoolConfigService.java | 17 +-- .../DynamicThreadPoolPostProcessor.java | 45 +++---- starters/threadpool/monitor/pom.xml | 2 +- starters/threadpool/pom.xml | 2 +- starters/threadpool/server/pom.xml | 9 +- .../DynamicThreadPoolAutoConfiguration.java | 28 ++-- .../core/BaseThreadDetailStateHandler.java | 8 +- .../springboot/starter/core/CacheData.java | 3 +- .../core/ServerThreadPoolDynamicRefresh.java | 10 +- .../monitor/ReportingEventExecutor.java | 12 +- .../monitor/collect/RunTimeInfoCollector.java | 4 +- .../notify/ServerModeNotifyConfigBuilder.java | 14 +- ...AdaptedThreadPoolDestroyPostProcessor.java | 18 +-- .../DynamicThreadPoolConfigService.java | 24 ++-- .../DynamicThreadPoolPostProcessor.java | 60 ++++----- threadpool/core/pom.xml | 5 + .../executor/DynamicThreadPoolExecutor.java | 4 + .../executor/DynamicThreadPoolWrapper.java | 3 + .../manage/GlobalThreadPoolManage.java | 101 +------------- .../impl/TaskRejectNotifyAlarmPlugin.java | 2 +- .../impl/TaskTimeoutNotifyAlarmPlugin.java | 6 +- .../proxy/RejectedProxyInvocationHandler.java | 2 +- .../state/AbstractThreadPoolRuntime.java | 10 +- .../state/ThreadPoolRunStateHandler.java | 8 +- .../Hippo4jDynamicThreadPoolInitializer.java | 47 +++++++ .../cn/hippo4j/core/toolkit/FileUtil.java | 9 +- .../cn/hippo4j/core/toolkit/IdentifyUtil.java | 18 +-- .../impl/TaskRejectNotifyAlarmPluginTest.java | 11 +- .../TaskTimeoutNotifyAlarmPluginTest.java | 11 +- .../RejectedProxyInvocationHandlerTest.java | 5 +- .../state/AbstractThreadPoolRuntimeTest.java | 22 +--- .../state/ThreadPoolRunStateHandlerTest.java | 19 ++- threadpool/message/pom.xml | 22 +--- .../message/config/MessageConfiguration.java | 26 ++-- ...PoolBaseSendMessageServiceInitializer.java | 45 +++++++ .../AbstractDynamicThreadPoolMonitor.java | 4 +- 127 files changed, 903 insertions(+), 825 deletions(-) rename infra/common/src/main/java/cn/hippo4j/common/executor/{ThreadPoolRegistry.java => ThreadPoolExecutorRegistry.java} (67%) create mode 100644 infra/common/src/main/java/cn/hippo4j/common/propertie/EnvironmentProperties.java create mode 100644 infra/common/src/main/java/cn/hippo4j/common/propertie/IdentifyProperties.java rename {threadpool/core/src/main/java/cn/hippo4j/core => kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm}/api/ThreadPoolCheckAlarm.java (94%) rename {threadpool/message/src/main/java/cn/hippo4j/message/service => kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/handler}/DefaultThreadPoolCheckAlarmHandler.java (64%) rename {threadpool/core/src/main/java/cn/hippo4j/core => kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm}/toolkit/ExecutorTraceContextUtil.java (97%) delete mode 100644 kernel/dynamic/core/src/main/java/cn/hippo4j/threadpool/dynamic/core/executor/manage/GlobalConfigThreadPoolManage.java delete mode 100644 kernel/dynamic/core/src/main/java/cn/hippo4j/threadpool/dynamic/core/executor/manage/GlobalThreadPoolManage.java create mode 100644 kernel/message/api/pom.xml rename {threadpool/message/src/main/java/cn/hippo4j/message/dto => kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api}/AlarmControlDTO.java (93%) rename {threadpool/message/src/main/java/cn/hippo4j => kernel/message/api/src/main/java/cn/hippo4j/threadpool}/message/api/NotifyConfigBuilder.java (92%) rename {threadpool/message/src/main/java/cn/hippo4j/message/dto => kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api}/NotifyConfigDTO.java (95%) rename {threadpool/message/src/main/java/cn/hippo4j/message/enums => kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api}/NotifyPlatformEnum.java (95%) rename {threadpool/message/src/main/java/cn/hippo4j/message/enums => kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api}/NotifyTypeEnum.java (96%) rename {threadpool/message/src/main/java/cn/hippo4j/message/dto => kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api}/ThreadPoolNotifyDTO.java (91%) create mode 100644 kernel/message/core/pom.xml rename {threadpool/message/src/main/java/cn/hippo4j/message/platform => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/constant/DingAlarmConstants.java (97%) rename {threadpool/message/src/main/java/cn/hippo4j/message/platform => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/constant/LarkAlarmConstants.java (96%) rename {threadpool/message/src/main/java/cn/hippo4j/message/platform => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/constant/WeChatAlarmConstants.java (96%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/platform/DingSendMessageHandler.java (79%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/platform/LarkSendMessageHandler.java (86%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/platform/WeChatSendMessageHandler.java (76%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/platform/base/AbstractRobotSendMessageHandler.java (94%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/platform/base/RobotMessageActualContent.java (96%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/platform/base/RobotMessageExecuteDTO.java (91%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/request/AlarmNotifyRequest.java (93%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/request/ChangeParameterNotifyRequest.java (93%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/request/RobotAlarmNotifyRequest.java (95%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/request/RobotChangeParameterNotifyRequest.java (95%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/request/ThreadPoolNotifyRequest.java (95%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/request/WebChangeParameterNotifyRequest.java (91%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/request/base/BaseNotifyRequest.java (95%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/service/AlarmControlHandler.java (96%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/service/DefaultThreadPoolConfigChangeHandler.java (73%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/service/GlobalNotifyAlarmManage.java (96%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/service/SendMessageHandler.java (61%) rename threadpool/message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolBaseSendMessageService.java (84%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/service/ThreadPoolNotifyAlarm.java (96%) rename threadpool/message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolSendMessageService.java (80%) rename {threadpool/message/src/main/java/cn/hippo4j/message => kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core}/service/WebThreadPoolConfigChangeHandler.java (72%) rename {threadpool/message => kernel/message/core}/src/main/resources/message/robot/dynamic-thread-pool/ding-alarm.txt (100%) rename {threadpool/message => kernel/message/core}/src/main/resources/message/robot/dynamic-thread-pool/ding-config.txt (100%) rename {threadpool/message => kernel/message/core}/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-timeout-replace.json (100%) rename {threadpool/message => kernel/message/core}/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-trace-replace.json (100%) rename {threadpool/message => kernel/message/core}/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json (100%) rename {threadpool/message => kernel/message/core}/src/main/resources/message/robot/dynamic-thread-pool/lark-config.json (100%) rename {threadpool/message => kernel/message/core}/src/main/resources/message/robot/dynamic-thread-pool/wechat-alarm.txt (100%) rename {threadpool/message => kernel/message/core}/src/main/resources/message/robot/dynamic-thread-pool/wechat-config.txt (100%) rename {threadpool/message => kernel/message/core}/src/test/java/cn/hippo4j/message/service/AlarmControlHandlerTest.java (89%) create mode 100644 kernel/message/mode/config/pom.xml create mode 100644 kernel/message/mode/pom.xml create mode 100644 kernel/message/mode/server/pom.xml rename starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/{Hippo4jConfigDynamicRefreshEvent.java => ThreadPoolConfigDynamicRefreshEvent.java} (86%) rename starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/{Hippo4jConfigDynamicRefreshEventOrder.java => ThreadPoolConfigDynamicRefreshEventOrder.java} (95%) create mode 100644 threadpool/core/src/main/java/cn/hippo4j/core/extension/initialize/Hippo4jDynamicThreadPoolInitializer.java create mode 100644 threadpool/message/src/main/java/cn/hippo4j/message/config/ThreadPoolBaseSendMessageServiceInitializer.java diff --git a/agent/hippo4j-agent-plugin/adapter-plugins/dubbo-plugin/src/main/java/cn/hippo4j/agent/adapter/dubbo/DubboThreadPoolAdapter.java b/agent/hippo4j-agent-plugin/adapter-plugins/dubbo-plugin/src/main/java/cn/hippo4j/agent/adapter/dubbo/DubboThreadPoolAdapter.java index 855972ea..6ba5045e 100644 --- a/agent/hippo4j-agent-plugin/adapter-plugins/dubbo-plugin/src/main/java/cn/hippo4j/agent/adapter/dubbo/DubboThreadPoolAdapter.java +++ b/agent/hippo4j-agent-plugin/adapter-plugins/dubbo-plugin/src/main/java/cn/hippo4j/agent/adapter/dubbo/DubboThreadPoolAdapter.java @@ -17,7 +17,7 @@ package cn.hippo4j.agent.adapter.dubbo; -import cn.hippo4j.common.executor.ThreadPoolRegistry; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.model.executor.ExecutorProperties; @@ -78,7 +78,7 @@ public class DubboThreadPoolAdapter { .queueCapacity(executor.getQueue().remainingCapacity()) .rejectedHandler(RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName(executor.getRejectedExecutionHandler().getClass().getSimpleName()).getName()) .build(); - ThreadPoolRegistry.putHolder(executorName, executor, executorProperties); + ThreadPoolExecutorRegistry.putHolder(executorName, executor, executorProperties); } public static String mark() { diff --git a/agent/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringThreadPoolRegisterSupport.java b/agent/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringThreadPoolRegisterSupport.java index b2d08574..8439d567 100644 --- a/agent/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringThreadPoolRegisterSupport.java +++ b/agent/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringThreadPoolRegisterSupport.java @@ -18,7 +18,7 @@ package cn.hippo4j.agent.plugin.spring.common.support; import cn.hippo4j.agent.core.util.ReflectUtil; -import cn.hippo4j.common.executor.ThreadPoolRegistry; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.model.executor.ExecutorProperties; @@ -41,7 +41,7 @@ public class SpringThreadPoolRegisterSupport { private static final Logger LOGGER = LoggerFactory.getLogger(SpringThreadPoolRegisterSupport.class); public static void registerThreadPoolInstances(ApplicationContext context) { - Map> referencedClassMap = ThreadPoolRegistry.REFERENCED_CLASS_MAP; + Map> referencedClassMap = ThreadPoolExecutorRegistry.REFERENCED_CLASS_MAP; for (Map.Entry> entry : referencedClassMap.entrySet()) { ThreadPoolExecutor enhancedInstance = entry.getKey(); Class declaredClass = entry.getValue(); @@ -93,6 +93,6 @@ public class SpringThreadPoolRegisterSupport { .queueCapacity(executor.getQueue().remainingCapacity()) .rejectedHandler(RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName(executor.getRejectedExecutionHandler().getClass().getSimpleName()).getName()) .build(); - ThreadPoolRegistry.putHolder(threadPoolId, executor, executorProperties); + ThreadPoolExecutorRegistry.putHolder(threadPoolId, executor, executorProperties); } } diff --git a/agent/hippo4j-agent-plugin/threadpool-plugin/src/main/java/cn/hippo4j/agent/plugin/thread/pool/interceptor/ThreadPoolExecutorConstructorMethodInterceptor.java b/agent/hippo4j-agent-plugin/threadpool-plugin/src/main/java/cn/hippo4j/agent/plugin/thread/pool/interceptor/ThreadPoolExecutorConstructorMethodInterceptor.java index 343d05d2..fb4194c8 100644 --- a/agent/hippo4j-agent-plugin/threadpool-plugin/src/main/java/cn/hippo4j/agent/plugin/thread/pool/interceptor/ThreadPoolExecutorConstructorMethodInterceptor.java +++ b/agent/hippo4j-agent-plugin/threadpool-plugin/src/main/java/cn/hippo4j/agent/plugin/thread/pool/interceptor/ThreadPoolExecutorConstructorMethodInterceptor.java @@ -22,7 +22,7 @@ import cn.hippo4j.agent.core.logging.api.ILog; import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance; import cn.hippo4j.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; -import cn.hippo4j.common.executor.ThreadPoolRegistry; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.agent.core.util.CollectionUtil; import cn.hippo4j.agent.core.util.StringUtil; import java.util.ArrayList; @@ -52,7 +52,7 @@ public class ThreadPoolExecutorConstructorMethodInterceptor implements InstanceC StackTraceElement declaredClassStackTraceElement = stackTraceElements.get(0); String declaredClassName = declaredClassStackTraceElement.getClassName(); Class declaredClass = Thread.currentThread().getContextClassLoader().loadClass(declaredClassName); - ThreadPoolRegistry.REFERENCED_CLASS_MAP.put((ThreadPoolExecutor) objInst, declaredClass); + ThreadPoolExecutorRegistry.REFERENCED_CLASS_MAP.put((ThreadPoolExecutor) objInst, declaredClass); } private List getStackTraceElements() { diff --git a/examples/threadpool-example/config/config-nacos/src/main/resources/bootstrap.properties b/examples/threadpool-example/config/config-nacos/src/main/resources/bootstrap.properties index ab1e8b01..ae7b0afb 100644 --- a/examples/threadpool-example/config/config-nacos/src/main/resources/bootstrap.properties +++ b/examples/threadpool-example/config/config-nacos/src/main/resources/bootstrap.properties @@ -25,11 +25,11 @@ spring.dynamic.thread-pool.monitor.initial-delay=10000 spring.dynamic.thread-pool.monitor.collect-interval=5000 spring.dynamic.thread-pool.notify-platforms[0].platform=WECHAT -spring.dynamic.thread-pool.notify-platforms[0].token=ac0426a5-c712-474c-9bff-72b8b8f5caff -spring.dynamic.thread-pool.notify-platforms[1].platform=DING -spring.dynamic.thread-pool.notify-platforms[1].token=56417ebba6a27ca352f0de77a2ae9da66d01f39610b5ee8a6033c60ef9071c55 -spring.dynamic.thread-pool.notify-platforms[2].platform=LARK -spring.dynamic.thread-pool.notify-platforms[2].token=2cbf2808-3839-4c26-a04d-fd201dd51f9e +spring.dynamic.thread-pool.notify-platforms[0].token=ec3be378-6c99-45d2-a147-b400c7e94a08 +# spring.dynamic.thread-pool.notify-platforms[1].platform=DING +# spring.dynamic.thread-pool.notify-platforms[1].token=56417ebba6a27ca352f0de77a2ae9da66d01f39610b5ee8a6033c60ef9071c55 +# spring.dynamic.thread-pool.notify-platforms[2].platform=LARK +# spring.dynamic.thread-pool.notify-platforms[2].token=2cbf2808-3839-4c26-a04d-fd201dd51f9e spring.dynamic.thread-pool.nacos.data-id=hippo4j-nacos.yaml spring.dynamic.thread-pool.nacos.group=DEFAULT_GROUP diff --git a/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/handler/TaskTraceBuilderHandler.java b/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/handler/TaskTraceBuilderHandler.java index 5098a8f2..6e95a969 100644 --- a/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/handler/TaskTraceBuilderHandler.java +++ b/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/handler/TaskTraceBuilderHandler.java @@ -18,7 +18,7 @@ package cn.hippo4j.example.core.handler; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.core.toolkit.ExecutorTraceContextUtil; +import cn.hippo4j.threadpool.alarm.toolkit.ExecutorTraceContextUtil; import org.slf4j.MDC; import org.springframework.core.task.TaskDecorator; diff --git a/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/AlarmSendMessageTest.java b/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/AlarmSendMessageTest.java index 7d57ee65..b26b6b01 100644 --- a/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/AlarmSendMessageTest.java +++ b/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/AlarmSendMessageTest.java @@ -17,9 +17,9 @@ package cn.hippo4j.example.core.inittest; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.example.core.constant.GlobalTestConstant; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -47,8 +47,8 @@ public class AlarmSendMessageTest { public void alarmSendMessageTest() { ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); scheduledThreadPool.scheduleWithFixedDelay(() -> { - DynamicThreadPoolWrapper poolWrapper = GlobalThreadPoolManage.getExecutorService(GlobalTestConstant.MESSAGE_PRODUCE); - ThreadPoolExecutor poolExecutor = poolWrapper.getExecutor(); + ThreadPoolExecutorHolder executorHolder = ThreadPoolExecutorRegistry.getHolder(GlobalTestConstant.MESSAGE_PRODUCE); + ThreadPoolExecutor poolExecutor = executorHolder.getExecutor(); try { poolExecutor.execute(() -> { try { diff --git a/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/RegisterDynamicThreadPoolTest.java b/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/RegisterDynamicThreadPoolTest.java index 79a815e0..23892224 100644 --- a/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/RegisterDynamicThreadPoolTest.java +++ b/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/RegisterDynamicThreadPoolTest.java @@ -24,7 +24,7 @@ import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper; import cn.hippo4j.common.model.register.notify.DynamicThreadPoolRegisterCoreNotifyParameter; import cn.hippo4j.common.model.register.notify.DynamicThreadPoolRegisterServerNotifyParameter; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.message.enums.NotifyPlatformEnum; +import cn.hippo4j.threadpool.message.api.NotifyPlatformEnum; import lombok.extern.slf4j.Slf4j; import java.util.concurrent.ThreadPoolExecutor; diff --git a/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/TaskDecoratorTest.java b/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/TaskDecoratorTest.java index dedc14bc..e9da08f1 100644 --- a/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/TaskDecoratorTest.java +++ b/examples/threadpool-example/example-core/src/main/java/cn/hippo4j/example/core/inittest/TaskDecoratorTest.java @@ -17,9 +17,9 @@ package cn.hippo4j.example.core.inittest; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.example.core.constant.GlobalTestConstant; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; import org.springframework.core.task.TaskDecorator; @@ -63,8 +63,8 @@ public class TaskDecoratorTest { MDC.put(PLACEHOLDER, "View the official website: https://www.hippo4j.cn"); try { Thread.sleep(SLEEP_TIME); - DynamicThreadPoolWrapper poolWrapper = GlobalThreadPoolManage.getExecutorService(GlobalTestConstant.MESSAGE_PRODUCE); - ThreadPoolExecutor threadPoolExecutor = poolWrapper.getExecutor(); + ThreadPoolExecutorHolder executorHolder = ThreadPoolExecutorRegistry.getHolder(GlobalTestConstant.MESSAGE_PRODUCE); + ThreadPoolExecutor threadPoolExecutor = executorHolder.getExecutor(); threadPoolExecutor.execute(() -> log.info("Pass context via taskDecorator MDC: {}", MDC.get(PLACEHOLDER))); } catch (InterruptedException e) { throw new RuntimeException(e); diff --git a/examples/threadpool-example/server/adapter-kafka/pom.xml b/examples/threadpool-example/server/adapter-kafka/pom.xml index 7be2e924..b37b9396 100644 --- a/examples/threadpool-example/server/adapter-kafka/pom.xml +++ b/examples/threadpool-example/server/adapter-kafka/pom.xml @@ -33,7 +33,7 @@ cn.hippo4j - hippo4j-threadpool-server-spring-boot-starter + hippo4j-threadpool-spring-boot-starter ${project.version} diff --git a/examples/threadpool-example/server/adapter-rabbitmq/pom.xml b/examples/threadpool-example/server/adapter-rabbitmq/pom.xml index 58d075fe..fd0fd5ca 100644 --- a/examples/threadpool-example/server/adapter-rabbitmq/pom.xml +++ b/examples/threadpool-example/server/adapter-rabbitmq/pom.xml @@ -33,7 +33,7 @@ cn.hippo4j - hippo4j-threadpool-server-spring-boot-starter + hippo4j-threadpool-spring-boot-starter ${project.version} diff --git a/examples/threadpool-example/server/adapter-rocketmq/pom.xml b/examples/threadpool-example/server/adapter-rocketmq/pom.xml index 756186c9..04e533d3 100644 --- a/examples/threadpool-example/server/adapter-rocketmq/pom.xml +++ b/examples/threadpool-example/server/adapter-rocketmq/pom.xml @@ -33,7 +33,7 @@ cn.hippo4j - hippo4j-threadpool-server-spring-boot-starter + hippo4j-threadpool-spring-boot-starter ${project.version} diff --git a/examples/threadpool-example/server/adapter-stream-rabbitmq/pom.xml b/examples/threadpool-example/server/adapter-stream-rabbitmq/pom.xml index 3f22f7be..8950dbf8 100644 --- a/examples/threadpool-example/server/adapter-stream-rabbitmq/pom.xml +++ b/examples/threadpool-example/server/adapter-stream-rabbitmq/pom.xml @@ -33,7 +33,7 @@ cn.hippo4j - hippo4j-threadpool-server-spring-boot-starter + hippo4j-threadpool-spring-boot-starter ${project.version} diff --git a/examples/threadpool-example/server/adapter-stream-rocketmq/pom.xml b/examples/threadpool-example/server/adapter-stream-rocketmq/pom.xml index df5b1b39..279beee3 100644 --- a/examples/threadpool-example/server/adapter-stream-rocketmq/pom.xml +++ b/examples/threadpool-example/server/adapter-stream-rocketmq/pom.xml @@ -37,7 +37,7 @@ cn.hippo4j - hippo4j-threadpool-server-spring-boot-starter + hippo4j-threadpool-spring-boot-starter ${project.version} diff --git a/examples/threadpool-example/server/server/pom.xml b/examples/threadpool-example/server/server/pom.xml index a3c42c3e..f1972f2b 100644 --- a/examples/threadpool-example/server/server/pom.xml +++ b/examples/threadpool-example/server/server/pom.xml @@ -22,7 +22,7 @@ cn.hippo4j - hippo4j-threadpool-server-spring-boot-starter + hippo4j-threadpool-spring-boot-starter ${revision} diff --git a/infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolExecutorHolder.java b/infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolExecutorHolder.java index a23eff55..0569e683 100644 --- a/infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolExecutorHolder.java +++ b/infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolExecutorHolder.java @@ -17,6 +17,7 @@ package cn.hippo4j.common.executor; +import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.model.executor.ExecutorProperties; import lombok.Data; import lombok.NoArgsConstructor; @@ -35,6 +36,12 @@ public class ThreadPoolExecutorHolder { private ExecutorProperties executorProperties; + /** + * The Server mode is used to compare whether the parameters have changed, + * and consider refactoring later + */ + private ThreadPoolParameterInfo parameterInfo; + public ThreadPoolExecutorHolder(String threadPoolId, ThreadPoolExecutor executor, ExecutorProperties executorProperties) { this.threadPoolId = threadPoolId; this.executor = executor; diff --git a/infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolRegistry.java b/infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolExecutorRegistry.java similarity index 67% rename from infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolRegistry.java rename to infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolExecutorRegistry.java index bff59cf0..f785c3ce 100644 --- a/infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolRegistry.java +++ b/infra/common/src/main/java/cn/hippo4j/common/executor/ThreadPoolExecutorRegistry.java @@ -21,13 +21,15 @@ import cn.hippo4j.common.model.executor.ExecutorProperties; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadPoolExecutor; @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class ThreadPoolRegistry { +public class ThreadPoolExecutorRegistry { private static final Map HOLDER_MAP = new ConcurrentHashMap<>(); @@ -38,8 +40,12 @@ public class ThreadPoolRegistry { } public static void putHolder(String executorName, ThreadPoolExecutor executor, ExecutorProperties executorProperties) { - ThreadPoolExecutorHolder holder = new ThreadPoolExecutorHolder(executorName, executor, executorProperties); - HOLDER_MAP.put(executorName, holder); + ThreadPoolExecutorHolder executorHolder = new ThreadPoolExecutorHolder(executorName, executor, executorProperties); + HOLDER_MAP.put(executorHolder.getThreadPoolId(), executorHolder); + } + + public static void putHolder(ThreadPoolExecutorHolder executorHolder) { + HOLDER_MAP.put(executorHolder.getThreadPoolId(), executorHolder); } public static ThreadPoolExecutorHolder getHolder(String executorName) { @@ -49,4 +55,19 @@ public class ThreadPoolRegistry { public static Map> getReferencedClassMap() { return REFERENCED_CLASS_MAP; } + + public static List listThreadPoolExecutorId() { + return new ArrayList<>(HOLDER_MAP.keySet()); + } + + /** + * Get the number of dynamic thread pools. + *

The data may be inaccurate when the project is initially + * launched because registration is done asynchronously. + * + * @return thread-pool size + */ + public static Integer getThreadPoolExecutorSize() { + return listThreadPoolExecutorId().size(); + } } diff --git a/infra/common/src/main/java/cn/hippo4j/common/propertie/EnvironmentProperties.java b/infra/common/src/main/java/cn/hippo4j/common/propertie/EnvironmentProperties.java new file mode 100644 index 00000000..c874df49 --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/propertie/EnvironmentProperties.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.common.propertie; + +/** + * Configurable environment properties. + */ +public final class EnvironmentProperties { + + /** + * spring.profiles.active + */ + public static String active = "UNKNOWN"; + + /** + * spring.dynamic.thread-pool.item-id + */ + public static String itemId; + + /** + * spring.application.name + */ + public static String applicationName; + + /** + * spring.dynamic.thread-pool.check-state-interval + */ + public static Long checkStateInterval; +} diff --git a/infra/common/src/main/java/cn/hippo4j/common/propertie/IdentifyProperties.java b/infra/common/src/main/java/cn/hippo4j/common/propertie/IdentifyProperties.java new file mode 100644 index 00000000..0382e6b7 --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/propertie/IdentifyProperties.java @@ -0,0 +1,32 @@ +/* + * 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.propertie; + +import lombok.Setter; + +/** + * Identify properties. + */ +public class IdentifyProperties { + + /** + * The unique identifier of the client, initialized when the project starts + */ + @Setter + public static String IDENTIFY; +} diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/MessageConvert.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/MessageConvert.java index 6a5d1fbc..fe8f0d3c 100644 --- a/infra/common/src/main/java/cn/hippo4j/common/toolkit/MessageConvert.java +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/MessageConvert.java @@ -24,22 +24,18 @@ import java.util.Map; import cn.hippo4j.common.monitor.AbstractMessage; import cn.hippo4j.common.monitor.Message; import cn.hippo4j.common.monitor.MessageWrapper; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import lombok.SneakyThrows; /** * Message convert. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class MessageConvert { - private MessageConvert(){ - - } - /** * {@link Message} to {@link MessageWrapper}. - * - * @param message - * @return */ public static MessageWrapper convert(Message message) { MessageWrapper wrapper = new MessageWrapper(); @@ -58,9 +54,6 @@ public class MessageConvert { /** * {@link MessageWrapper} to {@link Message}. - * - * @param messageWrapper - * @return */ @SneakyThrows public static Message convert(MessageWrapper messageWrapper) { diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/IoUtilTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/IoUtilTest.java index 0f48c046..c9091285 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/toolkit/IoUtilTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/IoUtilTest.java @@ -49,6 +49,7 @@ import java.util.zip.GZIPOutputStream; * test for {@link IoUtil} */ public class IoUtilTest { + private Path tempDir; private File sourceFile; diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/JacksonHandlerTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/JacksonHandlerTest.java index 907a459c..3acec9fa 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/toolkit/JacksonHandlerTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/JacksonHandlerTest.java @@ -87,8 +87,7 @@ public class JacksonHandlerTest { Assertions.assertEquals( EXPECTED_ENTITY, JACKSON_HANDLER.parseObject(EXPECTED_ENTITY_JSON, new TypeReference() { - }) - ); + })); // illegal json Assertions.assertThrows(MismatchedInputException.class, () -> JACKSON_HANDLER.parseObject(" ", Entity.class)); diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java index 88677d9f..52470aad 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java @@ -93,7 +93,7 @@ public class MessageConvertTest { @Test public void testMessageWrapperConvertException() { - Assertions.assertThrows(Exception.class, ()->{ + Assertions.assertThrows(Exception.class, () -> { Map data1 = new HashMap<>(); data1.put("key1", "value1"); data1.put("key2", 123); diff --git a/kernel/alarm/pom.xml b/kernel/alarm/pom.xml index 0bb19b1c..ea871060 100644 --- a/kernel/alarm/pom.xml +++ b/kernel/alarm/pom.xml @@ -10,4 +10,17 @@ hippo4j-threadpool-kernel-alarm + + + + org.projectlombok + lombok + + + + cn.hippo4j + hippo4j-threadpool-message-core + ${project.version} + + \ No newline at end of file diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/api/ThreadPoolCheckAlarm.java b/kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/api/ThreadPoolCheckAlarm.java similarity index 94% rename from threadpool/core/src/main/java/cn/hippo4j/core/api/ThreadPoolCheckAlarm.java rename to kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/api/ThreadPoolCheckAlarm.java index 25e32097..e48c47d6 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/api/ThreadPoolCheckAlarm.java +++ b/kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/api/ThreadPoolCheckAlarm.java @@ -15,12 +15,11 @@ * limitations under the License. */ -package cn.hippo4j.core.api; +package cn.hippo4j.threadpool.alarm.api; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.CommandLineRunner; import java.util.concurrent.ThreadPoolExecutor; @@ -30,7 +29,7 @@ import java.util.concurrent.ThreadPoolExecutor; *

Dynamic thread pool check and send logic wait for refactoring, * Try not to rely on this component for custom extensions, because it is undefined. */ -public interface ThreadPoolCheckAlarm extends CommandLineRunner { +public interface ThreadPoolCheckAlarm { /** * Get a none thread pool check alarm. @@ -42,6 +41,12 @@ public interface ThreadPoolCheckAlarm extends CommandLineRunner { return NoneThreadPoolCheckAlarm.INSTANCE; } + /** + * Execute scheduled tasks to scan the running status of the thread pool. + */ + default void scheduleExecute() { + } + /** * Check pool capacity alarm. * @@ -133,10 +138,5 @@ public interface ThreadPoolCheckAlarm extends CommandLineRunner { public void asyncSendExecuteTimeOutAlarm(String threadPoolId, long executeTime, long executeTimeOut, ThreadPoolExecutor threadPoolExecutor) { log.debug("Ignore async send execute time out alarm for ExecuteService '{}'", threadPoolId); } - - @Override - public void run(String... args) throws Exception { - // do nothing - } } } diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/DefaultThreadPoolCheckAlarmHandler.java b/kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/handler/DefaultThreadPoolCheckAlarmHandler.java similarity index 64% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/DefaultThreadPoolCheckAlarmHandler.java rename to kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/handler/DefaultThreadPoolCheckAlarmHandler.java index d5098b4c..9bf93f81 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/DefaultThreadPoolCheckAlarmHandler.java +++ b/kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/handler/DefaultThreadPoolCheckAlarmHandler.java @@ -15,22 +15,22 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.alarm.handler; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.toolkit.CalculateUtil; +import cn.hippo4j.common.toolkit.ReflectUtil; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.core.executor.support.ThreadPoolBuilder; -import cn.hippo4j.core.toolkit.ExecutorTraceContextUtil; -import cn.hippo4j.core.toolkit.IdentifyUtil; -import cn.hippo4j.message.enums.NotifyTypeEnum; -import cn.hippo4j.message.request.AlarmNotifyRequest; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; +import cn.hippo4j.threadpool.alarm.toolkit.ExecutorTraceContextUtil; +import cn.hippo4j.threadpool.message.core.request.AlarmNotifyRequest; +import cn.hippo4j.threadpool.message.api.NotifyTypeEnum; +import cn.hippo4j.threadpool.message.core.service.GlobalNotifyAlarmManage; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolNotifyAlarm; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolSendMessageService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import java.util.List; import java.util.Objects; @@ -40,8 +40,16 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import static cn.hippo4j.common.propertie.EnvironmentProperties.active; +import static cn.hippo4j.common.propertie.EnvironmentProperties.applicationName; +import static cn.hippo4j.common.propertie.EnvironmentProperties.checkStateInterval; +import static cn.hippo4j.common.propertie.EnvironmentProperties.itemId; +import static cn.hippo4j.common.propertie.IdentifyProperties.IDENTIFY; /** * Default thread-pool check alarm handler. @@ -50,46 +58,42 @@ import java.util.concurrent.TimeUnit; @RequiredArgsConstructor public class DefaultThreadPoolCheckAlarmHandler implements Runnable, ThreadPoolCheckAlarm { - private final Hippo4jSendMessageService hippo4jSendMessageService; - - @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; - - @Value("${spring.dynamic.thread-pool.check-state-interval:5}") - private Integer checkStateInterval; + private final ThreadPoolSendMessageService threadPoolSendMessageService; private final ScheduledExecutorService alarmNotifyExecutor = new ScheduledThreadPoolExecutor( 1, r -> new Thread(r, "client.alarm.notify")); - private final ExecutorService asyncAlarmNotifyExecutor = ThreadPoolBuilder.builder() - .poolThreadSize(2, 4) - .threadFactory("client.execute.timeout.alarm") - .allowCoreThreadTimeOut(true) - .keepAliveTime(60L, TimeUnit.SECONDS) - .workQueue(new LinkedBlockingQueue(4096)) - .rejected(new ThreadPoolExecutor.AbortPolicy()) - .build(); + private final ExecutorService asyncAlarmNotifyExecutor = new ThreadPoolExecutor( + 2, + 4, + 60L, + TimeUnit.SECONDS, + new LinkedBlockingQueue<>(4096), + new ThreadFactory() { + + private final AtomicInteger count = new AtomicInteger(); + + @Override + public Thread newThread(Runnable r) { + return new Thread("client.execute.timeout.alarm_" + count.incrementAndGet()); + } + }, + new ThreadPoolExecutor.AbortPolicy()); @Override - public void run(String... args) throws Exception { + public void scheduleExecute() { alarmNotifyExecutor.scheduleWithFixedDelay(this, 0, checkStateInterval, TimeUnit.SECONDS); } @Override public void run() { - List listThreadPoolId = GlobalThreadPoolManage.listThreadPoolId(); + List listThreadPoolId = ThreadPoolExecutorRegistry.listThreadPoolExecutorId(); listThreadPoolId.forEach(threadPoolId -> { ThreadPoolNotifyAlarm threadPoolNotifyAlarm = GlobalNotifyAlarmManage.get(threadPoolId); if (threadPoolNotifyAlarm != null && threadPoolNotifyAlarm.getAlarm()) { - DynamicThreadPoolWrapper wrapper = GlobalThreadPoolManage.getExecutorService(threadPoolId); - ThreadPoolExecutor executor = wrapper.getExecutor(); + ThreadPoolExecutorHolder executorHolder = ThreadPoolExecutorRegistry.getHolder(threadPoolId); + ThreadPoolExecutor executor = executorHolder.getExecutor(); checkPoolCapacityAlarm(threadPoolId, executor); checkPoolActivityAlarm(threadPoolId, executor); } @@ -116,7 +120,7 @@ public class DefaultThreadPoolCheckAlarmHandler implements Runnable, ThreadPoolC if (isSend) { AlarmNotifyRequest alarmNotifyRequest = buildAlarmNotifyRequest(threadPoolExecutor); alarmNotifyRequest.setThreadPoolId(threadPoolId); - hippo4jSendMessageService.sendAlarmMessage(NotifyTypeEnum.CAPACITY, alarmNotifyRequest); + threadPoolSendMessageService.sendAlarmMessage(NotifyTypeEnum.CAPACITY, alarmNotifyRequest); } } @@ -139,7 +143,7 @@ public class DefaultThreadPoolCheckAlarmHandler implements Runnable, ThreadPoolC if (isSend) { AlarmNotifyRequest alarmNotifyRequest = buildAlarmNotifyRequest(threadPoolExecutor); alarmNotifyRequest.setThreadPoolId(threadPoolId); - hippo4jSendMessageService.sendAlarmMessage(NotifyTypeEnum.ACTIVITY, alarmNotifyRequest); + threadPoolSendMessageService.sendAlarmMessage(NotifyTypeEnum.ACTIVITY, alarmNotifyRequest); } } @@ -155,11 +159,11 @@ public class DefaultThreadPoolCheckAlarmHandler implements Runnable, ThreadPoolC if (Objects.isNull(alarmConfig) || !alarmConfig.getAlarm()) { return; } - ThreadPoolExecutor threadPoolExecutor = GlobalThreadPoolManage.getExecutorService(threadPoolId).getExecutor(); - if (threadPoolExecutor instanceof DynamicThreadPoolExecutor) { + ThreadPoolExecutor threadPoolExecutor = ThreadPoolExecutorRegistry.getHolder(threadPoolId).getExecutor(); + if (Objects.equals(threadPoolExecutor.getClass().getName(), "cn.hippo4j.core.executor.DynamicThreadPoolExecutor")) { AlarmNotifyRequest alarmNotifyRequest = buildAlarmNotifyRequest(threadPoolExecutor); alarmNotifyRequest.setThreadPoolId(threadPoolId); - hippo4jSendMessageService.sendAlarmMessage(NotifyTypeEnum.REJECT, alarmNotifyRequest); + threadPoolSendMessageService.sendAlarmMessage(NotifyTypeEnum.REJECT, alarmNotifyRequest); } }; asyncAlarmNotifyExecutor.execute(checkPoolRejectedAlarmTask); @@ -179,21 +183,19 @@ public class DefaultThreadPoolCheckAlarmHandler implements Runnable, ThreadPoolC if (Objects.isNull(alarmConfig) || !alarmConfig.getAlarm()) { return; } - if (threadPoolExecutor instanceof DynamicThreadPoolExecutor) { - try { - AlarmNotifyRequest alarmNotifyRequest = buildAlarmNotifyRequest(threadPoolExecutor); - alarmNotifyRequest.setThreadPoolId(threadPoolId); - alarmNotifyRequest.setExecuteTime(executeTime); - alarmNotifyRequest.setExecuteTimeOut(executeTimeOut); - String executeTimeoutTrace = ExecutorTraceContextUtil.getAndRemoveTimeoutTrace(); - if (StringUtil.isNotBlank(executeTimeoutTrace)) { - alarmNotifyRequest.setExecuteTimeoutTrace(executeTimeoutTrace); - } - Runnable task = () -> hippo4jSendMessageService.sendAlarmMessage(NotifyTypeEnum.TIMEOUT, alarmNotifyRequest); - asyncAlarmNotifyExecutor.execute(task); - } catch (Throwable ex) { - log.error("Send thread pool execution timeout alarm error.", ex); + try { + AlarmNotifyRequest alarmNotifyRequest = buildAlarmNotifyRequest(threadPoolExecutor); + alarmNotifyRequest.setThreadPoolId(threadPoolId); + alarmNotifyRequest.setExecuteTime(executeTime); + alarmNotifyRequest.setExecuteTimeOut(executeTimeOut); + String executeTimeoutTrace = ExecutorTraceContextUtil.getAndRemoveTimeoutTrace(); + if (StringUtil.isNotBlank(executeTimeoutTrace)) { + alarmNotifyRequest.setExecuteTimeoutTrace(executeTimeoutTrace); } + Runnable task = () -> threadPoolSendMessageService.sendAlarmMessage(NotifyTypeEnum.TIMEOUT, alarmNotifyRequest); + asyncAlarmNotifyExecutor.execute(task); + } catch (Throwable ex) { + log.error("Send thread pool execution timeout alarm error.", ex); } } @@ -206,13 +208,17 @@ public class DefaultThreadPoolCheckAlarmHandler implements Runnable, ThreadPoolC public AlarmNotifyRequest buildAlarmNotifyRequest(ThreadPoolExecutor threadPoolExecutor) { BlockingQueue blockingQueue = threadPoolExecutor.getQueue(); RejectedExecutionHandler rejectedExecutionHandler = threadPoolExecutor.getRejectedExecutionHandler(); - long rejectCount = threadPoolExecutor instanceof DynamicThreadPoolExecutor - ? ((DynamicThreadPoolExecutor) threadPoolExecutor).getRejectCountNum() - : -1L; + long rejectCount = -1L; + if (Objects.equals(threadPoolExecutor.getClass().getName(), "cn.hippo4j.core.executor.DynamicThreadPoolExecutor")) { + Object actualRejectCountNum = ReflectUtil.invoke(threadPoolExecutor, "getRejectCountNum"); + if (actualRejectCountNum != null) { + rejectCount = (long) actualRejectCountNum; + } + } return AlarmNotifyRequest.builder() .appName(StringUtil.isBlank(itemId) ? applicationName : itemId) .active(active.toUpperCase()) - .identify(IdentifyUtil.getIdentify()) + .identify(IDENTIFY) .corePoolSize(threadPoolExecutor.getCorePoolSize()) .maximumPoolSize(threadPoolExecutor.getMaximumPoolSize()) .poolSize(threadPoolExecutor.getPoolSize()) diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/ExecutorTraceContextUtil.java b/kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/toolkit/ExecutorTraceContextUtil.java similarity index 97% rename from threadpool/core/src/main/java/cn/hippo4j/core/toolkit/ExecutorTraceContextUtil.java rename to kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/toolkit/ExecutorTraceContextUtil.java index b7da8497..e9cca2fb 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/ExecutorTraceContextUtil.java +++ b/kernel/alarm/src/main/java/cn/hippo4j/threadpool/alarm/toolkit/ExecutorTraceContextUtil.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.core.toolkit; +package cn.hippo4j.threadpool.alarm.toolkit; import org.slf4j.MDC; diff --git a/kernel/dynamic/core/src/main/java/cn/hippo4j/threadpool/dynamic/core/executor/manage/GlobalConfigThreadPoolManage.java b/kernel/dynamic/core/src/main/java/cn/hippo4j/threadpool/dynamic/core/executor/manage/GlobalConfigThreadPoolManage.java deleted file mode 100644 index 4911ac05..00000000 --- a/kernel/dynamic/core/src/main/java/cn/hippo4j/threadpool/dynamic/core/executor/manage/GlobalConfigThreadPoolManage.java +++ /dev/null @@ -1,62 +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.threadpool.dynamic.core.executor.manage; - -import cn.hippo4j.common.model.executor.ExecutorProperties; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Global core thread-pool manage. - * TODO - */ -public class GlobalConfigThreadPoolManage { - - private static final Map EXECUTOR_PROPERTIES = new ConcurrentHashMap<>(); - - /** - * Get properties. - * - * @param threadPoolId - * @return - */ - public static ExecutorProperties getProperties(String threadPoolId) { - return EXECUTOR_PROPERTIES.get(threadPoolId); - } - - /** - * Register. - * - * @param threadPoolId - * @param executorProperties - */ - public static void register(String threadPoolId, ExecutorProperties executorProperties) { - EXECUTOR_PROPERTIES.put(threadPoolId, executorProperties); - } - - /** - * Refresh. - * - * @param threadPoolId - * @param executorProperties - */ - public static void refresh(String threadPoolId, ExecutorProperties executorProperties) { - EXECUTOR_PROPERTIES.put(threadPoolId, executorProperties); - } -} diff --git a/kernel/dynamic/core/src/main/java/cn/hippo4j/threadpool/dynamic/core/executor/manage/GlobalThreadPoolManage.java b/kernel/dynamic/core/src/main/java/cn/hippo4j/threadpool/dynamic/core/executor/manage/GlobalThreadPoolManage.java deleted file mode 100644 index de4cc35e..00000000 --- a/kernel/dynamic/core/src/main/java/cn/hippo4j/threadpool/dynamic/core/executor/manage/GlobalThreadPoolManage.java +++ /dev/null @@ -1,70 +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.threadpool.dynamic.core.executor.manage; - -import cn.hippo4j.common.model.ThreadPoolParameter; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ThreadPoolExecutor; - -/** - * Global thread-pool manage. - */ -public class GlobalThreadPoolManage { - - /** - * Dynamic thread pool parameter container. - */ - private static final Map POOL_PARAMETER = new ConcurrentHashMap(); - - /** - * Dynamic thread pool wrapper. - */ - private static final Map EXECUTOR_MAP = new ConcurrentHashMap(); - - /** - * Get the dynamic thread pool class. - * - * @param threadPoolId thread-pool id - * @return dynamic thread-pool - */ - public static ThreadPoolExecutor getExecutorService(String threadPoolId) { - return EXECUTOR_MAP.get(threadPoolId); - } - - /** - * Get dynamic thread pool parameters. - * - * @param threadPoolId thread-pool id - * @return thread-pool parameter - */ - public static ThreadPoolParameter getPoolParameter(String threadPoolId) { - return POOL_PARAMETER.get(threadPoolId); - } - - /** - * Register dynamic thread pool parameters. - * - * @param threadPoolId thread-pool id - * @param threadPoolParameter thread-pool parameter - */ - public static void registerPoolParameter(String threadPoolId, ThreadPoolParameter threadPoolParameter) { - POOL_PARAMETER.put(threadPoolId, threadPoolParameter); - } -} diff --git a/kernel/dynamic/mode/config/pom.xml b/kernel/dynamic/mode/config/pom.xml index ed934787..c6e157a3 100644 --- a/kernel/dynamic/mode/config/pom.xml +++ b/kernel/dynamic/mode/config/pom.xml @@ -22,5 +22,9 @@ hippo4j-threadpool-dynamic-core ${project.version} + + org.springframework + spring-beans + \ No newline at end of file diff --git a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/YamlConfigParser.java b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/YamlConfigParser.java index 1f0c0d3f..b929f309 100644 --- a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/YamlConfigParser.java +++ b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/YamlConfigParser.java @@ -18,7 +18,11 @@ package cn.hippo4j.threadpool.dynamic.mode.config.parser; import cn.hippo4j.common.toolkit.CollectionUtil; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.io.ByteArrayResource; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,12 +33,12 @@ public class YamlConfigParser extends AbstractConfigParser { @Override public Map doParse(String content) { - // TODO - /* - * if (StringUtils.isEmpty(content)) { return new HashMap<>(1); } YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); - * yamlPropertiesFactoryBean.setResources(new ByteArrayResource(content.getBytes())); return yamlPropertiesFactoryBean.getObject(); - */ - return null; + if (StringUtils.isEmpty(content)) { + return new HashMap<>(1); + } + YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); + yamlPropertiesFactoryBean.setResources(new ByteArrayResource(content.getBytes())); + return yamlPropertiesFactoryBean.getObject(); } @Override diff --git a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/refresher/event/DynamicThreadPoolRefreshListener.java b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/refresher/event/DynamicThreadPoolRefreshListener.java index 91df114f..7a49151d 100644 --- a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/refresher/event/DynamicThreadPoolRefreshListener.java +++ b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/refresher/event/DynamicThreadPoolRefreshListener.java @@ -18,7 +18,7 @@ package cn.hippo4j.threadpool.dynamic.mode.config.refresher.event; import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; -import cn.hippo4j.common.executor.ThreadPoolRegistry; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.executor.support.ResizableCapacityLinkedBlockingQueue; @@ -55,7 +55,7 @@ public class DynamicThreadPoolRefreshListener implements Observer + + 4.0.0 + + cn.hippo4j + hippo4j-threadpool-kernel-message + ${revision} + + + hippo4j-threadpool-message-api + + + + org.projectlombok + lombok + + + diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/dto/AlarmControlDTO.java b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/AlarmControlDTO.java similarity index 93% rename from threadpool/message/src/main/java/cn/hippo4j/message/dto/AlarmControlDTO.java rename to kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/AlarmControlDTO.java index 73cb7314..6e4338d4 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/dto/AlarmControlDTO.java +++ b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/AlarmControlDTO.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.message.dto; +package cn.hippo4j.threadpool.message.api; -import cn.hippo4j.message.enums.NotifyTypeEnum; +import cn.hippo4j.threadpool.message.api.NotifyTypeEnum; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/api/NotifyConfigBuilder.java b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyConfigBuilder.java similarity index 92% rename from threadpool/message/src/main/java/cn/hippo4j/message/api/NotifyConfigBuilder.java rename to kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyConfigBuilder.java index 372c5d24..37943802 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/api/NotifyConfigBuilder.java +++ b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyConfigBuilder.java @@ -15,9 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.api; - -import cn.hippo4j.message.dto.NotifyConfigDTO; +package cn.hippo4j.threadpool.message.api; import java.util.List; import java.util.Map; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/dto/NotifyConfigDTO.java b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyConfigDTO.java similarity index 95% rename from threadpool/message/src/main/java/cn/hippo4j/message/dto/NotifyConfigDTO.java rename to kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyConfigDTO.java index 6b4370c2..b230769a 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/dto/NotifyConfigDTO.java +++ b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyConfigDTO.java @@ -15,9 +15,8 @@ * limitations under the License. */ -package cn.hippo4j.message.dto; +package cn.hippo4j.threadpool.message.api; -import cn.hippo4j.message.enums.NotifyTypeEnum; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/enums/NotifyPlatformEnum.java b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyPlatformEnum.java similarity index 95% rename from threadpool/message/src/main/java/cn/hippo4j/message/enums/NotifyPlatformEnum.java rename to kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyPlatformEnum.java index 5ffbad3b..7ad1a6b5 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/enums/NotifyPlatformEnum.java +++ b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyPlatformEnum.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.enums; +package cn.hippo4j.threadpool.message.api; /** * Notify platform enum. diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/enums/NotifyTypeEnum.java b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyTypeEnum.java similarity index 96% rename from threadpool/message/src/main/java/cn/hippo4j/message/enums/NotifyTypeEnum.java rename to kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyTypeEnum.java index cc9bc102..84b120b6 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/enums/NotifyTypeEnum.java +++ b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/NotifyTypeEnum.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.enums; +package cn.hippo4j.threadpool.message.api; /** * Notify type enum. diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/dto/ThreadPoolNotifyDTO.java b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/ThreadPoolNotifyDTO.java similarity index 91% rename from threadpool/message/src/main/java/cn/hippo4j/message/dto/ThreadPoolNotifyDTO.java rename to kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/ThreadPoolNotifyDTO.java index c0519b0f..456e3564 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/dto/ThreadPoolNotifyDTO.java +++ b/kernel/message/api/src/main/java/cn/hippo4j/threadpool/message/api/ThreadPoolNotifyDTO.java @@ -15,8 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.message.dto; +package cn.hippo4j.threadpool.message.api; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; import lombok.Data; import java.util.List; diff --git a/kernel/message/core/pom.xml b/kernel/message/core/pom.xml new file mode 100644 index 00000000..a93d702e --- /dev/null +++ b/kernel/message/core/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + cn.hippo4j + hippo4j-threadpool-kernel-message + ${revision} + + + hippo4j-threadpool-message-core + + + + cn.hippo4j + hippo4j-threadpool-message-api + ${project.version} + + + commons-codec + commons-codec + + + cn.hippo4j + hippo4j-threadpool-infra-common + ${project.version} + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework + spring-core + + + + + + + src/main/resources + + **/*.txt + **/*.json + + + + + diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/DingAlarmConstants.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/DingAlarmConstants.java similarity index 97% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/DingAlarmConstants.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/DingAlarmConstants.java index f4bb2285..6a5ce14f 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/DingAlarmConstants.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/DingAlarmConstants.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.platform.constant; +package cn.hippo4j.threadpool.message.core.constant; /** * Ding alarm constants. diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/LarkAlarmConstants.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/LarkAlarmConstants.java similarity index 96% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/LarkAlarmConstants.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/LarkAlarmConstants.java index a8e9c4a5..5396e0a1 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/LarkAlarmConstants.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/LarkAlarmConstants.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.platform.constant; +package cn.hippo4j.threadpool.message.core.constant; /** * Lark alarm constants. diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/WeChatAlarmConstants.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/WeChatAlarmConstants.java similarity index 96% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/WeChatAlarmConstants.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/WeChatAlarmConstants.java index 8b6001fc..a6f6c53c 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/constant/WeChatAlarmConstants.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/constant/WeChatAlarmConstants.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.platform.constant; +package cn.hippo4j.threadpool.message.core.constant; /** * We chat alarm constants. diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/DingSendMessageHandler.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/DingSendMessageHandler.java similarity index 79% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/DingSendMessageHandler.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/DingSendMessageHandler.java index e915f622..86c8072e 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/DingSendMessageHandler.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/DingSendMessageHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.platform; +package cn.hippo4j.threadpool.message.core.platform; import cn.hippo4j.common.toolkit.Assert; import cn.hippo4j.common.toolkit.CollectionUtil; @@ -23,13 +23,12 @@ import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.Singleton; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.toolkit.http.HttpUtil; -import cn.hippo4j.core.toolkit.FileUtil; -import cn.hippo4j.message.dto.NotifyConfigDTO; -import cn.hippo4j.message.enums.NotifyPlatformEnum; -import cn.hippo4j.message.platform.base.AbstractRobotSendMessageHandler; -import cn.hippo4j.message.platform.base.RobotMessageActualContent; -import cn.hippo4j.message.platform.base.RobotMessageExecuteDTO; -import cn.hippo4j.message.platform.constant.DingAlarmConstants; +import cn.hippo4j.threadpool.message.core.platform.base.AbstractRobotSendMessageHandler; +import cn.hippo4j.threadpool.message.core.platform.base.RobotMessageActualContent; +import cn.hippo4j.threadpool.message.core.platform.base.RobotMessageExecuteDTO; +import cn.hippo4j.threadpool.message.core.constant.DingAlarmConstants; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.api.NotifyPlatformEnum; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; @@ -42,13 +41,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Objects; -import static cn.hippo4j.message.platform.constant.DingAlarmConstants.DING_ALARM_TIMEOUT_REPLACE_TXT; -import static cn.hippo4j.message.platform.constant.DingAlarmConstants.DING_ALARM_TIMEOUT_TRACE_REPLACE_TXT; -import static cn.hippo4j.message.platform.constant.DingAlarmConstants.DING_ALARM_TITLE; -import static cn.hippo4j.message.platform.constant.DingAlarmConstants.DING_NOTICE_TITLE; - /** - * doc:自定义机器人接入 * Send ding notification message. */ @Slf4j @@ -66,10 +59,10 @@ public class DingSendMessageHandler extends AbstractRobotSendMessageHandler { RobotMessageActualContent robotMessageActualContent = RobotMessageActualContent.builder() .receiveSeparator(", @") .changeSeparator(" -> ") - .replaceTxt(DING_ALARM_TIMEOUT_REPLACE_TXT) - .traceReplaceTxt(DING_ALARM_TIMEOUT_TRACE_REPLACE_TXT) - .alarmMessageContent(Singleton.get(dingAlarmTxtKey, () -> FileUtil.readUtf8String(dingAlarmTxtKey))) - .configMessageContent(Singleton.get(dingConfigTxtKey, () -> FileUtil.readUtf8String(dingConfigTxtKey))) + .replaceTxt(DingAlarmConstants.DING_ALARM_TIMEOUT_REPLACE_TXT) + .traceReplaceTxt(DingAlarmConstants.DING_ALARM_TIMEOUT_TRACE_REPLACE_TXT) + .alarmMessageContent(Singleton.get(dingAlarmTxtKey, () -> readUtf8String(dingAlarmTxtKey))) + .configMessageContent(Singleton.get(dingConfigTxtKey, () -> readUtf8String(dingConfigTxtKey))) .build(); return robotMessageActualContent; } @@ -92,7 +85,7 @@ public class DingSendMessageHandler extends AbstractRobotSendMessageHandler { log.error("Failed to sign the message sent by nailing.", ex); } } - String title = Objects.equals(notifyConfig.getType(), "CONFIG") ? DING_NOTICE_TITLE : DING_ALARM_TITLE; + String title = Objects.equals(notifyConfig.getType(), "CONFIG") ? DingAlarmConstants.DING_NOTICE_TITLE : DingAlarmConstants.DING_ALARM_TITLE; String text = robotMessageExecuteDTO.getText(); ArrayList atMobiles = CollectionUtil.newArrayList(notifyConfig.getReceives().split(",")); HashMap markdown = new HashMap<>(); diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/LarkSendMessageHandler.java similarity index 86% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/LarkSendMessageHandler.java index 1a16f3be..4e43da9f 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/LarkSendMessageHandler.java @@ -15,20 +15,20 @@ * limitations under the License. */ -package cn.hippo4j.message.platform; +package cn.hippo4j.threadpool.message.core.platform; import cn.hippo4j.common.toolkit.Assert; import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.Singleton; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.toolkit.http.HttpUtil; -import cn.hippo4j.core.toolkit.FileUtil; -import cn.hippo4j.message.dto.NotifyConfigDTO; -import cn.hippo4j.message.enums.NotifyPlatformEnum; -import cn.hippo4j.message.enums.NotifyTypeEnum; -import cn.hippo4j.message.request.AlarmNotifyRequest; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; -import cn.hippo4j.message.service.SendMessageHandler; +import cn.hippo4j.threadpool.message.core.request.AlarmNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.ChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.service.SendMessageHandler; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.api.NotifyPlatformEnum; +import cn.hippo4j.threadpool.message.api.NotifyTypeEnum; +import cn.hippo4j.threadpool.message.core.constant.LarkAlarmConstants; import lombok.Data; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; @@ -46,11 +46,6 @@ import java.util.Arrays; import java.util.Objects; import java.util.stream.Collectors; -import static cn.hippo4j.message.platform.constant.LarkAlarmConstants.LARK_AT_FORMAT_OPENID; -import static cn.hippo4j.message.platform.constant.LarkAlarmConstants.LARK_AT_FORMAT_USERNAME; -import static cn.hippo4j.message.platform.constant.LarkAlarmConstants.LARK_BOT_URL; -import static cn.hippo4j.message.platform.constant.LarkAlarmConstants.LARK_OPENID_PREFIX; - /** * Send lark notification message. */ @@ -69,13 +64,13 @@ public class LarkSendMessageHandler implements SendMessageHandler { String afterReceives = getReceives(notifyConfig.getReceives()); String larkAlarmTimeoutReplaceTxt; String larkAlarmTxtKey = "message/robot/dynamic-thread-pool/lark-alarm.json"; - String larkAlarmTxt = Singleton.get(larkAlarmTxtKey, () -> FileUtil.readUtf8String(larkAlarmTxtKey)); + String larkAlarmTxt = Singleton.get(larkAlarmTxtKey, () -> readUtf8String(larkAlarmTxtKey)); String larkAlarmTimeoutReplaceJsonKey = "message/robot/dynamic-thread-pool/lark-alarm-timeout-replace.json"; - String larkAlarmTimeoutReplaceJson = Singleton.get(larkAlarmTimeoutReplaceJsonKey, () -> FileUtil.readUtf8String(larkAlarmTimeoutReplaceJsonKey)); + String larkAlarmTimeoutReplaceJson = Singleton.get(larkAlarmTimeoutReplaceJsonKey, () -> readUtf8String(larkAlarmTimeoutReplaceJsonKey)); if (Objects.equals(alarmNotifyRequest.getNotifyTypeEnum(), NotifyTypeEnum.TIMEOUT)) { String executeTimeoutTrace = alarmNotifyRequest.getExecuteTimeoutTrace(); String larkAlarmTimoutTraceReplaceJsonKey = "message/robot/dynamic-thread-pool/lark-alarm-trace-replace.json"; - String larkAlarmTimoutTraceReplaceJson = Singleton.get(larkAlarmTimoutTraceReplaceJsonKey, () -> FileUtil.readUtf8String(larkAlarmTimoutTraceReplaceJsonKey)); + String larkAlarmTimoutTraceReplaceJson = Singleton.get(larkAlarmTimoutTraceReplaceJsonKey, () -> readUtf8String(larkAlarmTimoutTraceReplaceJsonKey)); if (StringUtil.isNotBlank(executeTimeoutTrace)) { String larkAlarmTimoutTraceReplaceTxt = String.format(larkAlarmTimoutTraceReplaceJson, executeTimeoutTrace); larkAlarmTimeoutReplaceTxt = StringUtil.replace(larkAlarmTimeoutReplaceJson, larkAlarmTimoutTraceReplaceJson, larkAlarmTimoutTraceReplaceTxt); @@ -126,7 +121,7 @@ public class LarkSendMessageHandler implements SendMessageHandler { String threadPoolId = changeParameterNotifyRequest.getThreadPoolId(); String afterReceives = getReceives(notifyConfig.getReceives()); String larkNoticeJsonKey = "message/robot/dynamic-thread-pool/lark-config.json"; - String larkNoticeJson = Singleton.get(larkNoticeJsonKey, () -> FileUtil.readUtf8String(larkNoticeJsonKey)); + String larkNoticeJson = Singleton.get(larkNoticeJsonKey, () -> readUtf8String(larkNoticeJsonKey)); String text = String.format(larkNoticeJson, changeParameterNotifyRequest.getActive(), threadPoolId, @@ -151,12 +146,13 @@ public class LarkSendMessageHandler implements SendMessageHandler { return ""; } return Arrays.stream(receives.split(",")) - .map(receive -> StringUtil.startWith(receive, LARK_OPENID_PREFIX) ? String.format(LARK_AT_FORMAT_OPENID, receive) : String.format(LARK_AT_FORMAT_USERNAME, receive)) + .map(receive -> StringUtil.startWith(receive, LarkAlarmConstants.LARK_OPENID_PREFIX) ? String.format(LarkAlarmConstants.LARK_AT_FORMAT_OPENID, receive) + : String.format(LarkAlarmConstants.LARK_AT_FORMAT_USERNAME, receive)) .collect(Collectors.joining(" ")); } private void execute(String secretKey, String text) { - String serverUrl = LARK_BOT_URL + secretKey; + String serverUrl = LarkAlarmConstants.LARK_BOT_URL + secretKey; try { String responseBody = HttpUtil.postJson(serverUrl, text); LarkRobotResponse response = JSONUtil.parseObject(responseBody, LarkRobotResponse.class); @@ -173,10 +169,7 @@ public class LarkSendMessageHandler implements SendMessageHandler { * generate Signature */ private String genSign(String secret, String timestamp) throws NoSuchAlgorithmException, InvalidKeyException { - // geneSign String stringToSign = timestamp + "\n" + secret; - - // encode Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); byte[] signData = mac.doFinal(new byte[]{}); diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/WeChatSendMessageHandler.java similarity index 76% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/WeChatSendMessageHandler.java index 48e191f4..dbabc582 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/WeChatSendMessageHandler.java @@ -15,25 +15,21 @@ * limitations under the License. */ -package cn.hippo4j.message.platform; +package cn.hippo4j.threadpool.message.core.platform; import cn.hippo4j.common.toolkit.Assert; import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.Singleton; import cn.hippo4j.common.toolkit.http.HttpUtil; -import cn.hippo4j.core.toolkit.FileUtil; -import cn.hippo4j.message.enums.NotifyPlatformEnum; -import cn.hippo4j.message.platform.base.AbstractRobotSendMessageHandler; -import cn.hippo4j.message.platform.base.RobotMessageActualContent; -import cn.hippo4j.message.platform.base.RobotMessageExecuteDTO; +import cn.hippo4j.threadpool.message.core.platform.base.AbstractRobotSendMessageHandler; +import cn.hippo4j.threadpool.message.core.platform.base.RobotMessageActualContent; +import cn.hippo4j.threadpool.message.core.platform.base.RobotMessageExecuteDTO; +import cn.hippo4j.threadpool.message.api.NotifyPlatformEnum; +import cn.hippo4j.threadpool.message.core.constant.WeChatAlarmConstants; import lombok.Data; import lombok.experimental.Accessors; import lombok.extern.slf4j.Slf4j; -import static cn.hippo4j.message.platform.constant.WeChatAlarmConstants.WE_CHAT_ALARM_TIMOUT_REPLACE_TXT; -import static cn.hippo4j.message.platform.constant.WeChatAlarmConstants.WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT; -import static cn.hippo4j.message.platform.constant.WeChatAlarmConstants.WE_CHAT_SERVER_URL; - /** * WeChat send message handler. */ @@ -52,16 +48,16 @@ public class WeChatSendMessageHandler extends AbstractRobotSendMessageHandler { return RobotMessageActualContent.builder() .receiveSeparator("><@") .changeSeparator(" ➲ ") - .replaceTxt(WE_CHAT_ALARM_TIMOUT_REPLACE_TXT) - .traceReplaceTxt(WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT) - .alarmMessageContent(Singleton.get(weChatAlarmTxtKey, () -> FileUtil.readUtf8String(weChatAlarmTxtKey))) - .configMessageContent(Singleton.get(weChatConfigTxtKey, () -> FileUtil.readUtf8String(weChatConfigTxtKey))) + .replaceTxt(WeChatAlarmConstants.WE_CHAT_ALARM_TIMOUT_REPLACE_TXT) + .traceReplaceTxt(WeChatAlarmConstants.WE_CHAT_ALARM_TIMOUT_TRACE_REPLACE_TXT) + .alarmMessageContent(Singleton.get(weChatAlarmTxtKey, () -> readUtf8String(weChatAlarmTxtKey))) + .configMessageContent(Singleton.get(weChatConfigTxtKey, () -> readUtf8String(weChatConfigTxtKey))) .build(); } @Override protected void execute(RobotMessageExecuteDTO robotMessageExecuteDTO) { - String serverUrl = WE_CHAT_SERVER_URL + robotMessageExecuteDTO.getNotifyConfig().getSecretKey(); + String serverUrl = WeChatAlarmConstants.WE_CHAT_SERVER_URL + robotMessageExecuteDTO.getNotifyConfig().getSecretKey(); try { WeChatReqDTO weChatReq = new WeChatReqDTO(); weChatReq.setMsgtype("markdown"); diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/AbstractRobotSendMessageHandler.java similarity index 94% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/AbstractRobotSendMessageHandler.java index aefe572c..36add5c0 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/AbstractRobotSendMessageHandler.java @@ -15,15 +15,15 @@ * limitations under the License. */ -package cn.hippo4j.message.platform.base; +package cn.hippo4j.threadpool.message.core.platform.base; import cn.hippo4j.common.toolkit.Joiner; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.message.dto.NotifyConfigDTO; -import cn.hippo4j.message.enums.NotifyTypeEnum; -import cn.hippo4j.message.request.AlarmNotifyRequest; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; -import cn.hippo4j.message.service.SendMessageHandler; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.api.NotifyTypeEnum; +import cn.hippo4j.threadpool.message.core.request.AlarmNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.ChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.service.SendMessageHandler; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageActualContent.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/RobotMessageActualContent.java similarity index 96% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageActualContent.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/RobotMessageActualContent.java index fb483206..5ecd1bce 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageActualContent.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/RobotMessageActualContent.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.platform.base; +package cn.hippo4j.threadpool.message.core.platform.base; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageExecuteDTO.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/RobotMessageExecuteDTO.java similarity index 91% rename from threadpool/message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageExecuteDTO.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/RobotMessageExecuteDTO.java index 29191d6f..6bd05c57 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/base/RobotMessageExecuteDTO.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/platform/base/RobotMessageExecuteDTO.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.message.platform.base; +package cn.hippo4j.threadpool.message.core.platform.base; -import cn.hippo4j.message.dto.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/request/AlarmNotifyRequest.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/AlarmNotifyRequest.java similarity index 93% rename from threadpool/message/src/main/java/cn/hippo4j/message/request/AlarmNotifyRequest.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/AlarmNotifyRequest.java index 44a25599..eca49cc3 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/request/AlarmNotifyRequest.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/AlarmNotifyRequest.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package cn.hippo4j.message.request; +package cn.hippo4j.threadpool.message.core.request; -import cn.hippo4j.message.enums.NotifyTypeEnum; -import cn.hippo4j.message.request.base.BaseNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.base.BaseNotifyRequest; +import cn.hippo4j.threadpool.message.api.NotifyTypeEnum; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/request/ChangeParameterNotifyRequest.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/ChangeParameterNotifyRequest.java similarity index 93% rename from threadpool/message/src/main/java/cn/hippo4j/message/request/ChangeParameterNotifyRequest.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/ChangeParameterNotifyRequest.java index 72e7b925..3f96fe10 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/request/ChangeParameterNotifyRequest.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/ChangeParameterNotifyRequest.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.message.request; +package cn.hippo4j.threadpool.message.core.request; -import cn.hippo4j.message.request.base.BaseNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.base.BaseNotifyRequest; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/request/RobotAlarmNotifyRequest.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/RobotAlarmNotifyRequest.java similarity index 95% rename from threadpool/message/src/main/java/cn/hippo4j/message/request/RobotAlarmNotifyRequest.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/RobotAlarmNotifyRequest.java index 4d0828c7..d63dd7b1 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/request/RobotAlarmNotifyRequest.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/RobotAlarmNotifyRequest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.request; +package cn.hippo4j.threadpool.message.core.request; import lombok.Data; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/request/RobotChangeParameterNotifyRequest.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/RobotChangeParameterNotifyRequest.java similarity index 95% rename from threadpool/message/src/main/java/cn/hippo4j/message/request/RobotChangeParameterNotifyRequest.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/RobotChangeParameterNotifyRequest.java index 2b35b95b..6bf72c00 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/request/RobotChangeParameterNotifyRequest.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/RobotChangeParameterNotifyRequest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.request; +package cn.hippo4j.threadpool.message.core.request; import lombok.Data; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/request/ThreadPoolNotifyRequest.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/ThreadPoolNotifyRequest.java similarity index 95% rename from threadpool/message/src/main/java/cn/hippo4j/message/request/ThreadPoolNotifyRequest.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/ThreadPoolNotifyRequest.java index 7b73175a..c90ccde4 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/request/ThreadPoolNotifyRequest.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/ThreadPoolNotifyRequest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.request; +package cn.hippo4j.threadpool.message.core.request; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/request/WebChangeParameterNotifyRequest.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/WebChangeParameterNotifyRequest.java similarity index 91% rename from threadpool/message/src/main/java/cn/hippo4j/message/request/WebChangeParameterNotifyRequest.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/WebChangeParameterNotifyRequest.java index 25497484..3a17986c 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/request/WebChangeParameterNotifyRequest.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/WebChangeParameterNotifyRequest.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.message.request; +package cn.hippo4j.threadpool.message.core.request; -import cn.hippo4j.message.request.base.BaseNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.base.BaseNotifyRequest; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -49,5 +49,4 @@ public class WebChangeParameterNotifyRequest extends BaseNotifyRequest { private Long beforeKeepAliveTime; private Long nowKeepAliveTime; - } diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/request/base/BaseNotifyRequest.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/base/BaseNotifyRequest.java similarity index 95% rename from threadpool/message/src/main/java/cn/hippo4j/message/request/base/BaseNotifyRequest.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/base/BaseNotifyRequest.java index 5db9a63c..14f83b30 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/request/base/BaseNotifyRequest.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/request/base/BaseNotifyRequest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.request.base; +package cn.hippo4j.threadpool.message.core.request.base; import cn.hippo4j.common.api.NotifyRequest; import lombok.Data; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/AlarmControlHandler.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/AlarmControlHandler.java similarity index 96% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/AlarmControlHandler.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/AlarmControlHandler.java index 4d94b6bc..9832be91 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/AlarmControlHandler.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/AlarmControlHandler.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.message.core.service; import cn.hippo4j.common.constant.Constants; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.message.dto.AlarmControlDTO; +import cn.hippo4j.threadpool.message.api.AlarmControlDTO; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/DefaultThreadPoolConfigChangeHandler.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/DefaultThreadPoolConfigChangeHandler.java similarity index 73% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/DefaultThreadPoolConfigChangeHandler.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/DefaultThreadPoolConfigChangeHandler.java index 730edd81..6ce5dfd4 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/DefaultThreadPoolConfigChangeHandler.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/DefaultThreadPoolConfigChangeHandler.java @@ -15,14 +15,17 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.message.core.service; import cn.hippo4j.common.api.ThreadPoolConfigChange; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.core.toolkit.IdentifyUtil; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.ChangeParameterNotifyRequest; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; + +import static cn.hippo4j.common.propertie.EnvironmentProperties.active; +import static cn.hippo4j.common.propertie.EnvironmentProperties.applicationName; +import static cn.hippo4j.common.propertie.EnvironmentProperties.itemId; +import static cn.hippo4j.common.propertie.IdentifyProperties.IDENTIFY; /** * Default thread-pool config change handler. @@ -30,16 +33,7 @@ import org.springframework.beans.factory.annotation.Value; @RequiredArgsConstructor public class DefaultThreadPoolConfigChangeHandler 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; + private final ThreadPoolSendMessageService hippo4jSendMessageService; /** * Send pool config change. @@ -51,7 +45,7 @@ public class DefaultThreadPoolConfigChangeHandler implements ThreadPoolConfigCha requestParam.setActive(active.toUpperCase()); String appName = StringUtil.isBlank(itemId) ? applicationName : itemId; requestParam.setAppName(appName); - requestParam.setIdentify(IdentifyUtil.getIdentify()); + requestParam.setIdentify(IDENTIFY); hippo4jSendMessageService.sendChangeMessage(requestParam); } } diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/GlobalNotifyAlarmManage.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/GlobalNotifyAlarmManage.java similarity index 96% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/GlobalNotifyAlarmManage.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/GlobalNotifyAlarmManage.java index c9f9fdc8..ec7de73a 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/GlobalNotifyAlarmManage.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/GlobalNotifyAlarmManage.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.message.core.service; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/SendMessageHandler.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/SendMessageHandler.java similarity index 61% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/SendMessageHandler.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/SendMessageHandler.java index 042c4818..5a8b0630 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/SendMessageHandler.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/SendMessageHandler.java @@ -15,12 +15,18 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.message.core.service; -import cn.hippo4j.message.dto.NotifyConfigDTO; -import cn.hippo4j.message.request.AlarmNotifyRequest; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; -import cn.hippo4j.message.request.WebChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.AlarmNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.ChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.WebChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import lombok.SneakyThrows; +import org.springframework.core.io.ClassPathResource; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; /** * Send message handler. @@ -59,4 +65,23 @@ public interface SendMessageHandler { default void sendWebChangeMessage(NotifyConfigDTO notifyConfig, WebChangeParameterNotifyRequest changeParameterNotifyRequest) throws IllegalAccessException { throw new IllegalAccessException("Please implement this method before using it."); } + + @SneakyThrows + default String readUtf8String(String path) { + int endFlagCode = -1; + String resultReadStr; + ClassPathResource classPathResource = new ClassPathResource(path); + try ( + InputStream inputStream = classPathResource.getInputStream(); + BufferedInputStream bis = new BufferedInputStream(inputStream); + ByteArrayOutputStream buf = new ByteArrayOutputStream()) { + int result = bis.read(); + while (result != endFlagCode) { + buf.write((byte) result); + result = bis.read(); + } + resultReadStr = buf.toString("UTF-8"); + } + return resultReadStr; + } } diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolBaseSendMessageService.java similarity index 84% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolBaseSendMessageService.java index 21db2221..0bf039a6 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolBaseSendMessageService.java @@ -15,21 +15,18 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.message.core.service; import cn.hippo4j.common.toolkit.CollectionUtil; -import cn.hippo4j.message.api.NotifyConfigBuilder; -import cn.hippo4j.core.config.ApplicationContextHolder; -import cn.hippo4j.message.dto.AlarmControlDTO; -import cn.hippo4j.message.dto.NotifyConfigDTO; -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 cn.hippo4j.threadpool.message.api.AlarmControlDTO; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.api.NotifyTypeEnum; +import cn.hippo4j.threadpool.message.core.request.AlarmNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.ChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.WebChangeParameterNotifyRequest; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.CommandLineRunner; import java.util.HashMap; import java.util.List; @@ -40,15 +37,14 @@ import java.util.Map; */ @Slf4j @RequiredArgsConstructor -public class Hippo4jBaseSendMessageService implements Hippo4jSendMessageService, CommandLineRunner { - - private final NotifyConfigBuilder notifyConfigBuilder; +public class ThreadPoolBaseSendMessageService implements ThreadPoolSendMessageService { private final AlarmControlHandler alarmControlHandler; @Getter private final Map> notifyConfigs = new HashMap<>(); + @Getter private final Map sendMessageHandlers = new HashMap<>(); @Override @@ -165,13 +161,4 @@ public class Hippo4jBaseSendMessageService implements Hippo4jSendMessageService, public synchronized void putPlatform(Map> notifyConfigs) { this.notifyConfigs.putAll(notifyConfigs); } - - @Override - public void run(String... args) throws Exception { - Map sendMessageHandlerMap = - ApplicationContextHolder.getBeansOfType(SendMessageHandler.class); - sendMessageHandlerMap.values().forEach(each -> sendMessageHandlers.put(each.getType(), each)); - Map> buildNotify = notifyConfigBuilder.buildNotify(); - notifyConfigs.putAll(buildNotify); - } } diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/ThreadPoolNotifyAlarm.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolNotifyAlarm.java similarity index 96% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/ThreadPoolNotifyAlarm.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolNotifyAlarm.java index d0c2f5c9..5b6a3320 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/ThreadPoolNotifyAlarm.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolNotifyAlarm.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.message.core.service; import lombok.Data; import lombok.NoArgsConstructor; diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolSendMessageService.java similarity index 80% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolSendMessageService.java index 1878cbc7..a92cea7f 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/ThreadPoolSendMessageService.java @@ -15,17 +15,17 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.message.core.service; -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 cn.hippo4j.threadpool.message.core.request.AlarmNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.ChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.WebChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.api.NotifyTypeEnum; /** * Hippo-4j send message service. */ -public interface Hippo4jSendMessageService { +public interface ThreadPoolSendMessageService { /** * Send dynamic thread pool alert notifications. diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/WebThreadPoolConfigChangeHandler.java similarity index 72% rename from threadpool/message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java rename to kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/WebThreadPoolConfigChangeHandler.java index f2422d67..3f68afa5 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java +++ b/kernel/message/core/src/main/java/cn/hippo4j/threadpool/message/core/service/WebThreadPoolConfigChangeHandler.java @@ -15,15 +15,18 @@ * limitations under the License. */ -package cn.hippo4j.message.service; +package cn.hippo4j.threadpool.message.core.service; import cn.hippo4j.common.api.ThreadPoolConfigChange; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.core.toolkit.IdentifyUtil; -import cn.hippo4j.message.request.WebChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.WebChangeParameterNotifyRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; + +import static cn.hippo4j.common.propertie.EnvironmentProperties.active; +import static cn.hippo4j.common.propertie.EnvironmentProperties.applicationName; +import static cn.hippo4j.common.propertie.EnvironmentProperties.itemId; +import static cn.hippo4j.common.propertie.IdentifyProperties.IDENTIFY; /** * Web thread-pool config change handler. @@ -32,16 +35,7 @@ import org.springframework.beans.factory.annotation.Value; @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; + private final ThreadPoolSendMessageService hippo4jSendMessageService; /** * Send pool config change message for web. @@ -54,10 +48,10 @@ public class WebThreadPoolConfigChangeHandler implements ThreadPoolConfigChange< requestParam.setActive(active.toUpperCase()); String appName = StringUtil.isBlank(itemId) ? applicationName : itemId; requestParam.setAppName(appName); - requestParam.setIdentify(IdentifyUtil.getIdentify()); + requestParam.setIdentify(IDENTIFY); hippo4jSendMessageService.sendChangeMessage(requestParam); } catch (Throwable th) { - log.error("send web thread pool config change message failed.", th); + log.error("Send web thread pool config change message failed.", th); } } } diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/ding-alarm.txt b/kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/ding-alarm.txt similarity index 100% rename from threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/ding-alarm.txt rename to kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/ding-alarm.txt diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/ding-config.txt b/kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/ding-config.txt similarity index 100% rename from threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/ding-config.txt rename to kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/ding-config.txt diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-timeout-replace.json b/kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-timeout-replace.json similarity index 100% rename from threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-timeout-replace.json rename to kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-timeout-replace.json diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-trace-replace.json b/kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-trace-replace.json similarity index 100% rename from threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-trace-replace.json rename to kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm-trace-replace.json diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json b/kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json similarity index 100% rename from threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json rename to kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-config.json b/kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/lark-config.json similarity index 100% rename from threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-config.json rename to kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/lark-config.json diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/wechat-alarm.txt b/kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/wechat-alarm.txt similarity index 100% rename from threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/wechat-alarm.txt rename to kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/wechat-alarm.txt diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/wechat-config.txt b/kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/wechat-config.txt similarity index 100% rename from threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/wechat-config.txt rename to kernel/message/core/src/main/resources/message/robot/dynamic-thread-pool/wechat-config.txt diff --git a/threadpool/message/src/test/java/cn/hippo4j/message/service/AlarmControlHandlerTest.java b/kernel/message/core/src/test/java/cn/hippo4j/message/service/AlarmControlHandlerTest.java similarity index 89% rename from threadpool/message/src/test/java/cn/hippo4j/message/service/AlarmControlHandlerTest.java rename to kernel/message/core/src/test/java/cn/hippo4j/message/service/AlarmControlHandlerTest.java index e7b22bae..351116cd 100644 --- a/threadpool/message/src/test/java/cn/hippo4j/message/service/AlarmControlHandlerTest.java +++ b/kernel/message/core/src/test/java/cn/hippo4j/message/service/AlarmControlHandlerTest.java @@ -17,8 +17,9 @@ package cn.hippo4j.message.service; -import cn.hippo4j.message.dto.AlarmControlDTO; -import cn.hippo4j.message.enums.NotifyTypeEnum; +import cn.hippo4j.threadpool.message.api.AlarmControlDTO; +import cn.hippo4j.threadpool.message.api.NotifyTypeEnum; +import cn.hippo4j.threadpool.message.core.service.AlarmControlHandler; import org.junit.Assert; import org.junit.Test; diff --git a/kernel/message/mode/config/pom.xml b/kernel/message/mode/config/pom.xml new file mode 100644 index 00000000..cd90e448 --- /dev/null +++ b/kernel/message/mode/config/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + cn.hippo4j + hippo4j-threadpool-message-mode + ${revision} + + + hippo4j-threadpool-message-mode-config + diff --git a/kernel/message/mode/pom.xml b/kernel/message/mode/pom.xml new file mode 100644 index 00000000..e61cadaf --- /dev/null +++ b/kernel/message/mode/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + cn.hippo4j + hippo4j-threadpool-kernel-message + ${revision} + + + hippo4j-threadpool-message-mode + pom + + + config + server + + diff --git a/kernel/message/mode/server/pom.xml b/kernel/message/mode/server/pom.xml new file mode 100644 index 00000000..27b9f703 --- /dev/null +++ b/kernel/message/mode/server/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + cn.hippo4j + hippo4j-threadpool-message-mode + ${revision} + + + hippo4j-threadpool-message-mode-server + diff --git a/kernel/message/pom.xml b/kernel/message/pom.xml index 1957cad9..b013a794 100644 --- a/kernel/message/pom.xml +++ b/kernel/message/pom.xml @@ -10,4 +10,11 @@ hippo4j-threadpool-kernel-message + pom + + + api + core + mode + \ No newline at end of file diff --git a/starters/threadpool/adapter/pom.xml b/starters/threadpool/adapter/pom.xml index 1c04e448..292cb308 100644 --- a/starters/threadpool/adapter/pom.xml +++ b/starters/threadpool/adapter/pom.xml @@ -4,7 +4,7 @@ 4.0.0 cn.hippo4j - hippo4j-threadpool-spring-boot-starter + hippo4j-threadpool-starters ${revision} hippo4j-threadpool-spring-boot-starter-adapter diff --git a/starters/threadpool/config-spring-boot-1x/pom.xml b/starters/threadpool/config-spring-boot-1x/pom.xml index 36c34ca4..21088da8 100644 --- a/starters/threadpool/config-spring-boot-1x/pom.xml +++ b/starters/threadpool/config-spring-boot-1x/pom.xml @@ -5,7 +5,7 @@ 4.0.0 cn.hippo4j - hippo4j-threadpool-spring-boot-starter + hippo4j-threadpool-starters ${revision} hippo4j-threadpool-config-spring-boot-1x-starter diff --git a/starters/threadpool/config/pom.xml b/starters/threadpool/config/pom.xml index 810d423a..e452e533 100644 --- a/starters/threadpool/config/pom.xml +++ b/starters/threadpool/config/pom.xml @@ -4,7 +4,7 @@ 4.0.0 cn.hippo4j - hippo4j-threadpool-spring-boot-starter + hippo4j-threadpool-starters ${revision} hippo4j-threadpool-config-spring-boot-starter diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index ca97ebe0..b1cc647a 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -18,8 +18,6 @@ package cn.hippo4j.config.springboot.starter.config; import cn.hippo4j.adapter.web.WebThreadPoolService; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; -import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.config.springboot.starter.monitor.ThreadPoolMonitorExecutor; import cn.hippo4j.config.springboot.starter.notify.ConfigModeNotifyConfigBuilder; @@ -30,19 +28,22 @@ import cn.hippo4j.config.springboot.starter.refresher.event.WebExecutorRefreshLi import cn.hippo4j.config.springboot.starter.support.DynamicThreadPoolAdapterRegister; import cn.hippo4j.config.springboot.starter.support.DynamicThreadPoolConfigService; import cn.hippo4j.config.springboot.starter.support.DynamicThreadPoolPostProcessor; +import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.core.config.UtilAutoConfiguration; import cn.hippo4j.core.enable.MarkerConfiguration; import cn.hippo4j.core.executor.handler.DynamicThreadPoolBannerHandler; -import cn.hippo4j.message.api.NotifyConfigBuilder; +import cn.hippo4j.core.extension.initialize.Hippo4jDynamicThreadPoolInitializer; 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.WebThreadPoolConfigChangeHandler; import cn.hippo4j.springboot.starter.adapter.web.WebAdapterConfiguration; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; +import cn.hippo4j.threadpool.alarm.handler.DefaultThreadPoolCheckAlarmHandler; import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; +import cn.hippo4j.threadpool.message.api.NotifyConfigBuilder; +import cn.hippo4j.threadpool.message.core.service.AlarmControlHandler; +import cn.hippo4j.threadpool.message.core.service.DefaultThreadPoolConfigChangeHandler; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolBaseSendMessageService; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolSendMessageService; +import cn.hippo4j.threadpool.message.core.service.WebThreadPoolConfigChangeHandler; import lombok.AllArgsConstructor; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; @@ -91,13 +92,13 @@ public class DynamicThreadPoolAutoConfiguration { @Bean @ConditionalOnMissingBean - public ThreadPoolCheckAlarm defaultThreadPoolCheckAlarmHandler(Hippo4jSendMessageService hippo4jSendMessageService) { + public ThreadPoolCheckAlarm defaultThreadPoolCheckAlarmHandler(ThreadPoolSendMessageService hippo4jSendMessageService) { return new DefaultThreadPoolCheckAlarmHandler(hippo4jSendMessageService); } @Bean @ConditionalOnMissingBean - public DefaultThreadPoolConfigChangeHandler defaultThreadPoolConfigChangeHandler(Hippo4jSendMessageService hippo4jSendMessageService) { + public DefaultThreadPoolConfigChangeHandler defaultThreadPoolConfigChangeHandler(ThreadPoolSendMessageService hippo4jSendMessageService) { return new DefaultThreadPoolConfigChangeHandler(hippo4jSendMessageService); } @@ -105,7 +106,7 @@ public class DynamicThreadPoolAutoConfiguration { @ConditionalOnMissingBean public WebThreadPoolConfigChangeHandler webThreadPoolConfigChangeHandler(BootstrapConfigProperties bootstrapConfigProperties, WebThreadPoolService webThreadPoolService, - Hippo4jSendMessageService hippo4jSendMessageService) { + ThreadPoolSendMessageService hippo4jSendMessageService) { if (bootstrapConfigProperties.getWeb() != null && StringUtil.isBlank(bootstrapConfigProperties.getWeb().getThreadPoolId())) { bootstrapConfigProperties.getWeb().setThreadPoolId(webThreadPoolService.getWebContainerType().getName()); } @@ -126,8 +127,8 @@ public class DynamicThreadPoolAutoConfiguration { @SuppressWarnings("all") public DynamicThreadPoolRefreshListener hippo4jExecutorsListener(DefaultThreadPoolConfigChangeHandler threadPoolConfigChange, ConfigModeNotifyConfigBuilder configModeNotifyConfigBuilder, - Hippo4jBaseSendMessageService hippoBaseSendMessageService) { - return new DynamicThreadPoolRefreshListener(threadPoolConfigChange, configModeNotifyConfigBuilder, hippoBaseSendMessageService); + ThreadPoolBaseSendMessageService threadPoolBaseSendMessageService) { + return new DynamicThreadPoolRefreshListener(threadPoolConfigChange, configModeNotifyConfigBuilder, threadPoolBaseSendMessageService); } @Bean @@ -160,4 +161,9 @@ public class DynamicThreadPoolAutoConfiguration { public DynamicThreadPoolConfigService dynamicThreadPoolConfigService() { return new DynamicThreadPoolConfigService(); } + + @Bean + public Hippo4jDynamicThreadPoolInitializer hippo4jDynamicThreadPoolInitializer() { + return new Hippo4jDynamicThreadPoolInitializer(); + } } diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/monitor/ThreadPoolMonitorExecutor.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/monitor/ThreadPoolMonitorExecutor.java index e83b6081..ef9447cb 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/monitor/ThreadPoolMonitorExecutor.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/monitor/ThreadPoolMonitorExecutor.java @@ -17,31 +17,28 @@ package cn.hippo4j.config.springboot.starter.monitor; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.common.executor.ThreadFactoryBuilder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.extension.spi.ServiceLoaderRegistry; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; -import cn.hippo4j.threadpool.dynamic.mode.config.properties.MonitorProperties; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; +import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.monitor.base.DynamicThreadPoolMonitor; import cn.hippo4j.monitor.base.ThreadPoolMonitor; +import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; +import cn.hippo4j.threadpool.dynamic.mode.config.properties.MonitorProperties; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; - import org.springframework.beans.factory.DisposableBean; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; -import static cn.hippo4j.core.executor.manage.GlobalThreadPoolManage.getThreadPoolNum; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * Thread-pool monitor executor. @@ -82,8 +79,8 @@ public class ThreadPoolMonitorExecutor implements ApplicationRunner, DisposableB monitor.getInitialDelay(), monitor.getCollectInterval(), TimeUnit.MILLISECONDS); - if (GlobalThreadPoolManage.getThreadPoolNum() > 0) { - log.info("Dynamic thread pool: [{}]. The dynamic thread pool starts data collection and reporting.", getThreadPoolNum()); + if (ThreadPoolExecutorRegistry.getThreadPoolExecutorSize() > 0) { + log.info("Dynamic thread pool: [{}]. The dynamic thread pool starts data collection and reporting.", ThreadPoolExecutorRegistry.getThreadPoolExecutorSize()); } } diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java index 324ba783..3bc9b193 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/notify/ConfigModeNotifyConfigBuilder.java @@ -26,9 +26,9 @@ import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; import cn.hippo4j.threadpool.dynamic.mode.config.properties.NotifyPlatformProperties; import cn.hippo4j.threadpool.dynamic.mode.config.properties.WebExecutorProperties; -import cn.hippo4j.message.api.NotifyConfigBuilder; -import cn.hippo4j.message.dto.NotifyConfigDTO; -import cn.hippo4j.message.service.AlarmControlHandler; +import cn.hippo4j.threadpool.message.api.NotifyConfigBuilder; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.core.service.AlarmControlHandler; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java index fca575ba..67d82d98 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java @@ -18,7 +18,7 @@ package cn.hippo4j.config.springboot.starter.refresher; import cn.hippo4j.common.toolkit.CollectionUtil; -import cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfigDynamicRefreshEvent; +import cn.hippo4j.config.springboot.starter.refresher.event.ThreadPoolConfigDynamicRefreshEvent; import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.core.executor.support.ThreadPoolBuilder; import cn.hippo4j.threadpool.dynamic.api.BootstrapPropertiesInterface; @@ -73,7 +73,7 @@ public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPo } private void publishDynamicThreadPoolEvent(BootstrapConfigProperties configProperties) { - ApplicationContextHolder.getInstance().publishEvent(new Hippo4jConfigDynamicRefreshEvent(this, configProperties)); + ApplicationContextHolder.getInstance().publishEvent(new ThreadPoolConfigDynamicRefreshEvent(this, configProperties)); } @Override diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ZookeeperRefresherHandler.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ZookeeperRefresherHandler.java index 62ba302d..b83fc2eb 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ZookeeperRefresherHandler.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ZookeeperRefresherHandler.java @@ -18,8 +18,8 @@ package cn.hippo4j.config.springboot.starter.refresher; import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; -import cn.hippo4j.message.service.GlobalNotifyAlarmManage; -import cn.hippo4j.message.service.ThreadPoolNotifyAlarm; +import cn.hippo4j.threadpool.message.core.service.GlobalNotifyAlarmManage; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolNotifyAlarm; import lombok.extern.slf4j.Slf4j; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java index 85dd2dbc..e1d06c4e 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java @@ -24,7 +24,7 @@ import lombok.extern.slf4j.Slf4j; * Refresh listener abstract base class. */ @Slf4j -public abstract class AbstractRefreshListener implements RefreshListener { +public abstract class AbstractRefreshListener implements RefreshListener { /** * Matching nodes
diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AdapterExecutorsRefreshListener.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AdapterExecutorsRefreshListener.java index fd197215..7bd823e3 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AdapterExecutorsRefreshListener.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AdapterExecutorsRefreshListener.java @@ -34,7 +34,7 @@ import java.util.Map; import java.util.Objects; import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; -import static cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfigDynamicRefreshEventOrder.ADAPTER_EXECUTORS_LISTENER; +import static cn.hippo4j.config.springboot.starter.refresher.event.ThreadPoolConfigDynamicRefreshEventOrder.ADAPTER_EXECUTORS_LISTENER; /** * Adapter executors refresh listener. @@ -52,7 +52,7 @@ public class AdapterExecutorsRefreshListener extends AbstractRefreshListener adapterExecutors = event.getBootstrapConfigProperties().getAdapterExecutors(); Map threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class); if (CollectionUtil.isEmpty(adapterExecutors) || CollectionUtil.isEmpty(threadPoolAdapterMap)) { diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java index bd7b959e..25bb91e6 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java @@ -18,22 +18,22 @@ package cn.hippo4j.config.springboot.starter.refresher.event; import cn.hippo4j.common.api.ThreadPoolConfigChange; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.executor.support.ResizableCapacityLinkedBlockingQueue; +import cn.hippo4j.common.model.executor.ExecutorProperties; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.ThreadPoolExecutorUtil; -import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; -import cn.hippo4j.common.model.executor.ExecutorProperties; import cn.hippo4j.config.springboot.starter.notify.ConfigModeNotifyConfigBuilder; -import cn.hippo4j.threadpool.dynamic.core.executor.manage.GlobalConfigThreadPoolManage; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.message.dto.NotifyConfigDTO; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; -import cn.hippo4j.message.service.GlobalNotifyAlarmManage; -import cn.hippo4j.message.service.Hippo4jBaseSendMessageService; -import cn.hippo4j.message.service.ThreadPoolNotifyAlarm; +import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.core.request.ChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.service.GlobalNotifyAlarmManage; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolBaseSendMessageService; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolNotifyAlarm; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.Order; @@ -49,7 +49,7 @@ import java.util.concurrent.TimeUnit; import static cn.hippo4j.common.constant.ChangeThreadPoolConstants.CHANGE_DELIMITER; import static cn.hippo4j.common.constant.ChangeThreadPoolConstants.CHANGE_THREAD_POOL_TEXT; -import static cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfigDynamicRefreshEventOrder.EXECUTORS_LISTENER; +import static cn.hippo4j.config.springboot.starter.refresher.event.ThreadPoolConfigDynamicRefreshEventOrder.EXECUTORS_LISTENER; /** * Dynamic thread-pool refresh listener. @@ -63,7 +63,7 @@ public class DynamicThreadPoolRefreshListener extends AbstractRefreshListener executors = bindableConfigProperties.getExecutors(); for (ExecutorProperties properties : executors) { @@ -87,8 +87,9 @@ public class DynamicThreadPoolRefreshListener extends AbstractRefreshListener changeKeys = new ArrayList<>(); Map> newDynamicThreadPoolNotifyMap = configModeNotifyConfigBuilder.buildSingleNotifyConfig(executorProperties); - Map> notifyConfigs = hippo4jBaseSendMessageService.getNotifyConfigs(); + Map> notifyConfigs = threadPoolBaseSendMessageService.getNotifyConfigs(); if (CollectionUtil.isNotEmpty(notifyConfigs)) { for (Map.Entry> each : newDynamicThreadPoolNotifyMap.entrySet()) { if (checkNotifyConfig) { @@ -186,7 +187,7 @@ public class DynamicThreadPoolRefreshListener extends AbstractRefreshListener { @Override - public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynamicRefreshEvent) { + public void onApplicationEvent(ThreadPoolConfigDynamicRefreshEvent threadPoolDynamicRefreshEvent) { BootstrapConfigProperties bindableConfigProperties = threadPoolDynamicRefreshEvent.getBootstrapConfigProperties(); List executors = bindableConfigProperties.getExecutors(); for (ExecutorProperties executorProperties : executors) { String threadPoolId = executorProperties.getThreadPoolId(); - DynamicThreadPoolWrapper wrapper = GlobalThreadPoolManage.getExecutorService(threadPoolId); - if (wrapper != null && !wrapper.isInitFlag()) { - Hippo4jBaseSendMessageService sendMessageService = ApplicationContextHolder.getBean(Hippo4jBaseSendMessageService.class); + ThreadPoolExecutorHolder executorHolder = ThreadPoolExecutorRegistry.getHolder(threadPoolId); + ThreadPoolBaseSendMessageService sendMessageService = ApplicationContextHolder.getBean(ThreadPoolBaseSendMessageService.class); + /** + * TODO The logic here should be: + * + * 1. When the application starts, the thread pool parameters are not configured in the configuration center + * 2. After the application starts, put the relevant configuration into the configuration center + * 3. Use whether there is a thread pool notification as a judgment condition + */ + List notifyConfigList = sendMessageService.getNotifyConfigs().get(threadPoolId); + if (executorHolder != null && CollectionUtil.isEmpty(notifyConfigList)) { ConfigModeNotifyConfigBuilder configBuilder = ApplicationContextHolder.getBean(ConfigModeNotifyConfigBuilder.class); Map> notifyConfig = configBuilder.buildSingleNotifyConfig(executorProperties); sendMessageService.putPlatform(notifyConfig); - wrapper.setInitFlag(Boolean.TRUE); } } } diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/Hippo4jConfigDynamicRefreshEvent.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/ThreadPoolConfigDynamicRefreshEvent.java similarity index 86% rename from starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/Hippo4jConfigDynamicRefreshEvent.java rename to starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/ThreadPoolConfigDynamicRefreshEvent.java index caf3e2f5..253f7fc3 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/Hippo4jConfigDynamicRefreshEvent.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/ThreadPoolConfigDynamicRefreshEvent.java @@ -25,13 +25,13 @@ import org.springframework.context.ApplicationEvent; /** * Hippo-4j config dynamic refresh event. */ -public class Hippo4jConfigDynamicRefreshEvent extends ApplicationEvent { +public class ThreadPoolConfigDynamicRefreshEvent extends ApplicationEvent { @Getter @Setter private BootstrapConfigProperties bootstrapConfigProperties; - public Hippo4jConfigDynamicRefreshEvent(Object source, BootstrapConfigProperties bootstrapConfigProperties) { + public ThreadPoolConfigDynamicRefreshEvent(Object source, BootstrapConfigProperties bootstrapConfigProperties) { super(source); this.bootstrapConfigProperties = bootstrapConfigProperties; } diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/Hippo4jConfigDynamicRefreshEventOrder.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/ThreadPoolConfigDynamicRefreshEventOrder.java similarity index 95% rename from starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/Hippo4jConfigDynamicRefreshEventOrder.java rename to starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/ThreadPoolConfigDynamicRefreshEventOrder.java index f42e7901..15767d0c 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/Hippo4jConfigDynamicRefreshEventOrder.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/ThreadPoolConfigDynamicRefreshEventOrder.java @@ -20,7 +20,7 @@ package cn.hippo4j.config.springboot.starter.refresher.event; /** * Hippo-4j config dynamic refresh event order. */ -public class Hippo4jConfigDynamicRefreshEventOrder { +public class ThreadPoolConfigDynamicRefreshEventOrder { public static final int WEB_EXECUTOR_LISTENER = 0; diff --git a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java index b9663167..4165b9dd 100644 --- a/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java +++ b/starters/threadpool/config/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java @@ -20,18 +20,18 @@ package cn.hippo4j.config.springboot.starter.refresher.event; import cn.hippo4j.adapter.web.WebThreadPoolHandlerChoose; import cn.hippo4j.adapter.web.WebThreadPoolService; import cn.hippo4j.common.api.ThreadPoolConfigChange; -import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.common.model.ThreadPoolParameter; import cn.hippo4j.common.model.ThreadPoolParameterInfo; +import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; import cn.hippo4j.threadpool.dynamic.mode.config.properties.WebExecutorProperties; -import cn.hippo4j.message.request.WebChangeParameterNotifyRequest; +import cn.hippo4j.threadpool.message.core.request.WebChangeParameterNotifyRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.Order; import java.util.Objects; -import static cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfigDynamicRefreshEventOrder.WEB_EXECUTOR_LISTENER; +import static cn.hippo4j.config.springboot.starter.refresher.event.ThreadPoolConfigDynamicRefreshEventOrder.WEB_EXECUTOR_LISTENER; /** * Web executor refresh listener. @@ -53,7 +53,7 @@ public class WebExecutorRefreshListener extends AbstractRefreshListener4.0.0 cn.hippo4j - hippo4j-threadpool-spring-boot-starter + hippo4j-threadpool-starters ${revision} hippo4j-threadpool-spring-boot-starter-monitor diff --git a/starters/threadpool/pom.xml b/starters/threadpool/pom.xml index 52bb274e..253da58c 100644 --- a/starters/threadpool/pom.xml +++ b/starters/threadpool/pom.xml @@ -7,7 +7,7 @@ hippo4j-starters ${revision} - hippo4j-threadpool-spring-boot-starter + hippo4j-threadpool-starters pom diff --git a/starters/threadpool/server/pom.xml b/starters/threadpool/server/pom.xml index 7092cd9f..dd07cde8 100644 --- a/starters/threadpool/server/pom.xml +++ b/starters/threadpool/server/pom.xml @@ -4,10 +4,10 @@ 4.0.0 cn.hippo4j - hippo4j-threadpool-spring-boot-starter + hippo4j-threadpool-starters ${revision} - hippo4j-threadpool-server-spring-boot-starter + hippo4j-threadpool-spring-boot-starter @@ -81,5 +81,10 @@ hippo4j-threadpool-dynamic-api ${project.version} + + cn.hippo4j + hippo4j-threadpool-kernel-alarm + ${project.version} + diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index 7e8ccd9a..8e901da7 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -20,7 +20,6 @@ package cn.hippo4j.springboot.starter.config; import cn.hippo4j.adapter.base.ThreadPoolAdapterBeanContainer; import cn.hippo4j.adapter.web.WebThreadPoolHandlerChoose; import cn.hippo4j.common.api.ThreadDetailState; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; import cn.hippo4j.common.api.ThreadPoolConfigChange; import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.common.constant.Constants; @@ -29,15 +28,10 @@ import cn.hippo4j.core.enable.MarkerConfiguration; import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler; import cn.hippo4j.core.executor.support.service.DynamicThreadPoolService; import cn.hippo4j.core.executor.handler.DynamicThreadPoolBannerHandler; +import cn.hippo4j.core.extension.initialize.Hippo4jDynamicThreadPoolInitializer; import cn.hippo4j.core.toolkit.IdentifyUtil; import cn.hippo4j.core.toolkit.inet.InetUtils; -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.springboot.starter.adapter.web.WebAdapterConfiguration; import cn.hippo4j.springboot.starter.controller.ThreadPoolAdapterController; import cn.hippo4j.springboot.starter.controller.WebThreadPoolController; @@ -62,7 +56,14 @@ import cn.hippo4j.springboot.starter.support.AdaptedThreadPoolDestroyPostProcess import cn.hippo4j.springboot.starter.support.DynamicThreadPoolConfigService; import cn.hippo4j.springboot.starter.support.DynamicThreadPoolPostProcessor; import cn.hippo4j.springboot.starter.support.ThreadPoolPluginRegisterPostProcessor; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; +import cn.hippo4j.threadpool.alarm.handler.DefaultThreadPoolCheckAlarmHandler; import cn.hippo4j.threadpool.dynamic.api.ThreadPoolDynamicRefresh; +import cn.hippo4j.threadpool.message.api.NotifyConfigBuilder; +import cn.hippo4j.threadpool.message.core.service.AlarmControlHandler; +import cn.hippo4j.threadpool.message.core.service.DefaultThreadPoolConfigChangeHandler; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolBaseSendMessageService; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolSendMessageService; import lombok.AllArgsConstructor; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; @@ -126,9 +127,9 @@ public class DynamicThreadPoolAutoConfiguration { public DynamicThreadPoolService dynamicThreadPoolConfigService(HttpAgent httpAgent, ServerHealthCheck serverHealthCheck, ServerModeNotifyConfigBuilder serverModeNotifyConfigBuilder, - Hippo4jBaseSendMessageService hippo4jBaseSendMessageService, + ThreadPoolBaseSendMessageService threadPoolBaseSendMessageService, DynamicThreadPoolSubscribeConfig dynamicThreadPoolSubscribeConfig) { - return new DynamicThreadPoolConfigService(httpAgent, properties, serverModeNotifyConfigBuilder, hippo4jBaseSendMessageService, dynamicThreadPoolSubscribeConfig); + return new DynamicThreadPoolConfigService(httpAgent, properties, serverModeNotifyConfigBuilder, threadPoolBaseSendMessageService, dynamicThreadPoolSubscribeConfig); } @Bean @@ -219,13 +220,13 @@ public class DynamicThreadPoolAutoConfiguration { @Bean @ConditionalOnMissingBean - public ThreadPoolCheckAlarm defaultThreadPoolCheckAlarmHandler(Hippo4jSendMessageService hippo4jSendMessageService) { + public ThreadPoolCheckAlarm defaultThreadPoolCheckAlarmHandler(ThreadPoolSendMessageService hippo4jSendMessageService) { return new DefaultThreadPoolCheckAlarmHandler(hippo4jSendMessageService); } @Bean @ConditionalOnMissingBean - public ThreadPoolConfigChange defaultThreadPoolConfigChangeHandler(Hippo4jSendMessageService hippo4jSendMessageService) { + public ThreadPoolConfigChange defaultThreadPoolConfigChangeHandler(ThreadPoolSendMessageService hippo4jSendMessageService) { return new DefaultThreadPoolConfigChangeHandler(hippo4jSendMessageService); } @@ -249,4 +250,9 @@ public class DynamicThreadPoolAutoConfiguration { public ThreadPoolPluginRegisterPostProcessor threadPoolPluginRegisterPostProcessor() { return new ThreadPoolPluginRegisterPostProcessor(); } + + @Bean + public Hippo4jDynamicThreadPoolInitializer hippo4jDynamicThreadPoolInitializer() { + return new Hippo4jDynamicThreadPoolInitializer(); + } } diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/BaseThreadDetailStateHandler.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/BaseThreadDetailStateHandler.java index 72772f8b..99e56a13 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/BaseThreadDetailStateHandler.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/BaseThreadDetailStateHandler.java @@ -18,11 +18,11 @@ package cn.hippo4j.springboot.starter.core; import cn.hippo4j.common.api.ThreadDetailState; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.ThreadDetailStateInfo; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.ReflectUtil; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; @@ -44,8 +44,8 @@ public class BaseThreadDetailStateHandler implements ThreadDetailState { @Override public List getThreadDetailStateInfo(String threadPoolId) { - DynamicThreadPoolWrapper dynamicThreadPoolWrapper = GlobalThreadPoolManage.getExecutorService(threadPoolId); - ThreadPoolExecutor threadPoolExecutor = dynamicThreadPoolWrapper.getExecutor(); + ThreadPoolExecutorHolder executorHolder = ThreadPoolExecutorRegistry.getHolder(threadPoolId); + ThreadPoolExecutor threadPoolExecutor = executorHolder.getExecutor(); return getThreadDetailStateInfo(threadPoolExecutor); } diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/CacheData.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/CacheData.java index 19813c49..436df96c 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/CacheData.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/CacheData.java @@ -17,6 +17,7 @@ package cn.hippo4j.springboot.starter.core; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.springboot.starter.wrapper.ManagerListenerWrapper; import cn.hippo4j.common.toolkit.ContentUtil; @@ -57,7 +58,7 @@ public class CacheData { this.tenantId = tenantId; this.itemId = itemId; this.threadPoolId = threadPoolId; - this.content = ContentUtil.getPoolContent(GlobalThreadPoolManage.getPoolParameter(threadPoolId)); + this.content = ContentUtil.getPoolContent(ThreadPoolExecutorRegistry.getHolder(threadPoolId).getParameterInfo()); this.md5 = getMd5String(content); this.listeners = new CopyOnWriteArrayList<>(); } diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/ServerThreadPoolDynamicRefresh.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/ServerThreadPoolDynamicRefresh.java index 48325113..f4def9f4 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/ServerThreadPoolDynamicRefresh.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/core/ServerThreadPoolDynamicRefresh.java @@ -18,18 +18,18 @@ package cn.hippo4j.springboot.starter.core; import cn.hippo4j.common.api.ThreadPoolConfigChange; -import cn.hippo4j.common.extension.enums.EnableEnum; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.executor.support.ResizableCapacityLinkedBlockingQueue; +import cn.hippo4j.common.extension.enums.EnableEnum; import cn.hippo4j.common.model.ThreadPoolParameter; import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.ThreadPoolExecutorUtil; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.message.request.ChangeParameterNotifyRequest; import cn.hippo4j.threadpool.dynamic.api.ThreadPoolDynamicRefresh; +import cn.hippo4j.threadpool.message.core.request.ChangeParameterNotifyRequest; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -55,7 +55,7 @@ public class ServerThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh public void dynamicRefresh(String content) { ThreadPoolParameterInfo parameter = JSONUtil.parseObject(content, ThreadPoolParameterInfo.class); String threadPoolId = parameter.getTpId(); - ThreadPoolExecutor executor = GlobalThreadPoolManage.getExecutorService(threadPoolId).getExecutor(); + ThreadPoolExecutor executor = ThreadPoolExecutorRegistry.getHolder(threadPoolId).getExecutor(); refreshDynamicPool(parameter, executor); } @@ -75,7 +75,7 @@ public class ServerThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh originalExecuteTimeOut = dynamicExecutor.getExecuteTimeOut(); } changePoolInfo(executor, parameter); - ThreadPoolExecutor afterExecutor = GlobalThreadPoolManage.getExecutorService(threadPoolId).getExecutor(); + ThreadPoolExecutor afterExecutor = ThreadPoolExecutorRegistry.getHolder(threadPoolId).getExecutor(); String originalRejected = rejectedExecutionHandler.getClass().getSimpleName(); Long executeTimeOut = Optional.ofNullable(parameter.getExecuteTimeOut()).orElse(0L); ChangeParameterNotifyRequest changeNotifyRequest = ChangeParameterNotifyRequest.builder() diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java index 4fdee6b4..795521d7 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java @@ -17,14 +17,14 @@ package cn.hippo4j.springboot.starter.monitor; -import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.common.executor.ThreadFactoryBuilder; -import cn.hippo4j.common.monitor.Message; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.extension.spi.ServiceLoaderRegistry; +import cn.hippo4j.common.monitor.Message; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.toolkit.ThreadUtil; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; +import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.monitor.base.MonitorTypeEnum; import cn.hippo4j.monitor.base.ThreadPoolMonitor; import cn.hippo4j.springboot.starter.config.BootstrapProperties; @@ -49,8 +49,6 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import static cn.hippo4j.core.executor.manage.GlobalThreadPoolManage.getThreadPoolNum; - /** * Dynamic thread pool collection and reporting event executor. * @@ -144,8 +142,8 @@ public class ReportingEventExecutor implements Runnable, CommandLineRunner, Disp // Start reporting monitoring data thread. ThreadUtil.newThread(this, "client.thread.reporting.task", Boolean.TRUE).start(); } - if (GlobalThreadPoolManage.getThreadPoolNum() > 0) { - log.info("Dynamic thread pool: [{}]. The dynamic thread pool starts data collection and reporting.", getThreadPoolNum()); + if (ThreadPoolExecutorRegistry.getThreadPoolExecutorSize() > 0) { + log.info("Dynamic thread pool: [{}]. The dynamic thread pool starts data collection and reporting.", ThreadPoolExecutorRegistry.getThreadPoolExecutorSize()); } } diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/collect/RunTimeInfoCollector.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/collect/RunTimeInfoCollector.java index 31f4116e..c8b85771 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/collect/RunTimeInfoCollector.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/collect/RunTimeInfoCollector.java @@ -17,13 +17,13 @@ package cn.hippo4j.springboot.starter.monitor.collect; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; import cn.hippo4j.common.monitor.AbstractMessage; import cn.hippo4j.common.monitor.Message; import cn.hippo4j.common.monitor.MessageTypeEnum; import cn.hippo4j.common.monitor.RuntimeMessage; import cn.hippo4j.common.toolkit.BeanUtil; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.executor.state.AbstractThreadPoolRuntime; import cn.hippo4j.springboot.starter.config.BootstrapProperties; import lombok.AllArgsConstructor; @@ -45,7 +45,7 @@ public class RunTimeInfoCollector extends AbstractThreadPoolRuntime implements C public Message collectMessage() { AbstractMessage message = new RuntimeMessage(); List runtimeMessages = new ArrayList<>(); - List listThreadPoolId = GlobalThreadPoolManage.listThreadPoolId(); + List listThreadPoolId = ThreadPoolExecutorRegistry.listThreadPoolExecutorId(); for (String each : listThreadPoolId) { ThreadPoolRunStateInfo poolRunState = getPoolRunState(each); RuntimeMessage runtimeMessage = BeanUtil.convert(poolRunState, RuntimeMessage.class); diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/notify/ServerModeNotifyConfigBuilder.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/notify/ServerModeNotifyConfigBuilder.java index 09bed348..05e7a601 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/notify/ServerModeNotifyConfigBuilder.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/notify/ServerModeNotifyConfigBuilder.java @@ -17,18 +17,18 @@ package cn.hippo4j.springboot.starter.notify; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.Result; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.GroupKey; import cn.hippo4j.common.toolkit.JSONUtil; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; -import cn.hippo4j.message.api.NotifyConfigBuilder; -import cn.hippo4j.message.dto.NotifyConfigDTO; -import cn.hippo4j.message.dto.ThreadPoolNotifyDTO; -import cn.hippo4j.message.request.ThreadPoolNotifyRequest; -import cn.hippo4j.message.service.AlarmControlHandler; import cn.hippo4j.springboot.starter.config.BootstrapProperties; import cn.hippo4j.springboot.starter.remote.HttpAgent; +import cn.hippo4j.threadpool.message.api.NotifyConfigBuilder; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.api.ThreadPoolNotifyDTO; +import cn.hippo4j.threadpool.message.core.request.ThreadPoolNotifyRequest; +import cn.hippo4j.threadpool.message.core.service.AlarmControlHandler; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -55,7 +55,7 @@ public class ServerModeNotifyConfigBuilder implements NotifyConfigBuilder { @Override public Map> buildNotify() { - List threadPoolIds = GlobalThreadPoolManage.listThreadPoolId(); + List threadPoolIds = ThreadPoolExecutorRegistry.listThreadPoolExecutorId(); if (CollectionUtil.isEmpty(threadPoolIds)) { log.warn("The client does not have a dynamic thread pool instance configured."); return new HashMap<>(); diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/AdaptedThreadPoolDestroyPostProcessor.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/AdaptedThreadPoolDestroyPostProcessor.java index dfef5d71..bad47f90 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/AdaptedThreadPoolDestroyPostProcessor.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/AdaptedThreadPoolDestroyPostProcessor.java @@ -17,9 +17,9 @@ package cn.hippo4j.springboot.starter.support; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapter; import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapterChoose; import lombok.RequiredArgsConstructor; @@ -74,18 +74,20 @@ public class AdaptedThreadPoolDestroyPostProcessor implements DestructionAwareBe .map(each -> ((DynamicThreadPoolExecutor) each).getThreadPoolId()) // the internal thread pool is also managed by spring, no manual destruction required .filter(applicationContext::containsBeanDefinition) - .map(GlobalThreadPoolManage::getExecutorService) - .ifPresent(executor -> destroyAdaptedThreadPoolExecutor(beanName, executor)); + .map(ThreadPoolExecutorRegistry::getHolder) + .ifPresent(executorHolder -> destroyAdaptedThreadPoolExecutor(beanName, executorHolder)); } - private void destroyAdaptedThreadPoolExecutor(String beanName, DynamicThreadPoolWrapper executor) { + private void destroyAdaptedThreadPoolExecutor(String beanName, ThreadPoolExecutorHolder executorHolder) { try { if (log.isDebugEnabled()) { - log.debug("Destroy internal dynamic thread pool '{}' for bean '{}'", executor.getThreadPoolId(), beanName); + log.debug("Destroy internal dynamic thread pool '{}' for bean '{}'", executorHolder.getThreadPoolId(), beanName); + } + if (executorHolder.getExecutor() instanceof DynamicThreadPoolExecutor) { + ((DynamicThreadPoolExecutor) executorHolder.getExecutor()).destroy(); } - executor.destroy(); } catch (Exception e) { - log.warn("Failed to destroy internal dynamic thread pool '{}' for bean '{}'", executor.getThreadPoolId(), beanName); + log.warn("Failed to destroy internal dynamic thread pool '{}' for bean '{}'", executorHolder.getThreadPoolId(), beanName); } } } diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolConfigService.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolConfigService.java index f38d5f51..f28a0df5 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolConfigService.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolConfigService.java @@ -17,6 +17,8 @@ package cn.hippo4j.springboot.starter.support; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.Result; import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter; @@ -25,17 +27,15 @@ import cn.hippo4j.common.toolkit.Assert; import cn.hippo4j.common.toolkit.BooleanUtil; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.JSONUtil; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.executor.support.service.AbstractDynamicThreadPoolService; -import cn.hippo4j.message.dto.NotifyConfigDTO; -import cn.hippo4j.message.service.GlobalNotifyAlarmManage; -import cn.hippo4j.message.service.Hippo4jBaseSendMessageService; -import cn.hippo4j.message.service.ThreadPoolNotifyAlarm; import cn.hippo4j.springboot.starter.config.BootstrapProperties; import cn.hippo4j.springboot.starter.core.DynamicThreadPoolSubscribeConfig; import cn.hippo4j.springboot.starter.notify.ServerModeNotifyConfigBuilder; import cn.hippo4j.springboot.starter.remote.HttpAgent; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.core.service.GlobalNotifyAlarmManage; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolBaseSendMessageService; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolNotifyAlarm; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -56,7 +56,7 @@ public class DynamicThreadPoolConfigService extends AbstractDynamicThreadPoolSer private final HttpAgent httpAgent; private final BootstrapProperties properties; private final ServerModeNotifyConfigBuilder serverModeNotifyConfigBuilder; - private final Hippo4jBaseSendMessageService hippo4jBaseSendMessageService; + private final ThreadPoolBaseSendMessageService threadPoolBaseSendMessageService; private final DynamicThreadPoolSubscribeConfig dynamicThreadPoolSubscribeConfig; @Override @@ -84,11 +84,9 @@ public class DynamicThreadPoolConfigService extends AbstractDynamicThreadPoolSer } ThreadPoolParameterInfo parameter = JSONUtil.parseObject(JSONUtil.toJSONString(registerParameter), ThreadPoolParameterInfo.class); ThreadPoolExecutor dynamicThreadPoolExecutor = buildDynamicThreadPoolExecutor(registerParameter); - DynamicThreadPoolWrapper dynamicThreadPoolWrapper = DynamicThreadPoolWrapper.builder() - .threadPoolId(threadPoolId) - .executor(dynamicThreadPoolExecutor) - .build(); - GlobalThreadPoolManage.register(threadPoolId, parameter, dynamicThreadPoolWrapper); + ThreadPoolExecutorHolder executorHolder = new ThreadPoolExecutorHolder(threadPoolId, dynamicThreadPoolExecutor, null); + executorHolder.setParameterInfo(parameter); + ThreadPoolExecutorRegistry.putHolder(executorHolder); return dynamicThreadPoolExecutor; } @@ -104,7 +102,7 @@ public class DynamicThreadPoolConfigService extends AbstractDynamicThreadPoolSer registerParameter.getCapacityAlarm()); GlobalNotifyAlarmManage.put(registerParameter.getThreadPoolId(), threadPoolNotifyAlarm); Map> builderNotify = serverModeNotifyConfigBuilder.getAndInitNotify(CollectionUtil.newArrayList(registerParameter.getThreadPoolId())); - hippo4jBaseSendMessageService.putPlatform(builderNotify); + threadPoolBaseSendMessageService.putPlatform(builderNotify); } private void checkThreadPoolParameter(DynamicThreadPoolRegisterParameter registerParameter) { diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java index a83835a1..e2ea8e8f 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java @@ -17,30 +17,31 @@ package cn.hippo4j.springboot.starter.support; -import cn.hippo4j.common.model.Result; -import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.common.constant.Constants; -import cn.hippo4j.common.extension.enums.EnableEnum; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; +import cn.hippo4j.common.extension.enums.EnableEnum; +import cn.hippo4j.common.model.Result; import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter; import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper; import cn.hippo4j.common.toolkit.BooleanUtil; import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.ReflectUtil; +import cn.hippo4j.common.toolkit.ThreadPoolExecutorUtil; +import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.core.executor.DynamicThreadPool; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.executor.support.adpter.DynamicThreadPoolAdapterChoose; import cn.hippo4j.core.toolkit.DynamicThreadPoolAnnotationUtil; -import cn.hippo4j.common.toolkit.ThreadPoolExecutorUtil; -import cn.hippo4j.message.service.GlobalNotifyAlarmManage; -import cn.hippo4j.message.service.ThreadPoolNotifyAlarm; import cn.hippo4j.springboot.starter.config.BootstrapProperties; import cn.hippo4j.springboot.starter.core.DynamicThreadPoolSubscribeConfig; import cn.hippo4j.springboot.starter.remote.HttpAgent; +import cn.hippo4j.threadpool.message.core.service.GlobalNotifyAlarmManage; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolNotifyAlarm; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; @@ -54,14 +55,14 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import static cn.hippo4j.common.constant.Constants.INITIAL_CAPACITY; -import static cn.hippo4j.common.constant.Constants.TP_ID; -import static cn.hippo4j.common.constant.Constants.ITEM_ID; -import static cn.hippo4j.common.constant.Constants.NAMESPACE; import static cn.hippo4j.common.constant.Constants.ACTIVE_ALARM; import static cn.hippo4j.common.constant.Constants.CAPACITY_ALARM; import static cn.hippo4j.common.constant.Constants.EXECUTE_TIME_OUT; import static cn.hippo4j.common.constant.Constants.HTTP_EXECUTE_TIMEOUT; +import static cn.hippo4j.common.constant.Constants.INITIAL_CAPACITY; +import static cn.hippo4j.common.constant.Constants.ITEM_ID; +import static cn.hippo4j.common.constant.Constants.NAMESPACE; +import static cn.hippo4j.common.constant.Constants.TP_ID; /** * Dynamic thread-pool post processor. @@ -100,37 +101,32 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { if ((dynamicThreadPoolExecutor) == null) { dynamicThreadPoolExecutor = (DynamicThreadPoolExecutor) bean; } - DynamicThreadPoolWrapper dynamicThreadPoolWrapper = new DynamicThreadPoolWrapper(((DynamicThreadPoolExecutor) dynamicThreadPoolExecutor).getThreadPoolId(), dynamicThreadPoolExecutor); - ThreadPoolExecutor remoteThreadPoolExecutor = fillPoolAndRegister(dynamicThreadPoolWrapper); + ThreadPoolExecutor remoteThreadPoolExecutor = fillPoolAndRegister(((DynamicThreadPoolExecutor) dynamicThreadPoolExecutor).getThreadPoolId(), dynamicThreadPoolExecutor); DynamicThreadPoolAdapterChoose.replace(bean, remoteThreadPoolExecutor); - subscribeConfig(dynamicThreadPoolWrapper); + subscribeConfig(((DynamicThreadPoolExecutor) dynamicThreadPoolExecutor).getThreadPoolId()); return DynamicThreadPoolAdapterChoose.match(bean) ? bean : remoteThreadPoolExecutor; } - if (bean instanceof DynamicThreadPoolWrapper) { - DynamicThreadPoolWrapper dynamicThreadPoolWrapper = (DynamicThreadPoolWrapper) bean; - registerAndSubscribe(dynamicThreadPoolWrapper); - } return bean; } /** * Register and subscribe. * - * @param dynamicThreadPoolWrapper dynamic thread-pool wrapper + * @param threadPoolId dynamic thread-pool id + * @param executor dynamic thread-pool executor */ - protected void registerAndSubscribe(DynamicThreadPoolWrapper dynamicThreadPoolWrapper) { - fillPoolAndRegister(dynamicThreadPoolWrapper); - subscribeConfig(dynamicThreadPoolWrapper); + protected void registerAndSubscribe(String threadPoolId, ThreadPoolExecutor executor) { + fillPoolAndRegister(threadPoolId, executor); + subscribeConfig(threadPoolId); } /** * Fill the thread pool and register. * - * @param dynamicThreadPoolWrapper dynamic thread-pool wrapper + * @param threadPoolId dynamic thread-pool id + * @param executor dynamic thread-pool executor */ - protected ThreadPoolExecutor fillPoolAndRegister(DynamicThreadPoolWrapper dynamicThreadPoolWrapper) { - String threadPoolId = dynamicThreadPoolWrapper.getThreadPoolId(); - ThreadPoolExecutor executor = dynamicThreadPoolWrapper.getExecutor(); + protected ThreadPoolExecutor fillPoolAndRegister(String threadPoolId, ThreadPoolExecutor executor) { Map queryStrMap = new HashMap<>(INITIAL_CAPACITY); queryStrMap.put(TP_ID, threadPoolId); queryStrMap.put(ITEM_ID, properties.getItemId()); @@ -170,7 +166,9 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { } catch (Exception ex) { log.error("Failed to initialize thread pool configuration. error message: {}", ex.getMessage()); } - GlobalThreadPoolManage.register(dynamicThreadPoolWrapper.getThreadPoolId(), threadPoolParameterInfo, dynamicThreadPoolWrapper); + ThreadPoolExecutorHolder executorHolder = new ThreadPoolExecutorHolder(threadPoolId, executor, null); + executorHolder.setParameterInfo(threadPoolParameterInfo); + ThreadPoolExecutorRegistry.putHolder(executorHolder); return executor; } @@ -211,11 +209,9 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { /** * Client dynamic thread pool subscription server configuration. * - * @param dynamicThreadPoolWrapper dynamic thread-pool wrapper + * @param threadPoolId thread-pool id */ - protected void subscribeConfig(DynamicThreadPoolWrapper dynamicThreadPoolWrapper) { - if (dynamicThreadPoolWrapper.isSubscribeFlag()) { - dynamicThreadPoolSubscribeConfig.subscribeConfig(dynamicThreadPoolWrapper.getThreadPoolId()); - } + protected void subscribeConfig(String threadPoolId) { + dynamicThreadPoolSubscribeConfig.subscribeConfig(threadPoolId); } } diff --git a/threadpool/core/pom.xml b/threadpool/core/pom.xml index c151fe60..1df9e3a9 100644 --- a/threadpool/core/pom.xml +++ b/threadpool/core/pom.xml @@ -24,6 +24,11 @@ hippo4j-threadpool-dynamic-api ${project.version} + + cn.hippo4j + hippo4j-threadpool-kernel-alarm + ${project.version} + org.springframework.boot spring-boot-starter-test diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java b/threadpool/core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java index c0917f50..596cfd0b 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java @@ -24,6 +24,7 @@ import cn.hippo4j.core.executor.plugin.impl.TaskTimeoutNotifyAlarmPlugin; import cn.hippo4j.core.executor.plugin.impl.ThreadPoolExecutorShutdownPlugin; import cn.hippo4j.core.executor.plugin.manager.DefaultThreadPoolPluginManager; import cn.hippo4j.core.executor.plugin.manager.DefaultThreadPoolPluginRegistrar; +import cn.hippo4j.threadpool.alarm.handler.DefaultThreadPoolCheckAlarmHandler; import lombok.Getter; import lombok.NonNull; import lombok.Setter; @@ -44,6 +45,8 @@ import static cn.hippo4j.common.constant.Constants.NO_REJECT_COUNT_NUM; /** * Enhanced dynamic and monitored thread pool. + * + * @see DefaultThreadPoolCheckAlarmHandler#buildAlarmNotifyRequest */ @Slf4j public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor implements DisposableBean { @@ -182,6 +185,7 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl * Get reject count num. * * @return reject count num + * @see DefaultThreadPoolCheckAlarmHandler#buildAlarmNotifyRequest * @deprecated use {@link TaskRejectCountRecordPlugin} */ @Deprecated diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolWrapper.java b/threadpool/core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolWrapper.java index 7351055f..969d7123 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolWrapper.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolWrapper.java @@ -32,11 +32,14 @@ import java.util.concurrent.ThreadPoolExecutor; /** * Dynamic thread-pool wrapper. + * + * @deprecated 2.0.0 */ @Data @Builder @NoArgsConstructor @AllArgsConstructor +@Deprecated public class DynamicThreadPoolWrapper implements DisposableBean { /** diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/executor/manage/GlobalThreadPoolManage.java b/threadpool/core/src/main/java/cn/hippo4j/core/executor/manage/GlobalThreadPoolManage.java index 90e89567..40820e14 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/executor/manage/GlobalThreadPoolManage.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/executor/manage/GlobalThreadPoolManage.java @@ -17,17 +17,10 @@ package cn.hippo4j.core.executor.manage; -import cn.hippo4j.core.config.ApplicationContextHolder; -import cn.hippo4j.common.model.ThreadPoolParameter; import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; +import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.core.executor.support.service.DynamicThreadPoolService; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadPoolExecutor; /** @@ -35,78 +28,6 @@ import java.util.concurrent.ThreadPoolExecutor; */ public class GlobalThreadPoolManage { - /** - * Dynamic thread pool parameter container. - */ - private static final Map POOL_PARAMETER = new ConcurrentHashMap(); - - /** - * Dynamic thread pool wrapper. - */ - private static final Map EXECUTOR_MAP = new ConcurrentHashMap(); - - /** - * Get the dynamic thread pool wrapper class. - * - * @param threadPoolId thread-pool id - * @return dynamic thread-pool wrapper - */ - public static DynamicThreadPoolWrapper getExecutorService(String threadPoolId) { - return EXECUTOR_MAP.get(threadPoolId); - } - - /** - * Get the dynamic thread pool wrapper class. - * - * @param threadPoolId thread-pool id - * @return thread-pool executor - */ - public static ThreadPoolExecutor getExecutor(String threadPoolId) { - return Optional.ofNullable(EXECUTOR_MAP.get(threadPoolId)).map(each -> each.getExecutor()).orElse(null); - } - - /** - * Get dynamic thread pool parameters. - * - * @param threadPoolId thread-pool id - * @return thread-pool parameter - */ - public static ThreadPoolParameter getPoolParameter(String threadPoolId) { - return POOL_PARAMETER.get(threadPoolId); - } - - /** - * Register dynamic thread pool wrapper and parameters. - * - * @param threadPoolId thread-pool id - * @param threadPoolParameter thread-pool parameter - * @param executor executor - */ - public static void register(String threadPoolId, ThreadPoolParameter threadPoolParameter, DynamicThreadPoolWrapper executor) { - registerPool(threadPoolId, executor); - registerPoolParameter(threadPoolId, threadPoolParameter); - } - - /** - * Register dynamic thread pool. - * - * @param threadPoolId thread-pool id - * @param executor executor - */ - public static void registerPool(String threadPoolId, DynamicThreadPoolWrapper executor) { - EXECUTOR_MAP.put(threadPoolId, executor); - } - - /** - * Register dynamic thread pool parameters. - * - * @param threadPoolId thread-pool id - * @param threadPoolParameter thread-pool parameter - */ - public static void registerPoolParameter(String threadPoolId, ThreadPoolParameter threadPoolParameter) { - POOL_PARAMETER.put(threadPoolId, threadPoolParameter); - } - /** * Dynamically register thread pool records and notification records. * @@ -116,24 +37,4 @@ public class GlobalThreadPoolManage { DynamicThreadPoolService dynamicThreadPoolService = ApplicationContextHolder.getBean(DynamicThreadPoolService.class); return dynamicThreadPoolService.registerDynamicThreadPool(registerWrapper); } - - /** - * Get the dynamic thread pool identifier collection. - * - * @return thread-pool id list - */ - public static List listThreadPoolId() { - return new ArrayList<>(EXECUTOR_MAP.keySet()); - } - - /** - * Get the number of dynamic thread pools. - *

The data may be inaccurate when the project is initially - * launched because registration is done asynchronously. - * - * @return thread-pool num - */ - public static Integer getThreadPoolNum() { - return listThreadPoolId().size(); - } } diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/executor/plugin/impl/TaskRejectNotifyAlarmPlugin.java b/threadpool/core/src/main/java/cn/hippo4j/core/executor/plugin/impl/TaskRejectNotifyAlarmPlugin.java index 7a5f5c13..74801a86 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/executor/plugin/impl/TaskRejectNotifyAlarmPlugin.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/executor/plugin/impl/TaskRejectNotifyAlarmPlugin.java @@ -17,10 +17,10 @@ package cn.hippo4j.core.executor.plugin.impl; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor; import cn.hippo4j.core.executor.plugin.RejectedAwarePlugin; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; import lombok.NonNull; import lombok.RequiredArgsConstructor; diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/executor/plugin/impl/TaskTimeoutNotifyAlarmPlugin.java b/threadpool/core/src/main/java/cn/hippo4j/core/executor/plugin/impl/TaskTimeoutNotifyAlarmPlugin.java index 8910d621..ee43b3e3 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/executor/plugin/impl/TaskTimeoutNotifyAlarmPlugin.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/executor/plugin/impl/TaskTimeoutNotifyAlarmPlugin.java @@ -17,9 +17,9 @@ package cn.hippo4j.core.executor.plugin.impl; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.core.executor.plugin.PluginRuntime; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; @@ -63,8 +63,8 @@ public class TaskTimeoutNotifyAlarmPlugin extends AbstractTaskTimerPlugin { /** * Create a {@link TaskTimeoutNotifyAlarmPlugin}. * - * @param threadPoolId thread pool id - * @param executeTimeOut execute time out + * @param threadPoolId thread pool id + * @param executeTimeOut execute time out * @param threadPoolExecutor thread pool executor */ public TaskTimeoutNotifyAlarmPlugin(String threadPoolId, Long executeTimeOut, ThreadPoolExecutor threadPoolExecutor) { diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/executor/proxy/RejectedProxyInvocationHandler.java b/threadpool/core/src/main/java/cn/hippo4j/core/executor/proxy/RejectedProxyInvocationHandler.java index 33119193..89357f23 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/executor/proxy/RejectedProxyInvocationHandler.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/executor/proxy/RejectedProxyInvocationHandler.java @@ -17,8 +17,8 @@ package cn.hippo4j.core.executor.proxy; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; import cn.hippo4j.core.config.ApplicationContextHolder; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntime.java b/threadpool/core/src/main/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntime.java index 90b5f547..0066438e 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntime.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntime.java @@ -17,11 +17,11 @@ package cn.hippo4j.core.executor.state; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; -import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.common.toolkit.CalculateUtil; +import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -51,8 +51,8 @@ public abstract class AbstractThreadPoolRuntime { * @return thread-pool run state info */ public ThreadPoolRunStateInfo getPoolRunState(String threadPoolId) { - DynamicThreadPoolWrapper executorService = GlobalThreadPoolManage.getExecutorService(threadPoolId); - ThreadPoolExecutor pool = executorService.getExecutor(); + ThreadPoolExecutorHolder executorHolder = ThreadPoolExecutorRegistry.getHolder(threadPoolId); + ThreadPoolExecutor pool = executorHolder.getExecutor(); return getPoolRunState(threadPoolId, pool); } diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java b/threadpool/core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java index 20662aa6..8d27ef3f 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java @@ -17,14 +17,14 @@ package cn.hippo4j.core.executor.state; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.ManyThreadPoolRunStateInfo; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; import cn.hippo4j.common.toolkit.BeanUtil; import cn.hippo4j.common.toolkit.ByteConvertUtil; import cn.hippo4j.common.toolkit.MemoryUtil; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.toolkit.inet.InetUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -61,8 +61,8 @@ public class ThreadPoolRunStateHandler extends AbstractThreadPoolRuntime { poolRunStateInfo.setMemoryProportion(memoryProportion); poolRunStateInfo.setFreeMemory(ByteConvertUtil.getPrintSize(Math.subtractExact(max, used))); String threadPoolId = poolRunStateInfo.getTpId(); - DynamicThreadPoolWrapper executorService = GlobalThreadPoolManage.getExecutorService(threadPoolId); - ThreadPoolExecutor pool = executorService.getExecutor(); + ThreadPoolExecutorHolder executorHolder = ThreadPoolExecutorRegistry.getHolder(threadPoolId); + ThreadPoolExecutor pool = executorHolder.getExecutor(); String rejectedName; rejectedName = pool.getRejectedExecutionHandler().getClass().getSimpleName(); poolRunStateInfo.setRejectedName(rejectedName); diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/extension/initialize/Hippo4jDynamicThreadPoolInitializer.java b/threadpool/core/src/main/java/cn/hippo4j/core/extension/initialize/Hippo4jDynamicThreadPoolInitializer.java new file mode 100644 index 00000000..0cb52af0 --- /dev/null +++ b/threadpool/core/src/main/java/cn/hippo4j/core/extension/initialize/Hippo4jDynamicThreadPoolInitializer.java @@ -0,0 +1,47 @@ +/* + * 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.core.extension.initialize; + +import cn.hippo4j.common.propertie.EnvironmentProperties; +import cn.hippo4j.core.config.ApplicationContextHolder; +import cn.hippo4j.core.toolkit.IdentifyUtil; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * Hippo-4j dynamic thread-pool initializer. + *

+ * Compatible with SpringBoot Starter and Agent mode. + */ +public class Hippo4jDynamicThreadPoolInitializer implements InitializingBean { + + @Override + public void afterPropertiesSet() throws Exception { + // Environment initialize + ConfigurableEnvironment environment = ApplicationContextHolder.getBean(ConfigurableEnvironment.class); + EnvironmentProperties.active = environment.getProperty("spring.profiles.active", "UNKNOWN"); + EnvironmentProperties.itemId = environment.getProperty("spring.dynamic.thread-pool.item-id", ""); + EnvironmentProperties.applicationName = environment.getProperty("spring.application.name", ""); + EnvironmentProperties.checkStateInterval = environment.getProperty("spring.dynamic.thread-pool.check-state-interval", Long.class, 5L); + IdentifyUtil.getIdentify(); + // Check alarm + ThreadPoolCheckAlarm threadPoolCheckAlarm = ApplicationContextHolder.getBean(ThreadPoolCheckAlarm.class); + threadPoolCheckAlarm.scheduleExecute(); + } +} diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/FileUtil.java b/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/FileUtil.java index a267d91f..e0819b98 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/FileUtil.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/FileUtil.java @@ -20,13 +20,18 @@ package cn.hippo4j.core.toolkit; import lombok.SneakyThrows; import org.springframework.core.io.ClassPathResource; -import java.io.*; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; /** - * File util; + * File util. */ public class FileUtil { diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java b/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java index 3f6551b6..a9de2a43 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java @@ -17,6 +17,7 @@ package cn.hippo4j.core.toolkit; +import cn.hippo4j.common.propertie.IdentifyProperties; import cn.hippo4j.core.api.ClientNetworkService; import cn.hippo4j.core.config.ApplicationContextHolder; import cn.hippo4j.common.extension.spi.ServiceLoaderRegistry; @@ -36,17 +37,12 @@ import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; /** * Identify util. */ -public class IdentifyUtil { +public class IdentifyUtil extends IdentifyProperties { static { ServiceLoaderRegistry.register(ClientNetworkService.class); } - /** - * Identify - */ - private static String identify; - /** * Client identification value */ @@ -65,8 +61,8 @@ public class IdentifyUtil { * @return identify */ public static synchronized String generate(ConfigurableEnvironment environment, InetUtils inetUtil) { - if (StringUtil.isNotBlank(identify)) { - return identify; + if (StringUtil.isNotBlank(IDENTIFY)) { + return IDENTIFY; } String[] customerNetwork = ServiceLoaderRegistry.getSingletonServiceInstances(ClientNetworkService.class) .stream().findFirst().map(each -> each.getNetworkIpPort(environment)).orElse(null); @@ -84,7 +80,7 @@ public class IdentifyUtil { + port + IDENTIFY_SLICER_SYMBOL + CLIENT_IDENTIFICATION_VALUE; - identify = identify; + IDENTIFY = identify; return identify; } @@ -94,7 +90,7 @@ public class IdentifyUtil { * @return identify */ public static String getIdentify() { - while (StringUtil.isBlank(identify)) { + while (StringUtil.isBlank(IDENTIFY)) { ConfigurableEnvironment environment = ApplicationContextHolder.getBean(ConfigurableEnvironment.class); InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class); if (environment != null && inetUtils != null) { @@ -103,7 +99,7 @@ public class IdentifyUtil { } ThreadUtil.sleep(SLEEP_TIME); } - return identify; + return IDENTIFY; } /** diff --git a/threadpool/core/src/test/java/cn/hippo4j/core/executor/plugin/impl/TaskRejectNotifyAlarmPluginTest.java b/threadpool/core/src/test/java/cn/hippo4j/core/executor/plugin/impl/TaskRejectNotifyAlarmPluginTest.java index 01e699c4..aa35cdd0 100644 --- a/threadpool/core/src/test/java/cn/hippo4j/core/executor/plugin/impl/TaskRejectNotifyAlarmPluginTest.java +++ b/threadpool/core/src/test/java/cn/hippo4j/core/executor/plugin/impl/TaskRejectNotifyAlarmPluginTest.java @@ -17,11 +17,10 @@ package cn.hippo4j.core.executor.plugin.impl; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; import cn.hippo4j.common.toolkit.ThreadUtil; import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor; -import cn.hippo4j.core.executor.plugin.impl.TaskRejectNotifyAlarmPlugin; import cn.hippo4j.core.executor.plugin.manager.DefaultThreadPoolPluginManager; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; import lombok.Getter; import org.junit.Assert; import org.junit.Test; @@ -70,26 +69,26 @@ public class TaskRejectNotifyAlarmPluginTest { @Getter private final AtomicInteger numberOfAlarms = new AtomicInteger(0); + @Override public void checkPoolCapacityAlarm(String threadPoolId, ThreadPoolExecutor threadPoolExecutor) { // do noting } + @Override public void checkPoolActivityAlarm(String threadPoolId, ThreadPoolExecutor threadPoolExecutor) { // do noting } + @Override public void asyncSendRejectedAlarm(String threadPoolId) { numberOfAlarms.incrementAndGet(); } + @Override public void asyncSendExecuteTimeOutAlarm(String threadPoolId, long executeTime, long executeTimeOut, ThreadPoolExecutor threadPoolExecutor) { // do noting } - @Override - public void run(String... args) throws Exception { - - } } } diff --git a/threadpool/core/src/test/java/cn/hippo4j/core/executor/plugin/impl/TaskTimeoutNotifyAlarmPluginTest.java b/threadpool/core/src/test/java/cn/hippo4j/core/executor/plugin/impl/TaskTimeoutNotifyAlarmPluginTest.java index a5aabe04..c8d6de43 100644 --- a/threadpool/core/src/test/java/cn/hippo4j/core/executor/plugin/impl/TaskTimeoutNotifyAlarmPluginTest.java +++ b/threadpool/core/src/test/java/cn/hippo4j/core/executor/plugin/impl/TaskTimeoutNotifyAlarmPluginTest.java @@ -17,11 +17,10 @@ package cn.hippo4j.core.executor.plugin.impl; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; import cn.hippo4j.common.toolkit.ThreadUtil; import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor; -import cn.hippo4j.core.executor.plugin.impl.TaskTimeoutNotifyAlarmPlugin; import cn.hippo4j.core.executor.plugin.manager.DefaultThreadPoolPluginManager; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; import lombok.Getter; import org.junit.Assert; import org.junit.Test; @@ -89,26 +88,26 @@ public class TaskTimeoutNotifyAlarmPluginTest { @Getter private final AtomicInteger numberOfAlarms = new AtomicInteger(0); + @Override public void checkPoolCapacityAlarm(String threadPoolId, ThreadPoolExecutor threadPoolExecutor) { // do noting } + @Override public void checkPoolActivityAlarm(String threadPoolId, ThreadPoolExecutor threadPoolExecutor) { // do noting } + @Override public void asyncSendRejectedAlarm(String threadPoolId) { // do noting } + @Override public void asyncSendExecuteTimeOutAlarm(String threadPoolId, long executeTime, long executeTimeOut, ThreadPoolExecutor threadPoolExecutor) { numberOfAlarms.incrementAndGet(); } - @Override - public void run(String... args) throws Exception { - - } } } diff --git a/threadpool/core/src/test/java/cn/hippo4j/core/executor/proxy/RejectedProxyInvocationHandlerTest.java b/threadpool/core/src/test/java/cn/hippo4j/core/executor/proxy/RejectedProxyInvocationHandlerTest.java index 42a4286a..548c24a4 100644 --- a/threadpool/core/src/test/java/cn/hippo4j/core/executor/proxy/RejectedProxyInvocationHandlerTest.java +++ b/threadpool/core/src/test/java/cn/hippo4j/core/executor/proxy/RejectedProxyInvocationHandlerTest.java @@ -17,9 +17,8 @@ package cn.hippo4j.core.executor.proxy; -import cn.hippo4j.core.api.ThreadPoolCheckAlarm; import cn.hippo4j.core.config.ApplicationContextHolder; -import cn.hippo4j.core.executor.proxy.RejectedProxyInvocationHandler; +import cn.hippo4j.threadpool.alarm.api.ThreadPoolCheckAlarm; import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; @@ -29,6 +28,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; + import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.concurrent.atomic.AtomicLong; @@ -51,6 +51,7 @@ public class RejectedProxyInvocationHandlerTest { private RejectedProxyInvocationHandler handler; private AtomicLong rejectCount; + @Before public void setUp() { String threadPoolId = "test-pool"; diff --git a/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntimeTest.java b/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntimeTest.java index 7ff7db6e..39ce899f 100644 --- a/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntimeTest.java +++ b/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntimeTest.java @@ -17,19 +17,18 @@ package cn.hippo4j.core.executor.state; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import org.junit.Test; import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + @RunWith(MockitoJUnitRunner.class) public class AbstractThreadPoolRuntimeTest { @@ -42,24 +41,15 @@ public class AbstractThreadPoolRuntimeTest { return threadPoolRunStateInfo; } }; - final String threadPoolId = "test"; DynamicThreadPoolExecutor executor = new DynamicThreadPoolExecutor( 1, 1, 1000L, TimeUnit.MILLISECONDS, 1000L, true, 1000L, new ArrayBlockingQueue<>(1), threadPoolId, Thread::new, new ThreadPoolExecutor.DiscardOldestPolicy()); - - DynamicThreadPoolWrapper dynamicThreadPoolWrapper = DynamicThreadPoolWrapper.builder() - .threadPoolId(threadPoolId) - .executor(executor) - .build(); - GlobalThreadPoolManage.registerPool(threadPoolId, dynamicThreadPoolWrapper); - + ThreadPoolExecutorRegistry.putHolder(threadPoolId, executor, null); ThreadPoolRunStateInfo threadPoolRunStateInfo = threadPoolRuntime.getPoolRunState(threadPoolId); Assertions.assertNotNull(threadPoolRunStateInfo); - threadPoolRunStateInfo = threadPoolRuntime.getPoolRunState(threadPoolId, executor); Assertions.assertNotNull(threadPoolRunStateInfo); } - } \ No newline at end of file diff --git a/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandlerTest.java b/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandlerTest.java index 782ba281..0251b6bc 100644 --- a/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandlerTest.java +++ b/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandlerTest.java @@ -17,14 +17,14 @@ package cn.hippo4j.core.executor.state; +import cn.hippo4j.common.executor.ThreadPoolExecutorHolder; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.ManyThreadPoolRunStateInfo; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; import cn.hippo4j.common.toolkit.BeanUtil; import cn.hippo4j.common.toolkit.ByteConvertUtil; import cn.hippo4j.common.toolkit.MemoryUtil; import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -78,15 +78,15 @@ public class ThreadPoolRunStateHandlerTest { String threadPoolId = poolRunStateInfo.getTpId(); - try (final MockedStatic globalThreadPoolManage = mockStatic(GlobalThreadPoolManage.class)) { - globalThreadPoolManage.when(() -> GlobalThreadPoolManage.getExecutorService("1")).thenReturn(new DynamicThreadPoolWrapper()); - DynamicThreadPoolWrapper executorService = GlobalThreadPoolManage.getExecutorService(threadPoolId); - Assertions.assertNotNull(executorService); + try (final MockedStatic globalThreadPoolManage = mockStatic(ThreadPoolExecutorRegistry.class)) { + globalThreadPoolManage.when(() -> ThreadPoolExecutorRegistry.getHolder("1")).thenReturn(new ThreadPoolExecutorHolder()); + ThreadPoolExecutorHolder executorHolder = ThreadPoolExecutorRegistry.getHolder(threadPoolId); + Assertions.assertNotNull(executorHolder); } - DynamicThreadPoolWrapper dynamicThreadPoolWrapperMock = mock(DynamicThreadPoolWrapper.class); - when(dynamicThreadPoolWrapperMock.getExecutor()).thenReturn(new ThreadPoolExecutor(2, 2, 2000, TimeUnit.SECONDS, new SynchronousQueue<>())); - ThreadPoolExecutor pool = dynamicThreadPoolWrapperMock.getExecutor(); + ThreadPoolExecutorHolder threadPoolExecutorHolderMock = mock(ThreadPoolExecutorHolder.class); + when(threadPoolExecutorHolderMock.getExecutor()).thenReturn(new ThreadPoolExecutor(2, 2, 2000, TimeUnit.SECONDS, new SynchronousQueue<>())); + ThreadPoolExecutor pool = threadPoolExecutorHolderMock.getExecutor(); Assertions.assertNotNull(pool); String rejectedName; @@ -123,5 +123,4 @@ public class ThreadPoolRunStateHandlerTest { ByteConvertUtil.getPrintSize(max)); Assertions.assertEquals("Allocation: 54.87MB / Maximum available: 7.98GB", memoryProportion); } - } diff --git a/threadpool/message/pom.xml b/threadpool/message/pom.xml index 3bb64281..d767b29b 100644 --- a/threadpool/message/pom.xml +++ b/threadpool/message/pom.xml @@ -16,25 +16,9 @@ ${project.version} - commons-codec - commons-codec - - - org.springframework.boot - spring-boot-starter-test - test + cn.hippo4j + hippo4j-threadpool-message-core + ${project.version} - - - - - src/main/resources - - **/*.txt - **/*.json - - - - diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/config/MessageConfiguration.java b/threadpool/message/src/main/java/cn/hippo4j/message/config/MessageConfiguration.java index e495cfe4..1deda548 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/config/MessageConfiguration.java +++ b/threadpool/message/src/main/java/cn/hippo4j/message/config/MessageConfiguration.java @@ -17,14 +17,13 @@ package cn.hippo4j.message.config; -import cn.hippo4j.message.api.NotifyConfigBuilder; -import cn.hippo4j.message.platform.DingSendMessageHandler; -import cn.hippo4j.message.platform.LarkSendMessageHandler; -import cn.hippo4j.message.platform.WeChatSendMessageHandler; -import cn.hippo4j.message.service.AlarmControlHandler; -import cn.hippo4j.message.service.Hippo4jBaseSendMessageService; -import cn.hippo4j.message.service.Hippo4jSendMessageService; -import cn.hippo4j.message.service.SendMessageHandler; +import cn.hippo4j.threadpool.message.api.NotifyConfigBuilder; +import cn.hippo4j.threadpool.message.core.platform.DingSendMessageHandler; +import cn.hippo4j.threadpool.message.core.platform.LarkSendMessageHandler; +import cn.hippo4j.threadpool.message.core.platform.WeChatSendMessageHandler; +import cn.hippo4j.threadpool.message.core.service.AlarmControlHandler; +import cn.hippo4j.threadpool.message.core.service.SendMessageHandler; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolBaseSendMessageService; import org.springframework.context.annotation.Bean; /** @@ -33,9 +32,14 @@ import org.springframework.context.annotation.Bean; public class MessageConfiguration { @Bean - public Hippo4jSendMessageService hippo4jSendMessageService(NotifyConfigBuilder serverNotifyConfigBuilder, - AlarmControlHandler alarmControlHandler) { - return new Hippo4jBaseSendMessageService(serverNotifyConfigBuilder, alarmControlHandler); + public ThreadPoolBaseSendMessageService threadPoolSendMessageService(AlarmControlHandler alarmControlHandler) { + return new ThreadPoolBaseSendMessageService(alarmControlHandler); + } + + @Bean + public ThreadPoolBaseSendMessageServiceInitializer threadPoolBaseSendMessageServiceInitializer(NotifyConfigBuilder notifyConfigBuilder, + ThreadPoolBaseSendMessageService threadPoolBaseSendMessageService) { + return new ThreadPoolBaseSendMessageServiceInitializer(notifyConfigBuilder, threadPoolBaseSendMessageService); } @Bean diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/config/ThreadPoolBaseSendMessageServiceInitializer.java b/threadpool/message/src/main/java/cn/hippo4j/message/config/ThreadPoolBaseSendMessageServiceInitializer.java new file mode 100644 index 00000000..2513cf06 --- /dev/null +++ b/threadpool/message/src/main/java/cn/hippo4j/message/config/ThreadPoolBaseSendMessageServiceInitializer.java @@ -0,0 +1,45 @@ +/* + * 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.config; + +import cn.hippo4j.core.config.ApplicationContextHolder; +import cn.hippo4j.threadpool.message.api.NotifyConfigBuilder; +import cn.hippo4j.threadpool.message.api.NotifyConfigDTO; +import cn.hippo4j.threadpool.message.core.service.SendMessageHandler; +import cn.hippo4j.threadpool.message.core.service.ThreadPoolBaseSendMessageService; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.CommandLineRunner; + +import java.util.List; +import java.util.Map; + +@RequiredArgsConstructor +public class ThreadPoolBaseSendMessageServiceInitializer implements CommandLineRunner { + + private final NotifyConfigBuilder notifyConfigBuilder; + private final ThreadPoolBaseSendMessageService threadPoolBaseSendMessageService; + + @Override + public void run(String... args) throws Exception { + Map sendMessageHandlerMap = + ApplicationContextHolder.getBeansOfType(SendMessageHandler.class); + sendMessageHandlerMap.values().forEach(each -> threadPoolBaseSendMessageService.getSendMessageHandlers().put(each.getType(), each)); + Map> buildNotify = notifyConfigBuilder.buildNotify(); + threadPoolBaseSendMessageService.getNotifyConfigs().putAll(buildNotify); + } +} diff --git a/threadpool/monitor/base/src/main/java/cn/hippo4j/monitor/base/AbstractDynamicThreadPoolMonitor.java b/threadpool/monitor/base/src/main/java/cn/hippo4j/monitor/base/AbstractDynamicThreadPoolMonitor.java index 2e8a77ac..7b55f155 100644 --- a/threadpool/monitor/base/src/main/java/cn/hippo4j/monitor/base/AbstractDynamicThreadPoolMonitor.java +++ b/threadpool/monitor/base/src/main/java/cn/hippo4j/monitor/base/AbstractDynamicThreadPoolMonitor.java @@ -17,8 +17,8 @@ package cn.hippo4j.monitor.base; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; -import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler; import javax.annotation.Resource; @@ -41,7 +41,7 @@ public abstract class AbstractDynamicThreadPoolMonitor implements DynamicThreadP @Override public void collect() { - List listDynamicThreadPoolId = GlobalThreadPoolManage.listThreadPoolId(); + List listDynamicThreadPoolId = ThreadPoolExecutorRegistry.listThreadPoolExecutorId(); listDynamicThreadPoolId.forEach(each -> execute(threadPoolRunStateHandler.getPoolRunState(each))); } }