Java 生成隨機(jī)單據(jù)號(hào)的實(shí)現(xiàn)示例
背景:全局生成4位字符2222-9ZZ9
實(shí)現(xiàn)方式:
使用redis的原子自增 + google的retry保證,生成4位數(shù)
1、pom
<dependency>
<groupId>com.github.rholder</groupId>
<artifactId>guava-retrying</artifactId>
<version>2.0.0</version>
</dependency>2、獲取單號(hào)序列重試器
private final Retryer<Long> getOrderSequenceRetryer = RetryerBuilder.<Long>newBuilder()
.retryIfResult(Objects::isNull).retryIfException()
.withWaitStrategy(WaitStrategies.incrementingWait(30, TimeUnit.MILLISECONDS, 10, TimeUnit.MILLISECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt(2)).build();3、redis原子自增 + guava的重試機(jī)制保證
private String getFallbackSuffix() {
Long num;
try {
num = getOrderSequenceRetryer.call(
() -> redisGateway.incr()//使用redis的原子自增+1,并設(shè)置過(guò)期時(shí)間
);
} catch (Exception e) {
throw new RuntimeException("redis incr exception", e);
}
return getSuffix(num.intValue());
}4、將生成的原子自增數(shù),取后綴,最終4位范圍【2222-9ZZ9】和業(yè)務(wù)相關(guān)
/**
* 獲取后綴
* [(2-9), (2-9,A-H,J-N,P-Z), 2-9,A-H,J-N,P-Z), (2-9)]
* 2222-9ZZ9
*
* @param num num
* @return suffix
*/
public static String getSuffix(int num) {
String[] suffixFactors = new String[]{"2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
"G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
// 65536 = 8 * 32 * 32 * 8
int serialNumberThreshold = 65536;
if (num >= 0 && num < serialNumberThreshold) {
int index4 = num / 8192;
int mod4 = num % 8192;
int index3 = mod4 / 256;
int mod3 = mod4 % 256;
int index2 = mod3 / 8;
int mod2 = mod3 % 8;
return suffixFactors[index4] + suffixFactors[index3] + suffixFactors[index2] + suffixFactors[mod2];
}
throw new IllegalArgumentException("num 超過(guò)流水號(hào)閾值,單號(hào)可能會(huì)重復(fù)!!!");
}注意事項(xiàng)
1、單據(jù)號(hào)中,最好不要說(shuō)使用1和l,0和o(數(shù)字只能用8個(gè),字母只能用大寫(xiě)的24個(gè))
2、guava的重試機(jī)制參考:http://www.dbjr.com.cn/program/299337r11.htm
UUID
public class IdUtil {
/*
* 返回使用ThreadLocalRandom的UUID,比默認(rèn)的UUID性能更優(yōu)
*/
public static UUID fastUUID() {
ThreadLocalRandom random = ThreadLocalRandom.current();
return new UUID(random.nextLong(), random.nextLong());
}
}隨機(jī)生成前綴 + 日期 + 后綴隨機(jī)5位數(shù)
private static final DateTimeFormatter FORMATTER_DATE_YYYYMMDDHHMMSS = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
@Test
public void t() throws Exception {
String suffix = StringUtils.leftPad(String.valueOf(new Random().nextInt(100)), 5, "0");
String result = "prefix" + formatLocalDateTime(LocalDateTime.now()) + suffix;
}
public static String formatLocalDateTime(LocalDateTime localDateTime) {
return FORMATTER_DATE_YYYYMMDDHHMMSS.format(localDateTime);
}到此這篇關(guān)于Java 生成隨機(jī)單據(jù)號(hào)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java 生成隨機(jī)單據(jù)號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java消息隊(duì)列的簡(jiǎn)單實(shí)現(xiàn)代碼
本篇文章主要介紹了Java消息隊(duì)列的簡(jiǎn)單實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
java讀取cvs文件并導(dǎo)入數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了java讀取cvs文件并導(dǎo)入數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
SpringBoot Data JPA 關(guān)聯(lián)表查詢的方法
這篇文章主要介紹了SpringBoot Data JPA 關(guān)聯(lián)表查詢的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Java MultipartFile實(shí)現(xiàn)上傳文件/上傳圖片
這篇文章主要介紹了Java MultipartFile實(shí)現(xiàn)上傳文件/上傳圖片,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12

