From 5a9a16d1e406275a9ab6e7c0d8f3d95980898ff4 Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Tue, 21 Mar 2023 15:22:42 +0800 Subject: [PATCH] Add application profile enum. --- .../config/ApplicationContextHolder.java | 9 +++++ .../cn/hippo4j/common/enums/ProfileEnum.java | 39 +++++++++++++++++++ .../WebThreadPoolConfigChangeHandler.java | 12 +++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java b/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java index 5c2eb3fe..e75df41a 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/config/ApplicationContextHolder.java @@ -17,6 +17,7 @@ package cn.hippo4j.common.config; +import cn.hippo4j.common.enums.ProfileEnum; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -90,4 +91,12 @@ public class ApplicationContextHolder implements ApplicationContextAware { public static ApplicationContext getInstance() { return CONTEXT; } + + /** + * Get current application profile. + */ + public static ProfileEnum getActiveProfile() { + return ProfileEnum.of(CONTEXT.getEnvironment().getActiveProfiles()[0]); + } + } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java b/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java new file mode 100644 index 00000000..4d08040c --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/enums/ProfileEnum.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.enums; + +import java.util.Arrays; + +/** + * Application profile enum. + */ +public enum ProfileEnum { + + UNKNOWN, + DEV, + TEST01, + PRE, + PROD; + + public static ProfileEnum of(String profileStr) { + return Arrays.stream(values()) + .filter(v -> v.name().equalsIgnoreCase(profileStr)) + .findFirst() + .orElse(UNKNOWN); + } +} diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java index 56b8c448..70e8c9f1 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/service/WebThreadPoolConfigChangeHandler.java @@ -33,20 +33,28 @@ 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; /** - * Send pool config change. + * Send pool config change message for web. * * @param requestParam change parameter notify request */ @Override public void sendPoolConfigChange(WebChangeParameterNotifyRequest requestParam) { try { - requestParam.setAppName(applicationName); + requestParam.setActive(active.toUpperCase()); + String appName = StringUtil.isBlank(itemId) ? applicationName : itemId; + requestParam.setAppName(appName); requestParam.setIdentify(IdentifyUtil.getIdentify()); hippo4jSendMessageService.sendChangeMessage(requestParam); } catch (Throwable th) {