栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

生成序列号(幂等)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成序列号(幂等)

1、source+唯一序列号构成唯一字段入库

2、借助redis的自增指令实现唯一(incr指令 )

public class IdempotentTest {

    
    public static void main(String[] args) {
    	ArrayList arrayList = new ArrayList<>();
    	for(int i=0;i<10000;i++) {
    		String uniqueIdentifier = getUniqueIdentifier();
    		System.out.println(uniqueIdentifier);
    		arrayList.add(uniqueIdentifier);
    	}
    	
    	int size = arrayList.stream().distinct().collect(Collectors.toList()).size();
    	System.out.println(size+"==?"+arrayList.size());
    }
    
    public static String getUniqueIdentifier() {
    	UUID uuid = UUID.randomUUID();
    	// 4位数字
    	String uuidString=uuid.toString().replace("-", "").toUpperCase();
    	// 当前日期 yyyyMMdd 
    	String currentDate = formatDate(new Date());
    	
    	String prefix="AOP";
		return prefix+currentDate+uuidString;
    	
    }
    
    
    public static String formatDate(Date date){
        String formatDate=null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            formatDate = simpleDateFormat.format(date);
        } catch (Exception e) {
            //logger.error("时间格式化异常",e);
        }
        return formatDate;
    }
    
    
    public static Date stringToDate(String s){
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd");
        Date date=null;
        try {
            date = dateFormat.parse(s);
        } catch (ParseException e) {
            //logger.error("String转为date异常",e);
        }
        return date;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号