fix : add IdUtil for generate UUID(#725)

pull/762/head
pizihao 3 years ago
parent d902e2a942
commit 700720bf3a

@ -92,7 +92,7 @@ public class Assert {
}
public static void notBlank(String str, String message) {
if (StringUtil.isNotBlank(str)) {
if (StringUtil.isBlank(str)) {
throw new IllegalArgumentException(message);
}
}

@ -173,5 +173,4 @@ public class BeanUtil {
throw new IllegalException("not find setter for" + propertiesName + "in" + o.getName(), e);
}
}
}

@ -0,0 +1,85 @@
/*
* 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.util.UUID;
/**
* id and uuid util{@link UUID}
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class IdUtil {
/**
* get a random UUID
*
* @return UUID
*/
public static String randomUUID() {
return toString(UUID.randomUUID(), false);
}
/**
* get a simple random UUID
*
* @return a simple UUID
*/
public static String simpleUUID() {
return toString(UUID.randomUUID(), true);
}
/**
* toString
*
* @param uuid UUID
* @return UUID String
*/
public static String toString(UUID uuid, boolean isSimple) {
long mostSigBits = uuid.getMostSignificantBits();
long leastSigBits = uuid.getLeastSignificantBits();
if (isSimple) {
return (digits(mostSigBits >> 32, 8) +
digits(mostSigBits >> 16, 4) +
digits(mostSigBits, 4) +
digits(leastSigBits >> 48, 4) +
digits(leastSigBits, 12));
} else {
return (digits(mostSigBits >> 32, 8) + "-" +
digits(mostSigBits >> 16, 4) + "-" +
digits(mostSigBits, 4) + "-" +
digits(leastSigBits >> 48, 4) + "-" +
digits(leastSigBits, 12));
}
}
/**
* Returns val represented by the specified number of hex digits. <br>
* {@link UUID#digits(long, int)}
*
* @param val value
* @param digits position
* @return hex value
*/
private static String digits(long val, int digits) {
long hi = 1L << (digits * 4);
return Long.toHexString(hi | (val & (hi - 1))).substring(1);
}
}

@ -27,7 +27,6 @@ import java.util.*;
public class BeanUtilTest {
@Test
public void beanToMapConvertTest() {
// 测试BeanToMap

@ -0,0 +1,37 @@
/*
* 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 IdUtilTest {
@Test
public void randomUUIDTest() {
String randomUUID = IdUtil.randomUUID();
Assert.assertNotNull(randomUUID);
}
@Test
public void simpleUUIDTest() {
String simpleUUID = IdUtil.simpleUUID();
Assert.assertNotNull(simpleUUID);
Assert.assertFalse(simpleUUID.contains("-"));
}
}

@ -123,7 +123,7 @@ public class ReflectUtilTest {
@Test
public void getMethodByNameTest() {
Method field = ReflectUtil.getMethodByName(TestClass.class, "getPrivateField", String.class);
Method field = ReflectUtil.getMethodByName(TestClass.class, "setPrivateField", String.class);
Assert.assertNotNull(field);
}

@ -18,10 +18,7 @@
package cn.hippo4j.core.toolkit;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.common.toolkit.CollectionUtil;
import cn.hippo4j.common.toolkit.Joiner;
import cn.hippo4j.common.toolkit.ThreadUtil;
import cn.hippo4j.common.toolkit.*;
import cn.hippo4j.core.toolkit.inet.InetUtils;
import org.springframework.core.env.ConfigurableEnvironment;
@ -38,7 +35,7 @@ public class IdentifyUtil {
private static String IDENTIFY;
public static final String CLIENT_IDENTIFICATION_VALUE = UUID.randomUUID().toString();
public static final String CLIENT_IDENTIFICATION_VALUE = IdUtil.simpleUUID();
/**
* Generate identify.

@ -17,6 +17,7 @@
package cn.hippo4j.springboot.starter.adapter.kafka.example.produce;
import cn.hippo4j.common.toolkit.IdUtil;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.example.core.dto.SendMessageDTO;
import lombok.AllArgsConstructor;
@ -26,8 +27,6 @@ import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* Kafka message produce.
*/
@ -44,7 +43,7 @@ public class KafkaMessageProduce {
@GetMapping("/message/send")
public String sendMessage(Integer count) {
for (int i = 0; i < count; i++) {
String keys = UUID.randomUUID().toString();
String keys = IdUtil.randomUUID();
SendMessageDTO payload = SendMessageDTO.builder()
.receiver("156011xxx91")
.uid(keys)

@ -17,6 +17,7 @@
package cn.hippo4j.springboot.starter.adapter.rabbitmq.example.producer;
import cn.hippo4j.common.toolkit.IdUtil;
import cn.hippo4j.example.core.dto.SendMessageDTO;
import cn.hippo4j.springboot.starter.adapter.rabbitmq.example.constants.SimpleMQConstant;
import lombok.AllArgsConstructor;
@ -26,8 +27,6 @@ import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* Message produce.
*/
@ -42,7 +41,7 @@ public class MessageProduce {
@GetMapping("/message/send")
public String sendMessage(Integer count) {
for (int i = 0; i < count; i++) {
String keys = UUID.randomUUID().toString();
String keys = IdUtil.randomUUID();
SendMessageDTO payload = SendMessageDTO.builder()
.receiver("156011xxx91")
.uid(keys)

@ -17,6 +17,7 @@
package cn.hippo4j.springboot.starter.adapter.springcloud.stream.rabbitmq.example;
import cn.hippo4j.common.toolkit.IdUtil;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.example.core.dto.SendMessageDTO;
import lombok.AllArgsConstructor;
@ -26,8 +27,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* Message produce.
*/
@ -50,7 +49,7 @@ public class MessageProduce {
}
private void sendMessage0() {
String keys = UUID.randomUUID().toString();
String keys = IdUtil.randomUUID();
SendMessageDTO payload = SendMessageDTO.builder()
.receiver("156011xxx91")
.uid(keys)

@ -17,6 +17,7 @@
package cn.hippo4j.springboot.starter.adapter.springcloud.stream.rocketmq.example;
import cn.hippo4j.common.toolkit.IdUtil;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.example.core.dto.SendMessageDTO;
import lombok.AllArgsConstructor;
@ -28,8 +29,6 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* Message produce.
*/
@ -55,7 +54,7 @@ public class MessageProduce {
}
private void sendMessage(String tags) {
String keys = UUID.randomUUID().toString();
String keys = IdUtil.randomUUID();
SendMessageDTO payload = SendMessageDTO.builder()
.receiver("156011xxx91")
.uid(keys)

@ -18,6 +18,7 @@
package cn.hippo4j.message.service;
import cn.hippo4j.common.constant.Constants;
import cn.hippo4j.common.toolkit.IdUtil;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.message.dto.AlarmControlDTO;
import com.github.benmanes.caffeine.cache.Cache;
@ -25,7 +26,6 @@ import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@ -59,7 +59,7 @@ public class AlarmControlHandler {
pkId = cache.getIfPresent(alarmControl.getTypeEnum().name());
if (StringUtil.isBlank(pkId)) {
// Val meaningless.
cache.put(alarmControl.getTypeEnum().name(), UUID.randomUUID().toString());
cache.put(alarmControl.getTypeEnum().name(), IdUtil.simpleUUID());
return true;
}
} finally {

@ -20,6 +20,7 @@ package cn.hippo4j.springboot.starter.core;
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
import cn.hippo4j.common.toolkit.ContentUtil;
import cn.hippo4j.common.toolkit.GroupKey;
import cn.hippo4j.common.toolkit.IdUtil;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.common.design.builder.ThreadFactoryBuilder;
@ -152,7 +153,7 @@ public class ClientWorker {
public List<String> checkUpdateTpIds(String probeUpdateString, boolean isInitializingCacheList) {
Map<String, String> params = new HashMap(2);
params.put(PROBE_MODIFY_REQUEST, probeUpdateString);
params.put(WEIGHT_CONFIGS, UUID.randomUUID().toString());
params.put(WEIGHT_CONFIGS, IdUtil.simpleUUID());
Map<String, String> headers = new HashMap(2);
headers.put(LONG_PULLING_TIMEOUT, "" + timeout);
// Confirm the identity of the client, and can be modified separately when modifying the thread pool configuration.

@ -89,7 +89,6 @@
<artifactId>okhttp</artifactId>
<version>${okhttp3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>

Loading…
Cancel
Save