Commit b88b4c22 by 朱瑞泽

代码调整

parent f83c8495
......@@ -145,20 +145,16 @@ public class Constant {
/**
* 状态-无效
*/
public static final int STATE_INVALID = 0;
/**
* 状态-有效
*/
public static final int STATE_EFFECTIVE = 1;
public static final int STATUS_INVALID = 0;
/**
* 默认分页显示条数 20
* 状态-有效
*/
public final static int DEFAULT_PAGESIZE = 20;
public static final int STATUS_EFFECTIVE = 1;
/**
* 默认分页最多显示条数 200
* 成功代码
*/
public final static int DEFAULT_MAX_PAGESIZE = 200;
public static final String SUCCESS = "0000";
}
......@@ -12,14 +12,9 @@ public class ServiceResponse<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 是否走完正常业务逻辑流程
* 响应代码(成功代码固定为 {@link Constant#SUCCESS 0000})
*/
private boolean success;
/**
* 响应代码
*/
private int code;
private String code;
/**
* 响应信息(可为null,不一定和code说明信息一致)
......@@ -31,33 +26,47 @@ public class ServiceResponse<T> implements Serializable {
*/
private T result;
public ServiceResponse() {
public static <T> ServiceResponse<T> success() {
return new ServiceResponse<>(Constant.SUCCESS, null, null);
}
public ServiceResponse(boolean success, int code, T result) {
this(success, code, null, result);
public static <T> ServiceResponse<T> success(T result) {
return new ServiceResponse<>(Constant.SUCCESS, null, result);
}
public ServiceResponse(boolean success, int code, String message, T result) {
this.success = success;
public static <T> ServiceResponse<T> success(T result, String message) {
return new ServiceResponse<>(Constant.SUCCESS, message, result);
}
public static <T> ServiceResponse<T> failure(String code, String message) {
return new ServiceResponse<>(code, message, null);
}
public static <T> ServiceResponse<T> failure(String code, String message, T result) {
return new ServiceResponse<>(code, message, result);
}
public ServiceResponse() {
}
private ServiceResponse(String code, String message, T result) {
this.code = code;
this.message = message;
this.result = result;
}
/**
* @return 是否成功
*/
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
return Constant.SUCCESS.equals(code);
}
public int getCode() {
public String getCode() {
return code;
}
public void setCode(int code) {
public void setCode(String code) {
this.code = code;
}
......
package com.gic.demo.common.init;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Created 2019/1/9.
*
* @author hua
*/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
}
/**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clearHolder() {
logger.debug("清除SpringContextHolder中的ApplicationContext:"
+ applicationContext);
applicationContext = null;
}
/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
if (SpringContextHolder.applicationContext != null) {
logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
}
SpringContextHolder.applicationContext = applicationContext; // NOSONAR
}
/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
}
}
\ No newline at end of file
package com.gic.demo.common.interceptor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 跨域拦截器
*
* @author zhurz
*/
public class CORSInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(RequestMethod.OPTIONS.name().equals(request.getMethod())) {
response.setStatus(HttpStatus.OK.value());
return false;
}
return true;
}
}
package com.gic.demo.common.utils;
import com.alibaba.fastjson.JSON;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import java.util.List;
public class EntityUtils {
public static <T> T changeEntityByJSON(Class<T> target, Object baseTO) {
return JSON.parseObject(JSON.toJSONString(baseTO), target);
}
public static <T> List<T> changeEntityListByJSON(Class<T> target, Object baseTO) {
return JSON.parseArray(JSON.toJSONString(baseTO), target);
}
private static final MapperFacade MAPPER;
static {
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
MAPPER = mapperFactory.getMapperFacade();
}
public static <T> T changeEntityByOrika(Class<T> target, Object baseTO) {
return MAPPER.map(baseTO, target);
}
public static <D, S> List<D> changeEntityListByOrika(Class<D> target, Iterable<S> baseTO) {
return MAPPER.mapAsList(baseTO, target);
}
}
package com.gic.demo.common.utils;
import java.io.Serializable;
/**
* rest接口响应
*
* @author zhurz
*/
public class GicDemoResponse implements Serializable {
private static final long serialVersionUID = 1L;
private int errorCode;
private String message;
private Object result;
private String detailError;
public GicDemoResponse() {
}
public GicDemoResponse(int errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}
public String getDetailError() {
return detailError;
}
public void setDetailError(String detailError) {
this.detailError = detailError;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
package com.gic.demo.common.utils;
import java.util.*;
/**
* MAP工具类
*
* @author zhurz
*/
public class MapUtil {
public interface Handler<T, R> {
R apply(T input);
}
/**
* 转换MAP
*
* @param list
* @param handler
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map<K, V> convertMap(List<V> list, Handler<V, K> handler) {
Map<K, V> map = new HashMap<>();
if (list == null || handler == null) {
return map;
}
for (V v : list) {
map.put(handler.apply(v), v);
}
return map;
}
/**
* 转换SET
*
* @param list
* @param handler
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Set<K> convertSet(List<V> list, Handler<V, K> handler) {
Set<K> set = new HashSet<>();
if (list == null || handler == null) {
return set;
}
for (V v : list) {
set.add(handler.apply(v));
}
return set;
}
}
package com.gic.demo.common.utils;
import com.gic.redis.RedisFactory;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
public class UniqueIdUtils {
private static class SnowFlake {
private static final SnowFlake SNOW_FLAKE = new SnowFlake();
private static final long START_TIMESTAMP = 1557489395327L; //起始的时间戳
private static final long SEQUENCE_BIT = 12; //序列号占用位数
private static final long MACHINE_BIT = 10; //机器标识占用位数
private static final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT; //时间戳位移位数
private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BIT); //最大序列号
private static final long MAX_MACHINE_ID = ~(-1L << MACHINE_BIT); //最大机器编号
private long machineIdPart; //生成id机器标识部分
private long sequence = 0L; //序列号
private long lastStamp = -1L; //上一次时间戳
private SnowFlake() {
RedissonClient client = RedisFactory.getDefault();
RAtomicLong atomicLong = client.getAtomicLong("SnowFlake:MachineId");
machineIdPart = (atomicLong.addAndGet(1L) & MAX_MACHINE_ID) << SEQUENCE_BIT;
}
public synchronized long nextId() {
long currentStamp = System.currentTimeMillis();
while (currentStamp < lastStamp) { //避免机器时钟回拨
currentStamp = System.currentTimeMillis();
}
if (currentStamp == lastStamp) {
sequence++; //相同毫秒内,序列号自增
if (sequence > MAX_SEQUENCE) { //同一毫秒的序列数已经达到最大
currentStamp = getNextMill();
sequence = 0L;
}
} else {
sequence = 0L; //不同毫秒内,序列号置0
}
lastStamp = currentStamp;
return (currentStamp - START_TIMESTAMP) << TIMESTAMP_LEFT | machineIdPart | sequence; //时间戳部分+机器标识部分+序列号部分
}
private long getNextMill() {
long mill = System.currentTimeMillis();
while (mill <= lastStamp) {
mill = System.currentTimeMillis();
}
return mill;
}
}
public static long uniqueLong() {
return SnowFlake.SNOW_FLAKE.nextId();
}
public static String uniqueLongHex() {
return String.format("%016x", uniqueLong());
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
final CountDownLatch latch = new CountDownLatch(100);
final Map<Long, Integer> map = new ConcurrentHashMap<Long, Integer>();
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 20000; i++) {
Integer put = map.put(UniqueIdUtils.uniqueLong(), 1);
if (put != null) {
throw new RuntimeException("主键重复");
}
}
latch.countDown();
}
}).start();
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(System.currentTimeMillis() - start);
}
}
......@@ -9,7 +9,7 @@
<apollo:config namespaces="COMMON.gic-properties"/>
<bean class="com.gic.demo.common.init.SpringContextHolder"/>
<bean class="com.gic.commons.util.SpringContextHolder"/>
<bean id="proConfig" class="com.gic.commons.init.CustomizedPropertyPlaceholderConfigurer">
<property name="locations">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment