diff --git a/hippo4j-common/pom.xml b/hippo4j-common/pom.xml index 65042ef1..44b46e4d 100644 --- a/hippo4j-common/pom.xml +++ b/hippo4j-common/pom.xml @@ -71,6 +71,11 @@ spring-boot-starter-test test + + + com.github.ben-manes.caffeine + caffeine + 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 new file mode 100644 index 00000000..70dd23fa --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/Joiner.java @@ -0,0 +1,90 @@ +/* + * 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 java.io.IOException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Objects; + +/** + * reference google guava + * + */ +public class Joiner { + + private final String separator; + + private Joiner(String separator) { + this.separator = Objects.requireNonNull(separator); + } + + /** Returns a joiner which automatically places {@code separator} between consecutive elements. */ + public static Joiner on(String separator) { + return new Joiner(separator); + } + + /** + * Returns a string containing the string representation of each of {@code parts}, using the + * previously configured separator between each. + * + */ + public final String join(Object[] parts) { + return join(Arrays.asList(parts)); + } + + public final String join(Iterable parts) { + return join(parts.iterator()); + } + + /** + * Returns a string containing the string representation of each of {@code parts}, using the + * previously configured separator between each. + * + */ + public final String join(Iterator parts) { + return appendTo(new StringBuilder(), parts).toString(); + } + + public final StringBuilder appendTo(StringBuilder builder, Iterator parts) { + try { + appendTo((Appendable) builder, parts); + } catch (IOException impossible) { + throw new AssertionError(impossible); + } + return builder; + } + + public A appendTo(A appendable, Iterator parts) throws IOException { + Objects.requireNonNull(appendable); + if (parts.hasNext()) { + appendable.append(toString(parts.next())); + while (parts.hasNext()) { + appendable.append(separator); + appendable.append(toString(parts.next())); + } + } + return appendable; + } + + CharSequence toString(Object part) { + Objects.requireNonNull(part); + return (part instanceof CharSequence) ? (CharSequence) part : part.toString(); + } + +} 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 6072a586..74cccf9d 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 @@ -17,11 +17,6 @@ package cn.hippo4j.common.toolkit; -import java.io.IOException; -import java.util.Arrays; -import java.util.Iterator; -import java.util.Objects; - /** * String util. */ @@ -197,71 +192,4 @@ public class StringUtil { return sb.toString(); } - /** - * reference google guava - * - */ - public static class Joiner { - - private final String separator; - - private Joiner(String separator) { - this.separator = Objects.requireNonNull(separator); - } - - /** Returns a joiner which automatically places {@code separator} between consecutive elements. */ - public static Joiner on(String separator) { - return new Joiner(separator); - } - - /** - * Returns a string containing the string representation of each of {@code parts}, using the - * previously configured separator between each. - * - */ - public final String join(Object[] parts) { - return join(Arrays.asList(parts)); - } - - public final String join(Iterable parts) { - return join(parts.iterator()); - } - - /** - * Returns a string containing the string representation of each of {@code parts}, using the - * previously configured separator between each. - * - */ - public final String join(Iterator parts) { - return appendTo(new StringBuilder(), parts).toString(); - } - - public final StringBuilder appendTo(StringBuilder builder, Iterator parts) { - try { - appendTo((Appendable) builder, parts); - } catch (IOException impossible) { - throw new AssertionError(impossible); - } - return builder; - } - - public A appendTo(A appendable, Iterator parts) throws IOException { - Objects.requireNonNull(appendable); - if (parts.hasNext()) { - appendable.append(toString(parts.next())); - while (parts.hasNext()) { - appendable.append(separator); - appendable.append(toString(parts.next())); - } - } - return appendable; - } - - CharSequence toString(Object part) { - Objects.requireNonNull(part); - return (part instanceof CharSequence) ? (CharSequence) part : part.toString(); - } - - } - } diff --git a/hippo4j-config/pom.xml b/hippo4j-config/pom.xml index 61cd12de..c145134f 100644 --- a/hippo4j-config/pom.xml +++ b/hippo4j-config/pom.xml @@ -15,6 +15,12 @@ + + cn.hippo4j + hippo4j-common + ${revision} + + org.springframework.boot spring-boot-starter diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java b/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java index 7e9a475b..3d154c72 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java @@ -24,8 +24,8 @@ 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.config.event.LocalDataChangeEvent; import cn.hippo4j.config.model.CacheItem; import cn.hippo4j.config.model.ConfigAllInfo; @@ -148,7 +148,7 @@ public class ConfigCacheService { public static List getIdentifyList(String tenantId, String itemId, String threadPoolId) { List identifyList = null; - String buildKey = StringUtil.Joiner.on(GROUP_KEY_DELIMITER).join(CollectionUtil.newArrayList(threadPoolId, itemId, tenantId)); + String buildKey = Joiner.on(GROUP_KEY_DELIMITER).join(CollectionUtil.newArrayList(threadPoolId, itemId, tenantId)); List keys = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, buildKey); if (CollectionUtil.isNotEmpty(keys)) { identifyList = new ArrayList<>(keys.size()); diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigServletInner.java b/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigServletInner.java index 8dbeca72..48457554 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigServletInner.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigServletInner.java @@ -17,8 +17,8 @@ package cn.hippo4j.config.service; -import cn.hutool.cache.Cache; -import cn.hutool.cache.CacheUtil; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -39,7 +39,9 @@ public class ConfigServletInner { @NonNull private final LongPollingService longPollingService; - private final Cache deWeightCache = CacheUtil.newLRUCache(1024); + private final Cache deWeightCache = Caffeine.newBuilder() + .maximumSize(1024) + .build(); /** * Poll configuration. @@ -69,8 +71,7 @@ public class ConfigServletInner { */ private boolean weightVerification(HttpServletRequest request) { String clientIdentify = request.getParameter(WEIGHT_CONFIGS); -// Long timeVal = deWeightCache.getIfPresent(clientIdentify); - Long timeVal = deWeightCache.containsKey(clientIdentify) ? deWeightCache.get(clientIdentify) : null; + Long timeVal = deWeightCache.getIfPresent(clientIdentify); if (timeVal == null) { deWeightCache.put(clientIdentify, System.currentTimeMillis()); return true; diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java index 306558b6..c4e18963 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java @@ -19,7 +19,7 @@ package cn.hippo4j.core.toolkit; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.toolkit.CollectionUtil; -import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.common.toolkit.Joiner; import cn.hippo4j.common.toolkit.ThreadUtil; import cn.hippo4j.core.toolkit.inet.InetUtils; import cn.hutool.core.util.IdUtil; @@ -90,6 +90,6 @@ public class IdentifyUtil { */ public static String getThreadPoolIdentify(String threadPoolId, String itemId, String namespace) { ArrayList params = CollectionUtil.newArrayList(threadPoolId, itemId, namespace, getIdentify()); - return StringUtil.Joiner.on(GROUP_KEY_DELIMITER).join(params); + return Joiner.on(GROUP_KEY_DELIMITER).join(params); } } diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java index d9157618..c2d2acd2 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java @@ -17,6 +17,7 @@ package cn.hippo4j.message.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; @@ -109,7 +110,7 @@ public abstract class AbstractRobotSendMessageHandler implements SendMessageHand // 拒绝策略次数 alarmNotifyRequest.getRejectCountNum(), // 告警手机号 - StringUtil.Joiner.on(robotMessageActualContent.getReceiveSeparator()).join(notifyConfig.getReceives().split(",")), + Joiner.on(robotMessageActualContent.getReceiveSeparator()).join(notifyConfig.getReceives().split(",")), // 报警频率 notifyConfig.getInterval(), // 当前时间 @@ -155,7 +156,7 @@ public abstract class AbstractRobotSendMessageHandler implements SendMessageHand changeParameterNotifyRequest.getBeforeRejectedName(), changeParameterNotifyRequest.getNowRejectedName(), // 告警手机号 - StringUtil.Joiner.on(robotMessageActualContent.getReceiveSeparator()).join(notifyConfig.getReceives().split(",")), + Joiner.on(robotMessageActualContent.getReceiveSeparator()).join(notifyConfig.getReceives().split(",")), // 当前时间 DateUtil.now()); execute(RobotMessageExecuteDTO.builder().text(text).notifyConfig(notifyConfig).build()); diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/service/AlarmControlHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/service/AlarmControlHandler.java index 85815db0..c0f0c500 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/service/AlarmControlHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/service/AlarmControlHandler.java @@ -19,14 +19,15 @@ package cn.hippo4j.message.service; import cn.hippo4j.common.constant.Constants; import cn.hippo4j.message.dto.AlarmControlDTO; -import cn.hutool.cache.Cache; -import cn.hutool.cache.CacheUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; /** @@ -50,12 +51,12 @@ public class AlarmControlHandler { if (cache == null) { return false; } - String pkId = cache.containsKey(alarmControl.getTypeEnum().name()) ? cache.get(alarmControl.getTypeEnum().name()) : null; + String pkId = cache.getIfPresent(alarmControl.getTypeEnum().name()); if (StrUtil.isBlank(pkId)) { ReentrantLock lock = threadPoolLock.get(threadPoolKey); lock.lock(); try { - pkId = cache.containsKey(alarmControl.getTypeEnum().name()) ? cache.get(alarmControl.getTypeEnum().name()) : null; + pkId = cache.getIfPresent(alarmControl.getTypeEnum().name()); if (StrUtil.isBlank(pkId)) { // Val meaningless. cache.put(alarmControl.getTypeEnum().name(), IdUtil.simpleUUID()); @@ -77,8 +78,9 @@ public class AlarmControlHandler { */ public void initCacheAndLock(String threadPoolId, String platform, Integer interval) { String threadPoolKey = StrUtil.builder(threadPoolId, Constants.GROUP_KEY_DELIMITER, platform).toString(); - long milliseconds = interval * 60 * 1000; - Cache cache = CacheUtil.newTimedCache(milliseconds); + Cache cache = Caffeine.newBuilder() + .expireAfterWrite(interval, TimeUnit.MINUTES) + .build(); threadPoolAlarmCache.put(threadPoolKey, cache); // Set the lock to prevent false sending of alarm information. ReentrantLock reentrantLock = new ReentrantLock(); diff --git a/pom.xml b/pom.xml index 0ab7d6c6..63e9cf33 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,7 @@ 1.4.2-SNAPSHOT 6.5.0 + 2.9.3 30.0-jre 3.12.0 5.4.7 @@ -124,6 +125,12 @@ dozer-core ${dozer.version} + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} + + com.google.guava guava