fix: SpotBugs扫描分析(六):优化Map迭代方式提高性能、显示指定字符编码UTF-8(减少平台差异)

pull/66/head
xiaoxiamo 6 months ago
parent 4be4e3c9f2
commit 5c2f39cd49

@ -35,7 +35,7 @@ public class CommonConstant {
/**
*
*/
public static final String CHARSET_NAME = "UTF-8";
public static final String CHARSET_UTF_8 = "UTF-8";
/**
* HTTP
*/

@ -4,12 +4,16 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.csv.*;
import cn.hutool.core.util.CharsetUtil;
import com.google.common.base.Throwables;
import com.java3y.austin.cron.csv.CountFileRowHandler;
import com.java3y.austin.cron.vo.CrowdInfoVo;
import lombok.extern.slf4j.Slf4j;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/**
@ -37,7 +41,8 @@ public class ReadFileUtils {
public static void getCsvRow(String path, CsvRowHandler csvRowHandler) {
// 把首行当做是标题获取reader
try (CsvReader reader = CsvUtil.getReader(new FileReader(path),
try (CsvReader reader = CsvUtil.getReader(
new InputStreamReader(Files.newInputStream(Paths.get(path)), CharsetUtil.CHARSET_UTF_8),
new CsvReadConfig().setContainsHeader(true))) {
reader.read(csvRowHandler);
} catch (Exception e) {
@ -54,7 +59,8 @@ public class ReadFileUtils {
public static long countCsvRow(String path, CountFileRowHandler countFileRowHandler) {
// 把首行当做是标题获取reader
try (CsvReader reader = CsvUtil.getReader(new FileReader(path),
try (CsvReader reader = CsvUtil.getReader(
new InputStreamReader(new FileInputStream(path), CharsetUtil.CHARSET_UTF_8),
new CsvReadConfig().setContainsHeader(true))) {
reader.read(countFileRowHandler);

@ -128,7 +128,7 @@ public class DingDingRobotHandler extends BaseHandler{
Mac mac = Mac.getInstance(CommonConstant.HMAC_SHA256_ENCRYPTION_ALGO);
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), CommonConstant.HMAC_SHA256_ENCRYPTION_ALGO));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), CommonConstant.CHARSET_NAME);
sign = URLEncoder.encode(new String(Base64.encodeBase64(signData), CommonConstant.CHARSET_UTF_8));
} catch (Exception e) {
log.error("DingDingHandler#assembleSign fail!:{}", Throwables.getStackTraceAsString(e));
}

@ -84,14 +84,15 @@ public class ReceiverStart {
@Bean
public ConcurrentKafkaListenerContainerFactory filterContainerFactory(@Value("${austin.business.tagId.key}") String tagIdKey,
@Value("${austin.business.tagId.value}") String tagIdValue) {
ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory();
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory);
factory.setAckDiscarded(true);
factory.setRecordFilterStrategy(consumerRecord -> {
if (Optional.ofNullable(consumerRecord.value()).isPresent()) {
for (Header header : consumerRecord.headers()) {
if (header.key().equals(tagIdKey) && new String(header.value()).equals(new String(tagIdValue.getBytes(StandardCharsets.UTF_8)))) {
if (header.key().equals(tagIdKey) &&
new String(header.value(), StandardCharsets.UTF_8).equals(tagIdValue)) {
return false;
}
}

@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.List;
@ -42,7 +43,7 @@ public class RabbitMqReceiver implements MessageReceiver {
public void onMessage(Message message) {
String messageType = message.getMessageProperties().getHeader("messageType");
byte[] body = message.getBody();
String messageContent = new String(body);
String messageContent = new String(body, StandardCharsets.UTF_8);
if (StringUtils.isBlank(messageContent)) {
return;
}

@ -13,6 +13,7 @@ import io.lettuce.core.RedisFuture;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
@ -50,8 +51,8 @@ public class AustinSink implements SinkFunction<AnchorInfo> {
*/
String redisMessageKey = CharSequenceUtil.join(StrPool.COLON, AustinConstant.CACHE_KEY_PREFIX, AustinConstant.MESSAGE_ID, info.getMessageId());
SimpleAnchorInfo messageAnchorInfo = SimpleAnchorInfo.builder().businessId(info.getBusinessId()).state(info.getState()).timestamp(info.getLogTimestamp()).build();
redisFutures.add(redisAsyncCommands.lpush(redisMessageKey.getBytes(), JSON.toJSONString(messageAnchorInfo).getBytes()));
redisFutures.add(redisAsyncCommands.expire(redisMessageKey.getBytes(), Duration.ofDays(3).toMillis() / 1000));
redisFutures.add(redisAsyncCommands.lpush(redisMessageKey.getBytes(StandardCharsets.UTF_8), JSON.toJSONString(messageAnchorInfo).getBytes(StandardCharsets.UTF_8)));
redisFutures.add(redisAsyncCommands.expire(redisMessageKey.getBytes(StandardCharsets.UTF_8), Duration.ofDays(3).toMillis() / 1000));
/**
* 1.userId list:{key,list}
@ -59,17 +60,17 @@ public class AustinSink implements SinkFunction<AnchorInfo> {
*/
SimpleAnchorInfo userAnchorInfo = SimpleAnchorInfo.builder().businessId(info.getBusinessId()).state(info.getState()).timestamp(info.getLogTimestamp()).build();
for (String id : info.getIds()) {
redisFutures.add(redisAsyncCommands.lpush(id.getBytes(), JSON.toJSONString(userAnchorInfo).getBytes()));
redisFutures.add(redisAsyncCommands.expire(id.getBytes(), (DateUtil.endOfDay(new Date()).getTime() - DateUtil.current()) / 1000));
redisFutures.add(redisAsyncCommands.lpush(id.getBytes(StandardCharsets.UTF_8), JSON.toJSONString(userAnchorInfo).getBytes(StandardCharsets.UTF_8)));
redisFutures.add(redisAsyncCommands.expire(id.getBytes(StandardCharsets.UTF_8), (DateUtil.endOfDay(new Date()).getTime() - DateUtil.current()) / 1000));
}
/**
* 2. hash:{key,hash}
* key:businessId,hashValue:{state,stateCount}
*/
redisFutures.add(redisAsyncCommands.hincrby(String.valueOf(info.getBusinessId()).getBytes(),
String.valueOf(info.getState()).getBytes(), info.getIds().size()));
redisFutures.add(redisAsyncCommands.expire(String.valueOf(info.getBusinessId()).getBytes(),
redisFutures.add(redisAsyncCommands.hincrby(String.valueOf(info.getBusinessId()).getBytes(StandardCharsets.UTF_8),
String.valueOf(info.getState()).getBytes(StandardCharsets.UTF_8), info.getIds().size()));
redisFutures.add(redisAsyncCommands.expire(String.valueOf(info.getBusinessId()).getBytes(StandardCharsets.UTF_8),
((DateUtil.offsetDay(new Date(), 30).getTime()) / 1000) - DateUtil.currentSeconds()));
return redisFutures;

@ -70,12 +70,12 @@ public class OkHttpUtils {
StringBuilder sb = new StringBuilder(url);
if (Objects.nonNull(params) && params.keySet().size() > 0) {
boolean firstFlag = true;
for (String key : params.keySet()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
if (firstFlag) {
sb.append("?").append(key).append("=").append(params.get(key));
sb.append("?").append(entry.getKey()).append("=").append(entry.getValue());
firstFlag = false;
} else {
sb.append("&").append(key).append("=").append(params.get(key));
sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
}
}
@ -98,8 +98,8 @@ public class OkHttpUtils {
FormBody.Builder formBuilder = new FormBody.Builder();
if (Objects.nonNull(params) && params.keySet().size() > 0) {
for (String key : params.keySet()) {
formBuilder.add(key, params.get(key));
for (Map.Entry<String, String> entry : params.entrySet()) {
formBuilder.add(entry.getKey(), entry.getValue());
}
}
Request.Builder builder = getBuilderWithHeaders(headers);

@ -10,6 +10,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
@ -82,8 +83,8 @@ public class RedisUtils {
try {
redisTemplate.executePipelined((RedisCallback<String>) connection -> {
for (Map.Entry<String, String> entry : keyValues.entrySet()) {
connection.setEx(entry.getKey().getBytes(), seconds,
entry.getValue().getBytes());
connection.setEx(entry.getKey().getBytes(StandardCharsets.UTF_8), seconds,
entry.getValue().getBytes(StandardCharsets.UTF_8));
}
return null;
});
@ -99,8 +100,8 @@ public class RedisUtils {
public void lPush(String key, String value, Long seconds) {
try {
redisTemplate.executePipelined((RedisCallback<String>) connection -> {
connection.lPush(key.getBytes(), value.getBytes());
connection.expire(key.getBytes(), seconds);
connection.lPush(key.getBytes(StandardCharsets.UTF_8), value.getBytes(StandardCharsets.UTF_8));
connection.expire(key.getBytes(StandardCharsets.UTF_8), seconds);
return null;
});
} catch (Exception e) {
@ -142,8 +143,11 @@ public class RedisUtils {
try {
redisTemplate.executePipelined((RedisCallback<String>) connection -> {
for (Map.Entry<String, String> entry : keyValues.entrySet()) {
connection.hIncrBy(entry.getKey().getBytes(), entry.getValue().getBytes(), delta);
connection.expire(entry.getKey().getBytes(), seconds);
connection.hIncrBy(entry.getKey().getBytes(StandardCharsets.UTF_8),
entry.getValue().getBytes(StandardCharsets.UTF_8),
delta);
connection.expire(entry.getKey().getBytes(StandardCharsets.UTF_8),
seconds);
}
return null;
});

Loading…
Cancel
Save