update "Globally remove the Guava toolkit dependency"

pull/759/head
Lijx 3 years ago
parent 1e775632ad
commit cd9259014a

@ -71,6 +71,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
</dependencies>
<build>

@ -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 extends Appendable> 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();
}
}

@ -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 extends Appendable> 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();
}
}
}

@ -15,6 +15,12 @@
</properties>
<dependencies>
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-common</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>

@ -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<String> getIdentifyList(String tenantId, String itemId, String threadPoolId) {
List<String> 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<String> keys = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, buildKey);
if (CollectionUtil.isNotEmpty(keys)) {
identifyList = new ArrayList<>(keys.size());

@ -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<String, Long> deWeightCache = CacheUtil.newLRUCache(1024);
private final Cache<String, Long> 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;

@ -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<String> params = CollectionUtil.newArrayList(threadPoolId, itemId, namespace, getIdentify());
return StringUtil.Joiner.on(GROUP_KEY_DELIMITER).join(params);
return Joiner.on(GROUP_KEY_DELIMITER).join(params);
}
}

@ -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());

@ -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<String, String> cache = CacheUtil.newTimedCache(milliseconds);
Cache<String, String> 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();

@ -29,6 +29,7 @@
<revision>1.4.2-SNAPSHOT</revision>
<!-- Tool -->
<dozer.version>6.5.0</dozer.version>
<caffeine.version>2.9.3</caffeine.version>
<guava.version>30.0-jre</guava.version>
<okhttp3.version>3.12.0</okhttp3.version>
<hutool-all.version>5.4.7</hutool-all.version>
@ -124,6 +125,12 @@
<artifactId>dozer-core</artifactId>
<version>${dozer.version}</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>${caffeine.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>

Loading…
Cancel
Save