web:重构sequenceUtil到common中
parent
a12748a27c
commit
1fd02b71b3
@ -0,0 +1,48 @@
|
||||
package com.supervision.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
|
||||
@Slf4j
|
||||
public class RedisSequenceUtil {
|
||||
|
||||
private volatile static RedisTemplate<String, String> redisTemplate = null;
|
||||
|
||||
|
||||
public static String getProcessNo(){
|
||||
// 从零开始计算
|
||||
long processNoSeq = getIncrement("process_no_seq") -1;
|
||||
|
||||
char prefix = (char) ('a' + processNoSeq / 1000000);
|
||||
Long suffix = processNoSeq % 1000000;
|
||||
return prefix + String.format("%06d", suffix);
|
||||
|
||||
}
|
||||
|
||||
public static Long getIncrement(String key){
|
||||
ValueOperations<String, String> operations = getRedisTemplate().opsForValue();
|
||||
return operations.increment( key,1L);
|
||||
}
|
||||
|
||||
public static RedisTemplate<String, String> getRedisTemplate() {
|
||||
if (redisTemplate == null) {
|
||||
synchronized (RedisSequenceUtil.class){
|
||||
if (null == redisTemplate){
|
||||
redisTemplate = createRedisTemplate();
|
||||
}
|
||||
}
|
||||
}
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
private static RedisTemplate<String, String> createRedisTemplate() {
|
||||
ApplicationContext applicationContext = SpringBeanUtil.getApplicationContext();
|
||||
if (null == applicationContext){
|
||||
return null;
|
||||
}
|
||||
return applicationContext.getBean(RedisTemplate.class);
|
||||
}
|
||||
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.supervision.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
|
||||
@Slf4j
|
||||
public class SequenceUtil {
|
||||
|
||||
private static final RedisTemplate<String, String> redisTemplate = SpringBeanUtil.getBean("redisTemplate", RedisTemplate.class);
|
||||
|
||||
|
||||
public static String getProcessNo(){
|
||||
|
||||
// 从零开始计算
|
||||
long processNoSeq = getIncrement("process_no_seq") -1;
|
||||
|
||||
char prefix = (char) ('a' + processNoSeq / 1000000);
|
||||
Long suffix = processNoSeq % 1000000;
|
||||
return prefix + String.format("%06d", suffix);
|
||||
|
||||
}
|
||||
|
||||
public static Long getIncrement(String key){
|
||||
ValueOperations<String, String> operations = redisTemplate.opsForValue();
|
||||
return operations.increment( key,1L);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue