直接上代码
public static final Long START_NUM = 10001L;//生成订单号初始值
public static final long lockSeconds=60*20L;//释放锁超时时间
/*
* prefixNo 流水号前缀
* dateString yyMMdd 自定义年月日, 或者年月日时分秒 yyMMdd HHssmm
*/
public String getSeriaCode(String prefixNo, String dateString) {
Long tmpSerialCode = START_NUM;
boolean flag=true;
prefixNo = "SMTS";//流水号前缀
do {
String lock = null;
String key = null;
try {
key = String.format("%s%s%s",prefixNo, dateString,"A");
lock = redisUtils.getLock(key, lockSeconds);
if(lock!=null){
long incr = redisUtils.incr(prefixNo + dateString, 1);
tmpSerialCode = tmpSerialCode + incr ;
flag = false;
}
} finally {
if (key != null){
redisUtils.del(key);
}
}
} while (flag);
return prefixNo+dateString+tmpSerialCode;
}