From 8eaf587f6ab9c2081b1d76eca3654c502490dc96 Mon Sep 17 00:00:00 2001 From: pizihao <48643103+pizihao@users.noreply.github.com> Date: Mon, 24 Oct 2022 21:15:37 +0800 Subject: [PATCH] organize the toolkit, add references and reference sources (#835) * fix : organize the toolkit, add references and reference sources * fix : add MemoryUtil for get memory info * fix : code format adjustment --- .../web/WebThreadPoolRunStateHandler.java | 23 ++--- .../cn/hippo4j/common/toolkit/ArrayUtil.java | 3 +- .../cn/hippo4j/common/toolkit/BeanUtil.java | 11 ++- .../common/toolkit/CollectionUtil.java | 3 +- .../hippo4j/common/toolkit/ContentUtil.java | 5 +- .../cn/hippo4j/common/toolkit/DateUtil.java | 2 - .../cn/hippo4j/common/toolkit/FileUtil.java | 35 +++---- .../cn/hippo4j/common/toolkit/GroupKey.java | 1 + .../cn/hippo4j/common/toolkit/IdUtil.java | 24 ++++- .../cn/hippo4j/common/toolkit/Joiner.java | 3 +- .../cn/hippo4j/common/toolkit/MapUtil.java | 40 +++++++- .../cn/hippo4j/common/toolkit/Md5Util.java | 17 ++-- .../cn/hippo4j/common/toolkit/MemoryUtil.java | 94 +++++++++++++++++++ .../hippo4j/common/toolkit/ReflectUtil.java | 7 +- .../cn/hippo4j/common/toolkit/StringUtil.java | 12 ++- .../hippo4j/common}/toolkit/MapUtilTest.java | 24 +++-- .../common/toolkit/MemoryUtilTest.java | 60 ++++++++++++ .../common/toolkit/http/HttpUtilsTest.java | 4 +- .../state/ThreadPoolRunStateHandler.java | 22 ++--- .../hippo4j/core/proxy/RejectedProxyUtil.java | 7 +- .../cn/hippo4j/core/toolkit/SystemClock.java | 3 +- .../inet/DynamicThreadPoolAnnotationUtil.java | 15 +-- .../hippo4j/core/toolkit/inet/InetUtils.java | 5 +- .../hippo4j/config/notify/NotifyCenter.java | 4 +- .../config/service/ConfigCacheService.java | 7 +- .../config/service/LongPollingService.java | 6 +- .../cn/hippo4j/config/toolkit/MapUtil.java | 82 ---------------- ...bstractConfigThreadPoolDynamicRefresh.java | 6 +- 28 files changed, 315 insertions(+), 210 deletions(-) create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/MemoryUtil.java rename {hippo4j-server/hippo4j-config/src/test/java/cn/hippo4j/config => hippo4j-common/src/test/java/cn/hippo4j/common}/toolkit/MapUtilTest.java (85%) create mode 100644 hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/MemoryUtilTest.java delete mode 100644 hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/MapUtil.java diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolRunStateHandler.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolRunStateHandler.java index 2cee24ef..2727e52c 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolRunStateHandler.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolRunStateHandler.java @@ -19,13 +19,11 @@ package cn.hippo4j.adapter.web; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; import cn.hippo4j.common.toolkit.ByteConvertUtil; +import cn.hippo4j.common.toolkit.MemoryUtil; +import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.core.executor.state.AbstractThreadPoolRuntime; import lombok.extern.slf4j.Slf4j; -import java.lang.management.ManagementFactory; -import java.lang.management.MemoryMXBean; -import java.lang.management.MemoryUsage; - /** * Web thread pool run state handler. */ @@ -34,16 +32,13 @@ public class WebThreadPoolRunStateHandler extends AbstractThreadPoolRuntime { @Override public ThreadPoolRunStateInfo supplement(ThreadPoolRunStateInfo poolRunStateInfo) { - MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); - MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); - long used = heapMemoryUsage.getUsed(); - long max = heapMemoryUsage.getMax(); - String memoryProportion = new StringBuilder() - .append("已分配: ") - .append(ByteConvertUtil.getPrintSize(used)) - .append(" / 最大可用: ") - .append(ByteConvertUtil.getPrintSize(max)) - .toString(); + long used = MemoryUtil.heapMemoryUsed(); + long max = MemoryUtil.heapMemoryMax(); + String memoryProportion = StringUtil.newBuilder( + "已分配: ", + ByteConvertUtil.getPrintSize(used), + " / 最大可用: ", + ByteConvertUtil.getPrintSize(max)); poolRunStateInfo.setCurrentLoad(poolRunStateInfo.getCurrentLoad() + "%"); poolRunStateInfo.setPeakLoad(poolRunStateInfo.getPeakLoad() + "%"); poolRunStateInfo.setMemoryProportion(memoryProportion); diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ArrayUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ArrayUtil.java index bf7d3bde..16a32e28 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ArrayUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ArrayUtil.java @@ -22,7 +22,8 @@ import cn.hippo4j.common.function.Matcher; import java.lang.reflect.Array; /** - * Array util. + * Array util.
+ * Refer to cn.hutool.core.util.ArrayUtil:
*/ public class ArrayUtil { diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/BeanUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/BeanUtil.java index 523f56a9..a952351f 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/BeanUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/BeanUtil.java @@ -22,7 +22,6 @@ import com.github.dozermapper.core.DozerBeanMapperBuilder; import com.github.dozermapper.core.Mapper; import lombok.AccessLevel; import lombok.NoArgsConstructor; -import org.springframework.beans.BeanUtils; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; @@ -30,7 +29,8 @@ import java.lang.reflect.Method; import java.util.*; /** - * Bean util. + * Bean util.
+ * use com.github.dozermapper */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class BeanUtil { @@ -73,6 +73,13 @@ public class BeanUtil { .orElse(null); } + /** + * map to bean + * + * @param map source data + * @param clazz type + * @param toCamelCase key convert + */ public static T mapToBean(Map map, Class clazz, boolean toCamelCase) { if (clazz == null) { return null; diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/CollectionUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/CollectionUtil.java index baa5a02f..d694924a 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/CollectionUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/CollectionUtil.java @@ -21,7 +21,8 @@ import java.util.*; import java.util.stream.Collectors; /** - * Collection util. + * Collection util.
+ * Refer to cn.hutool.core.collection.CollUtil:
*/ public class CollectionUtil { diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java index 430db1c1..13e3151d 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java @@ -46,14 +46,13 @@ public class ContentUtil { } public static String getGroupKey(ThreadPoolParameter parameter) { - StringBuilder stringBuilder = new StringBuilder(); - String resultStr = stringBuilder.append(parameter.getTpId()) + return StringUtil.createBuilder() + .append(parameter.getTpId()) .append(Constants.GROUP_KEY_DELIMITER) .append(parameter.getItemId()) .append(Constants.GROUP_KEY_DELIMITER) .append(parameter.getTenantId()) .toString(); - return resultStr; } public static String getGroupKey(String... parameters) { diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/DateUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/DateUtil.java index 7db6d7c5..dc3e8ddc 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/DateUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/DateUtil.java @@ -23,9 +23,7 @@ import lombok.NoArgsConstructor; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; -import java.time.ZoneOffset; import java.util.Date; -import java.util.SimpleTimeZone; import java.util.TimeZone; /** diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/FileUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/FileUtil.java index 1e7d971f..9785698c 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/FileUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/FileUtil.java @@ -23,7 +23,6 @@ import org.springframework.core.io.ClassPathResource; import java.io.*; import java.nio.charset.Charset; -import java.nio.file.Files; import java.util.ArrayList; import java.util.List; @@ -52,36 +51,24 @@ public class FileUtil { return resultReadStr; } + /** + * According to the line read + * + * @param path the path + * @param charset the charset + */ public static List readLines(String path, Charset charset) { List strList = new ArrayList<>(); - InputStreamReader inputStreamReader = null; - BufferedReader bufferedReader = null; ClassPathResource classPathResource = new ClassPathResource(path); - try { - inputStreamReader = new InputStreamReader(classPathResource.getInputStream(), charset); - bufferedReader = new BufferedReader(inputStreamReader); + try ( + InputStreamReader in = new InputStreamReader(classPathResource.getInputStream(), charset); + BufferedReader reader = new BufferedReader(in)) { String line; - while ((line = bufferedReader.readLine()) != null) { + while ((line = reader.readLine()) != null) { strList.add(line); } } catch (IOException e) { - e.printStackTrace(); - throw new IllegalException("file read error"); - } finally { - if (inputStreamReader != null) { - try { - inputStreamReader.close(); - } catch (IOException e) { - throw new IllegalException("file read error"); - } - } - if (bufferedReader != null) { - try { - bufferedReader.close(); - } catch (IOException e) { - throw new IllegalException("file read error"); - } - } + throw new IllegalException("file read error", e); } return strList; } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/GroupKey.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/GroupKey.java index 227bba06..cea1792e 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/GroupKey.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/GroupKey.java @@ -24,6 +24,7 @@ import static cn.hippo4j.common.constant.Constants.GROUP_KEY_DELIMITER; /** * Group key. + * Refer to com.alibaba.nacos.client.config.common.GroupKey:
*/ public class GroupKey { diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/IdUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/IdUtil.java index e9bf9a50..26fad185 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/IdUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/IdUtil.java @@ -47,10 +47,28 @@ public class IdUtil { } /** - * toString + * Returns a {@code String} object representing this {@code UUID}. * - * @param uuid UUID - * @return UUID String + *

The UUID string representation is as described by this BNF: + *

+     * {@code
+     * UUID                   =  "-"  "-"
+     *                           "-"
+     *                           "-"
+     *                          
+     * time_low               = 4*
+     * time_mid               = 2*
+     * time_high_and_version  = 2*
+     * variant_and_sequence   = 2*
+     * node                   = 6*
+     * hexOctet               = 
+     * hexDigit               =
+     *       "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
+     *       | "a" | "b" | "c" | "d" | "e" | "f"
+     *       | "A" | "B" | "C" | "D" | "E" | "F"
+     * }
+ * + * @return A string representation of this {@code UUID} */ public static String toString(UUID uuid, boolean isSimple) { long mostSigBits = uuid.getMostSignificantBits(); diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Joiner.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Joiner.java index 05eefe1f..de798647 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Joiner.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Joiner.java @@ -23,7 +23,8 @@ import java.util.Iterator; import java.util.Objects; /** - * reference google guava + * reference google guava
+ * com.google.common.base.Joiner */ public class Joiner { diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/MapUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/MapUtil.java index 7436cfda..f23b0a04 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/MapUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/MapUtil.java @@ -17,10 +17,7 @@ package cn.hippo4j.common.toolkit; -import java.util.Collection; -import java.util.Dictionary; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.function.BiFunction; import java.util.function.Predicate; @@ -151,6 +148,41 @@ public class MapUtil { return val; } + /** + * Fuzzy matching based on Key. + * + * @param sourceMap + * @param filters + * @return + */ + public static List parseMapForFilter(Map sourceMap, String filters) { + List resultList = new ArrayList<>(); + if (CollectionUtil.isEmpty(sourceMap)) { + return resultList; + } + sourceMap.forEach((key, val) -> { + if (checkKey(key, filters)) { + resultList.add(key); + } + }); + return resultList; + } + + /** + * Match the characters you want to query. + * + * @param key + * @param filters + * @return + */ + private static boolean checkKey(String key, String filters) { + if (key.indexOf(filters) > -1) { + return true; + } else { + return false; + } + } + /** * remove value, Thread safety depends on whether the Map is a thread-safe Map. * diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Md5Util.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Md5Util.java index 931f683d..1e410b02 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Md5Util.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Md5Util.java @@ -28,6 +28,7 @@ import java.util.List; /** * MD5 util. + * Refer to com.alibaba.nacos.common.util.Md5Utils:
*/ public class Md5Util { @@ -96,16 +97,12 @@ public class Md5Util { sb.append(Constants.WORD_SEPARATOR); sb.append(dataIdGroupId[1]); // if have tenant, then set it - if (dataIdGroupId.length == DATA_ID_GROUP_ID_THREE_LEN) { - if (StringUtil.isNotBlank(dataIdGroupId[2])) { - sb.append(Constants.WORD_SEPARATOR); - sb.append(dataIdGroupId[2]); - } - } else if (dataIdGroupId.length == DATA_ID_GROUP_ID_FOUR_LEN) { - if (StringUtil.isNotBlank(dataIdGroupId[2])) { - sb.append(Constants.WORD_SEPARATOR); - sb.append(dataIdGroupId[2]); - } + boolean b = (dataIdGroupId.length == DATA_ID_GROUP_ID_THREE_LEN + || dataIdGroupId.length == DATA_ID_GROUP_ID_FOUR_LEN) + && StringUtil.isNotBlank(dataIdGroupId[2]); + if (b) { + sb.append(Constants.WORD_SEPARATOR); + sb.append(dataIdGroupId[2]); } sb.append(Constants.LINE_SEPARATOR); } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/MemoryUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/MemoryUtil.java new file mode 100644 index 00000000..3b62bab9 --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/MemoryUtil.java @@ -0,0 +1,94 @@ +/* + * 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.toolkit; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; +import java.lang.management.MemoryUsage; + +/** + * memory util
+ * the obtained information is not invalid, after a long wait, obtain it again + * + * @author liuwenhao + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class MemoryUtil { + + static MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); + static MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); + static MemoryUsage noHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage(); + + /** + * get used memory in heap + * + * @return long bytes + */ + public static long heapMemoryUsed() { + return heapMemoryUsage.getUsed(); + } + + /** + * get max memory in heap + * + * @return long bytes + */ + public static long heapMemoryMax() { + return heapMemoryUsage.getMax(); + } + + /** + * get free memory in heap + * + * @return long bytes + */ + public static long heapMemoryFree() { + return Math.subtractExact(heapMemoryMax(), heapMemoryUsed()); + } + + /** + * get used memory in no-heap + * + * @return long bytes + */ + public static long noHeapMemoryUsed() { + return noHeapMemoryUsage.getUsed(); + } + + /** + * get max memory in no-heap + * + * @return long bytes + */ + public static long noHeapMemoryMax() { + return noHeapMemoryUsage.getMax(); + } + + /** + * get free memory in no-heap + * + * @return long bytes + */ + public static long noHeapMemoryFree() { + return Math.subtractExact(noHeapMemoryMax(), noHeapMemoryUsed()); + } + +} diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ReflectUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ReflectUtil.java index 35ae9095..9a4bfa86 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ReflectUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ReflectUtil.java @@ -29,10 +29,13 @@ import java.util.Arrays; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ThreadPoolExecutor; /** - * Reflect util. + * Reflect util.
+ * Refer to cn.hutool.core.util.ReflectUtil:
+ * {@link this#getFieldsDirectly(Class, boolean)}
+ * {@link this#setFieldValue(Object, Field, Object)}
+ * {@link this#getDefaultValue(Class)}
*/ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class ReflectUtil { diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java index c9fcde40..1134b34b 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java @@ -21,7 +21,11 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * String util. + * String util.
+ * Refer to cn.hutool.core.util.StrUtil:
+ * {@link this#toSymbolCase(CharSequence, char)}
+ * {@link this#toCamelCase(CharSequence, char)}
+ * {@link this#subBefore(String, String)}
*/ public class StringUtil { @@ -53,8 +57,9 @@ public class StringUtil { /** * Returns {@code true} if the given string is null or is the empty string. - * + *

* this method has been deprecated, use isEmpty() instead. + * * @param str * @return */ @@ -302,7 +307,8 @@ public class StringUtil { * StringUtils.split("abc def", " ") = ["abc", "def"] * StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"] * - * @param str the String to parse, may be null + * + * @param str the String to parse, may be null * @param separatorChars the characters used as the delimiters, * @return an array of parsed Strings */ diff --git a/hippo4j-server/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/MapUtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/MapUtilTest.java similarity index 85% rename from hippo4j-server/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/MapUtilTest.java rename to hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/MapUtilTest.java index 440b84dd..b0c00d04 100644 --- a/hippo4j-server/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/MapUtilTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/MapUtilTest.java @@ -15,10 +15,8 @@ * limitations under the License. */ -package cn.hippo4j.config.toolkit; +package cn.hippo4j.common.toolkit; -import cn.hippo4j.common.toolkit.Assert; -import cn.hippo4j.common.toolkit.CollectionUtil; import org.junit.Test; import java.util.HashMap; @@ -66,9 +64,9 @@ public class MapUtilTest { @Test public void computeIfAbsentNotExistKeyTest() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("abc", "123"); - BiFunction mappingFunction = (a, b) -> a + b; + BiFunction mappingFunction = (a, b) -> a + b; try { MapUtil.computeIfAbsent(map, null, mappingFunction, "param1", "param2"); } catch (Exception e) { @@ -93,9 +91,9 @@ public class MapUtilTest { @Test public void computeIfAbsentNotExistParam1Test() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("abc", "123"); - BiFunction mappingFunction = (a, b) -> a + b; + BiFunction mappingFunction = (a, b) -> a + b; try { MapUtil.computeIfAbsent(map, "abc", mappingFunction, null, "param2"); } catch (Exception e) { @@ -107,9 +105,9 @@ public class MapUtilTest { @Test public void computeIfAbsentNotExistParam2Test() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("abc", "123"); - BiFunction mappingFunction = (a, b) -> a + b; + BiFunction mappingFunction = (a, b) -> a + b; try { MapUtil.computeIfAbsent(map, "abc", mappingFunction, "param1", null); } catch (Exception e) { @@ -121,18 +119,18 @@ public class MapUtilTest { @Test public void computeIfAbsentValNotNullTest() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("abc", "123"); - BiFunction mappingFunction = (a, b) -> a + b; + BiFunction mappingFunction = (a, b) -> a + b; Object ret = MapUtil.computeIfAbsent(map, "abc", mappingFunction, "param1", "param2"); Assert.isTrue(Objects.equals("123", String.valueOf(ret))); } @Test public void computeIfAbsentValIsNullTest() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("abc", "123"); - BiFunction mappingFunction = (a, b) -> a + b; + BiFunction mappingFunction = (a, b) -> a + b; Object ret = MapUtil.computeIfAbsent(map, "xyz", mappingFunction, "param1", "param2"); Assert.isTrue(Objects.equals("param1param2", String.valueOf(ret))); } diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/MemoryUtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/MemoryUtilTest.java new file mode 100644 index 00000000..3009804e --- /dev/null +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/MemoryUtilTest.java @@ -0,0 +1,60 @@ +/* + * 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.toolkit; + +import org.junit.Assert; +import org.junit.Test; + +public class MemoryUtilTest { + + @Test + public void heapMemoryUsed() { + long memoryUsed = MemoryUtil.heapMemoryUsed(); + Assert.assertNotEquals(0, memoryUsed); + } + + @Test + public void heapMemoryMax() { + long memoryUsed = MemoryUtil.heapMemoryMax(); + Assert.assertNotEquals(0, memoryUsed); + } + + @Test + public void heapMemoryFree() { + long memoryUsed = MemoryUtil.heapMemoryFree(); + Assert.assertNotEquals(0, memoryUsed); + } + + @Test + public void noHeapMemoryUsed() { + long memoryUsed = MemoryUtil.noHeapMemoryUsed(); + Assert.assertNotEquals(0, memoryUsed); + } + + @Test + public void noHeapMemoryMax() { + long memoryUsed = MemoryUtil.noHeapMemoryMax(); + Assert.assertNotEquals(0, memoryUsed); + } + + @Test + public void noHeapMemoryFree() { + long memoryUsed = MemoryUtil.noHeapMemoryFree(); + Assert.assertNotEquals(0, memoryUsed); + } +} \ No newline at end of file diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HttpUtilsTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HttpUtilsTest.java index 105228d6..30b2b028 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HttpUtilsTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HttpUtilsTest.java @@ -72,14 +72,14 @@ public class HttpUtilsTest { Assert.assertNotNull(data); } - @Test + @Test(expected = SocketTimeoutException.class) public void testRestApiPostTimeout() { String loginUrl = postUrl + "auth/login"; LoginInfo loginInfo = new LoginInfo(); loginInfo.setPassword("hippo4j"); loginInfo.setUsername("hippo4j"); loginInfo.setRememberMe(1); - Assert.assertThrows(SocketTimeoutException.class, () -> HttpUtil.post(loginUrl, loginInfo, 1, Result.class)); + HttpUtil.post(loginUrl, loginInfo, 1, Result.class); } @Test diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java index 62b9b1ab..ca9d7eaf 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java @@ -21,6 +21,8 @@ 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.DynamicThreadPoolExecutor; import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; @@ -30,9 +32,6 @@ import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.core.env.ConfigurableEnvironment; -import java.lang.management.ManagementFactory; -import java.lang.management.MemoryMXBean; -import java.lang.management.MemoryUsage; import java.util.concurrent.ThreadPoolExecutor; import static cn.hippo4j.core.toolkit.IdentifyUtil.CLIENT_IDENTIFICATION_VALUE; @@ -50,16 +49,13 @@ public class ThreadPoolRunStateHandler extends AbstractThreadPoolRuntime { @Override public ThreadPoolRunStateInfo supplement(ThreadPoolRunStateInfo poolRunStateInfo) { - MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); - MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); - long used = heapMemoryUsage.getUsed(); - long max = heapMemoryUsage.getMax(); - String memoryProportion = new StringBuilder() - .append("已分配: ") - .append(ByteConvertUtil.getPrintSize(used)) - .append(" / 最大可用: ") - .append(ByteConvertUtil.getPrintSize(max)) - .toString(); + long used = MemoryUtil.heapMemoryUsed(); + long max = MemoryUtil.heapMemoryMax(); + String memoryProportion = StringUtil.newBuilder( + "已分配: ", + ByteConvertUtil.getPrintSize(used), + " / 最大可用: ", + ByteConvertUtil.getPrintSize(max)); poolRunStateInfo.setCurrentLoad(poolRunStateInfo.getCurrentLoad() + "%"); poolRunStateInfo.setPeakLoad(poolRunStateInfo.getPeakLoad() + "%"); String ipAddress = hippo4JInetUtils.findFirstNonLoopBackHostInfo().getIpAddress(); diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyUtil.java b/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyUtil.java index 432ddbad..4c0b2db9 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyUtil.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/proxy/RejectedProxyUtil.java @@ -17,6 +17,9 @@ package cn.hippo4j.core.proxy; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + import java.lang.reflect.Proxy; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.atomic.AtomicLong; @@ -24,6 +27,7 @@ import java.util.concurrent.atomic.AtomicLong; /** * Rejected proxy util. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class RejectedProxyUtil { /** @@ -35,11 +39,10 @@ public class RejectedProxyUtil { * @return */ public static RejectedExecutionHandler createProxy(RejectedExecutionHandler rejectedExecutionHandler, String threadPoolId, AtomicLong rejectedNum) { - RejectedExecutionHandler rejectedProxy = (RejectedExecutionHandler) Proxy + return (RejectedExecutionHandler) Proxy .newProxyInstance( rejectedExecutionHandler.getClass().getClassLoader(), new Class[]{RejectedExecutionHandler.class}, new RejectedProxyInvocationHandler(rejectedExecutionHandler, threadPoolId, rejectedNum)); - return rejectedProxy; } } diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/SystemClock.java b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/SystemClock.java index 473148b3..b2f54c7a 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/SystemClock.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/SystemClock.java @@ -22,7 +22,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; /** - * System clock. + * System clock.
+ * Refer to cn.hutool.core.date.SystemClock
*/ public class SystemClock { diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/DynamicThreadPoolAnnotationUtil.java b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/DynamicThreadPoolAnnotationUtil.java index d11b2fd3..20d267aa 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/DynamicThreadPoolAnnotationUtil.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/DynamicThreadPoolAnnotationUtil.java @@ -18,6 +18,8 @@ package cn.hippo4j.core.toolkit.inet; import cn.hippo4j.common.config.ApplicationContextHolder; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -39,24 +41,23 @@ import java.util.Optional; * @author chen.ma * @date 2022/1/5 21:15 */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class DynamicThreadPoolAnnotationUtil { /** * 根据 {@param beanName} 查询注解 {@param annotationType} 是否存在. * - * @param beanName - * @param annotationType - * @param - * @return + * @param beanName bean name + * @param annotationType annotation class + * @param the Annotation type */ public static A findAnnotationOnBean(String beanName, Class annotationType) { AbstractApplicationContext context = (AbstractApplicationContext) ApplicationContextHolder.getInstance(); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); - A annotation = Optional.ofNullable(beanFactory) + return Optional.of(beanFactory) .map(each -> (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName)) - .map(definition -> definition.getResolvedFactoryMethod()) + .map(RootBeanDefinition::getResolvedFactoryMethod) .map(factoryMethod -> AnnotationUtils.getAnnotation(factoryMethod, annotationType)) .orElse(null); - return annotation; } } diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/InetUtils.java b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/InetUtils.java index 46cb7e32..babc8e55 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/InetUtils.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/InetUtils.java @@ -35,7 +35,8 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** - * Inet utils. + * Inet utils.
+ * Refer to org.springframework.cloud.commons.util.InetUtils
*/ public class InetUtils implements Closeable { @@ -80,7 +81,7 @@ public class InetUtils implements Closeable { this.log.trace("Testing interface: " + ifc.getDisplayName()); if (ifc.getIndex() < lowest || result == null) { lowest = ifc.getIndex(); - } else if (result != null) { + } else { continue; } // @formatter:off diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/notify/NotifyCenter.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/notify/NotifyCenter.java index bd5d04fa..0fce3453 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/notify/NotifyCenter.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/notify/NotifyCenter.java @@ -17,12 +17,12 @@ package cn.hippo4j.config.notify; +import cn.hippo4j.common.toolkit.MapUtil; import cn.hippo4j.config.event.AbstractEvent; +import cn.hippo4j.config.event.AbstractSlowEvent; import cn.hippo4j.config.notify.listener.AbstractSmartSubscriber; import cn.hippo4j.config.notify.listener.AbstractSubscriber; import cn.hippo4j.config.toolkit.ClassUtil; -import cn.hippo4j.config.toolkit.MapUtil; -import cn.hippo4j.config.event.AbstractSlowEvent; import lombok.extern.slf4j.Slf4j; import java.util.Map; diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java index 825d5097..082c25ca 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java @@ -22,17 +22,12 @@ import cn.hippo4j.common.constant.Constants; import cn.hippo4j.common.design.observer.AbstractSubjectCenter; import cn.hippo4j.common.design.observer.Observer; import cn.hippo4j.common.design.observer.ObserverMessage; -import cn.hippo4j.common.toolkit.CollectionUtil; -import cn.hippo4j.common.toolkit.JSONUtil; -import cn.hippo4j.common.toolkit.Joiner; -import cn.hippo4j.common.toolkit.Md5Util; -import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.common.toolkit.*; import cn.hippo4j.config.event.LocalDataChangeEvent; import cn.hippo4j.config.model.CacheItem; import cn.hippo4j.config.model.ConfigAllInfo; import cn.hippo4j.config.notify.NotifyCenter; import cn.hippo4j.config.service.biz.ConfigService; -import cn.hippo4j.config.toolkit.MapUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java index 5e8fd0fd..426e47c3 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java @@ -17,17 +17,13 @@ package cn.hippo4j.config.service; -import cn.hippo4j.common.toolkit.CollectionUtil; -import cn.hippo4j.common.toolkit.JSONUtil; -import cn.hippo4j.common.toolkit.Md5Util; -import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.common.toolkit.*; import cn.hippo4j.common.web.base.Results; import cn.hippo4j.config.event.AbstractEvent; import cn.hippo4j.config.event.LocalDataChangeEvent; import cn.hippo4j.config.notify.NotifyCenter; import cn.hippo4j.config.notify.listener.AbstractSubscriber; import cn.hippo4j.config.toolkit.ConfigExecutor; -import cn.hippo4j.config.toolkit.MapUtil; import cn.hippo4j.config.toolkit.Md5ConfigUtil; import cn.hippo4j.config.toolkit.RequestUtil; import lombok.SneakyThrows; diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/MapUtil.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/MapUtil.java deleted file mode 100644 index f66aef67..00000000 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/MapUtil.java +++ /dev/null @@ -1,82 +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.config.toolkit; - -import cn.hippo4j.common.toolkit.CollectionUtil; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.function.BiFunction; - -/** - * Map util. - */ -public class MapUtil { - - public static Object computeIfAbsent(Map target, Object key, BiFunction mappingFunction, Object param1, Object param2) { - Objects.requireNonNull(target, "target"); - Objects.requireNonNull(key, "key"); - Objects.requireNonNull(mappingFunction, "mappingFunction"); - Objects.requireNonNull(param1, "param1"); - Objects.requireNonNull(param2, "param2"); - Object val = target.get(key); - if (val == null) { - Object ret = mappingFunction.apply(param1, param2); - target.put(key, ret); - return ret; - } - return val; - } - - /** - * Fuzzy matching based on Key. - * - * @param sourceMap - * @param filters - * @return - */ - public static List parseMapForFilter(Map sourceMap, String filters) { - List resultList = new ArrayList<>(); - if (CollectionUtil.isEmpty(sourceMap)) { - return resultList; - } - sourceMap.forEach((key, val) -> { - if (checkKey(key, filters)) { - resultList.add(key); - } - }); - return resultList; - } - - /** - * Match the characters you want to query. - * - * @param key - * @param filters - * @return - */ - private static boolean checkKey(String key, String filters) { - if (key.indexOf(filters) > -1) { - return true; - } else { - return false; - } - } -} diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java index a3e410c7..f78e5873 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java @@ -36,11 +36,7 @@ import java.util.concurrent.ExecutorService; * Abstract core thread-pool dynamic refresh. */ @Slf4j -public abstract class AbstractConfigThreadPoolDynamicRefresh - implements - ThreadPoolDynamicRefresh, - ThreadPoolInitRefresh, - InitializingBean { +public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, ThreadPoolInitRefresh, InitializingBean { private final BootstrapConfigPropertiesBinderAdapt bootstrapConfigPropertiesBinderAdapt;