Commit 6c618daa by songxiang

增加内部接口调用链路跟踪

parent 2d9ecb8d
package com.pcloud.common.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Author: songx
* @Date: 2019年07月11日, 14:11
*/
@Component
public class Application {
/**
* 应用名称
*/
public static String APP_NAME;
@Value("${spring.application.name}")
public void setAppName(String appName) {
this.APP_NAME = appName;
}
}
package com.pcloud.common.dto; package com.pcloud.common.dto;
import java.io.Serializable;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.io.Serializable;
/** /**
* @描述:服务响应实体类 * @描述:服务响应实体类
* @作者:shichunshan * @作者:shichunshan
...@@ -12,107 +13,159 @@ import java.io.Serializable; ...@@ -12,107 +13,159 @@ import java.io.Serializable;
*/ */
@JsonInclude(value = Include.NON_NULL) @JsonInclude(value = Include.NON_NULL)
public class ResponseDto<T> implements Serializable { public class ResponseDto<T> implements Serializable {
/** /**
* *
*/ */
private static final long serialVersionUID = -9004186626234235043L; private static final long serialVersionUID = -9004186626234235043L;
/** /**
* 默认操作成功,成功代码 * 默认操作成功,成功代码
*/ */
private static final int SUCCESS = 0; private static final int SUCCESS = 0;
/** /**
* 默认成功消息 * 默认成功消息
*/ */
private static final String SUCCESS_MSG = "操作成功!"; private static final String SUCCESS_MSG = "操作成功!";
/** /**
* 错误码 * 错误码
*/ */
private int errCode; private int errCode;
/** /**
* 消息 * 消息
*/ */
private String message; private String message;
/** /**
* 数据 * 数据
*/ */
private T data; private T data;
/** /**
* 默认成功 * 生产者名称
*/ */
public ResponseDto() { private String produceAppName;
super();
this.errCode = SUCCESS; /**
this.message = SUCCESS_MSG; * 生产者IP
} */
private String produceIp;
/**
* @param errCode /**
* @param message * 生产者类名
*/ */
public ResponseDto(int errCode, String message) { private String produceClassName;
super();
this.errCode = errCode; /**
this.message = message; * 生产者方法名
} */
private String produceMethodName;
/**
* @param errCode /**
* @param message * 默认成功
* @param data */
*/ public ResponseDto() {
public ResponseDto(int errCode, String message, T data) { super();
super(); this.errCode = SUCCESS;
this.errCode = errCode; this.message = SUCCESS_MSG;
this.message = message; }
this.data = data;
} /**
* @param errCode
/** * @param message
* 默认成功 */
* public ResponseDto(int errCode, String message) {
* @param data super();
*/ this.errCode = errCode;
public ResponseDto(T data) { this.message = message;
super(); }
this.errCode = SUCCESS;
this.message = SUCCESS_MSG; /**
this.data = data; * @param errCode
} * @param message
* @param data
public int getErrCode() { */
return errCode; public ResponseDto(int errCode, String message, T data) {
} super();
this.errCode = errCode;
public void setErrCode(int errCode) { this.message = message;
this.errCode = errCode; this.data = data;
} }
public String getMessage() { /**
return message; * 默认成功
} *
* @param data
public void setMessage(String message) { */
this.message = message; public ResponseDto(T data) {
} super();
this.errCode = SUCCESS;
public T getData() { this.message = SUCCESS_MSG;
return data; this.data = data;
} }
public void setData(T data) { public int getErrCode() {
this.data = data; return errCode;
} }
@Override public void setErrCode(int errCode) {
public String toString() { this.errCode = errCode;
return "ResponseDto [errCode=" + errCode + ", message=" + message + ", data=" + data + "]"; }
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getProduceAppName() {
return produceAppName;
}
public void setProduceAppName(String produceAppName) {
this.produceAppName = produceAppName;
}
public String getProduceClassName() {
return produceClassName;
}
public void setProduceClassName(String produceClassName) {
this.produceClassName = produceClassName;
}
public String getProduceMethodName() {
return produceMethodName;
}
public void setProduceMethodName(String produceMethodName) {
this.produceMethodName = produceMethodName;
}
public String getProduceIp() {
return produceIp;
}
public void setProduceIp(String produceIp) {
this.produceIp = produceIp;
}
@Override
public String toString() {
return "ResponseDto -> " + JSON.toJSONString(this);
}
} }
package com.pcloud.common.mq.constant;
/**
* @Author: songx
* @Date: 2019年07月11日, 14:48
*/
public class QueueNameConstant {
/**
* 服务调用链路
*/
public static final String SERVER_LINK = "serverLinkQueue";
}
package com.pcloud.common.mq.dto;
import com.pcloud.common.dto.BaseDto;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* @Author: songx
* @Date: 2019年07月11日, 14:52
*/
@Getter
@Setter
@ToString(callSuper = true)
public class ServerLinkDTO extends BaseDto {
private static final long serialVersionUID = 2029227731936018946L;
/**
* 消费者服务名称
*/
private String consumerAppName;
/**
* 消费者IP
*/
private String consumerIp;
/**
* 消费者类名
*/
private String consumerClassName;
/**
* 消费者方法名
*/
private String consumerMethodName;
/**
* 生产者服务名称
*/
private String produceAppName;
/**
* 生产者IP
*/
private String produceIp;
/**
* 生产者类名
*/
private String produceClassName;
/**
* 生产者方法名
*/
private String produceMethodName;
/**
* 完整的调用链路
*/
private String links;
}
/**
*
*/
package com.pcloud.common.mq.queue;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.pcloud.common.exceptions.BizException;
import com.pcloud.common.mq.constant.QueueNameConstant;
import com.pcloud.common.mq.dto.ServerLinkDTO;
import lombok.extern.slf4j.Slf4j;
/**
* @author:songx
* @date:2019/6/20,11:37
*/
@Slf4j
@Component
public class ServerLinkQueue {
/**
* 服务调用链路QUEUE
*/
public static void send(ServerLinkDTO serverLinkDTO) throws BizException {
log.info("【QUEUE】服务调用链路.[serverLinkDTO]->" + serverLinkDTO);
if (serverLinkDTO == null) {
throw BizException.PARAM_IS_NULL;
}
getAmqpTemplate().convertAndSend(QueueNameConstant.SERVER_LINK, serverLinkDTO);
}
/**
* 获取实例
*
* @return
*/
public static AmqpTemplate getAmqpTemplate() {
return SingletonInstance.amqpTemplate;
}
@Component
public static class SingletonInstance {
private static AmqpTemplate amqpTemplate;
public SingletonInstance(@Autowired AmqpTemplate amqpTemplate) {
this.amqpTemplate = amqpTemplate;
}
}
}
...@@ -4,195 +4,209 @@ import java.math.BigDecimal; ...@@ -4,195 +4,209 @@ import java.math.BigDecimal;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.pcloud.common.config.Application;
import com.pcloud.common.dto.ResponseDto; import com.pcloud.common.dto.ResponseDto;
import com.pcloud.common.exceptions.BizException; import com.pcloud.common.exceptions.BizException;
import com.pcloud.common.mq.dto.ServerLinkDTO;
import com.pcloud.common.mq.queue.ServerLinkQueue;
import com.pcloud.common.utils.nginx.NginxUtils;
/** /**
* Created by zengqiang on 17-12-14. * Created by zengqiang on 17-12-14.
*/ */
public class ResponseHandleUtil { public class ResponseHandleUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ResponseHandleUtil.class); private static final Logger LOGGER = LoggerFactory.getLogger(ResponseHandleUtil.class);
/** /**
* 解析一层list包装泛型数据 * 解析一层list包装泛型数据
* *
* @param obj * @param obj 需要解析的数据对象
* 需要解析的数据对象 * @param clazz list内元素类型
* @param clazz * @param <T>
* list内元素类型 * @return
* @param <T> */
* @return public static <T> List<T> getListData(Object obj, Class<T> clazz) {
*/ if (null == obj) {
public static <T> List<T> getListData(Object obj, Class<T> clazz) { return null;
if (null == obj) { }
return null; return JSON.parseArray(JSON.toJSONString(obj), clazz);
} }
return JSON.parseArray(JSON.toJSONString(obj), clazz);
} /**
* @param obj
public static <K, V> Map<K, V> getMapData(Object obj, Class<K> clazzK, Class<V> clazzV) { * @param clazzK
if (null == obj) { * @param clazzV
return null; * @param <K>
} * @param <V>
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(obj)); * @return
Map<K, V> resultMap = Maps.newHashMap(); */
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { public static <K, V> Map<K, V> getMapData(Object obj, Class<K> clazzK, Class<V> clazzV) {
resultMap.put(JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK), if (null == obj) {
JSON.parseObject(JSON.toJSONString(entry.getValue()), clazzV)); return null;
} }
return resultMap; JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(obj));
} Map<K, V> resultMap = Maps.newHashMap();
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
resultMap.put(JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK),
JSON.parseObject(JSON.toJSONString(entry.getValue()), clazzV));
}
return resultMap;
}
/** /**
* 解析map的value是list的泛型数据 * 解析map的value是list的泛型数据
*
* @param obj 需要解析的数据对象 * @param obj 需要解析的数据对象
* @param clazzK key元素类型 * @param clazzK key元素类型
* @param clazzT value中list元素类型 * @param clazzT value中list元素类型
* @return * @return
*/ */
public static <K, T> Map<K, List<T>> getMapListData(Object obj, Class<K> clazzK, Class<T> clazzT) { public static <K, T> Map<K, List<T>> getMapListData(Object obj, Class<K> clazzK, Class<T> clazzT) {
if (null == obj) { if (null == obj) {
return null; return null;
} }
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(obj)); JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(obj));
Map<K, List<T>> resultMap = Maps.newHashMap(); Map<K, List<T>> resultMap = Maps.newHashMap();
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
resultMap.put(JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK), resultMap.put(JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK),
JSON.parseArray(JSON.toJSONString(entry.getValue()), clazzT)); JSON.parseArray(JSON.toJSONString(entry.getValue()), clazzT));
} }
return resultMap; return resultMap;
} }
public static <T> T parseResponse(ResponseEntity<?> entity, Class<T> clazz) { /**
Object obj = parseResponse(entity); * @param entity
if (null == obj) { * @param clazz
return null; * @param <T>
} * @return
if (clazz.equals(Long.class)) { */
if (obj instanceof Long) { public static <T> T parseResponse(ResponseEntity<?> entity, Class<T> clazz) {
return clazz.cast(obj); Object obj = parseResponse(entity);
} else if (obj instanceof Number) { if (null == obj) {
return clazz.cast(((Number) obj).longValue()); return null;
} }
} else if (clazz.equals(Integer.class)) { if (clazz.equals(Long.class)) {
if (obj instanceof Integer) { if (obj instanceof Long) {
return clazz.cast(obj); return clazz.cast(obj);
} else if (obj instanceof Number) { } else if (obj instanceof Number) {
return clazz.cast(((Number) obj).intValue()); return clazz.cast(((Number) obj).longValue());
} }
} else if (clazz.equals(Double.class)) { } else if (clazz.equals(Integer.class)) {
if (obj instanceof Double) { if (obj instanceof Integer) {
return clazz.cast(obj); return clazz.cast(obj);
} else if (obj instanceof Number) { } else if (obj instanceof Number) {
return clazz.cast(((Number) obj).doubleValue()); return clazz.cast(((Number) obj).intValue());
} }
} else if (clazz.equals(Float.class)) { } else if (clazz.equals(Double.class)) {
if (obj instanceof Float) { if (obj instanceof Double) {
return clazz.cast(obj); return clazz.cast(obj);
} else if (obj instanceof Number) { } else if (obj instanceof Number) {
return clazz.cast(((Number) obj).floatValue()); return clazz.cast(((Number) obj).doubleValue());
} }
} else if (clazz.equals(BigDecimal.class)) { } else if (clazz.equals(Float.class)) {
if (obj instanceof BigDecimal) { if (obj instanceof Float) {
return clazz.cast(obj); return clazz.cast(obj);
} else if (obj instanceof Number) { } else if (obj instanceof Number) {
return clazz.cast(BigDecimal.valueOf(((Number) obj).doubleValue())); return clazz.cast(((Number) obj).floatValue());
} }
} } else if (clazz.equals(BigDecimal.class)) {
try { if (obj instanceof BigDecimal) {
return clazz.cast(obj); return clazz.cast(obj);
} catch (ClassCastException e) { } else if (obj instanceof Number) {
return JSON.parseObject(JSON.toJSONString(obj), clazz); return clazz.cast(BigDecimal.valueOf(((Number) obj).doubleValue()));
} }
} }
try {
/** return clazz.cast(obj);
* List数据解析 } catch (ClassCastException e) {
* return JSON.parseObject(JSON.toJSONString(obj), clazz);
* @param entity }
* @param clazz }
* @param <T>
* List元素类型 /**
* @return * List数据解析
*/ *
public static <T> List<T> parseListResponse(ResponseEntity<?> entity, Class<T> clazz) { * @param entity
List list = parseResponse(entity, List.class); * @param clazz
if (null == list) { * @param <T> List元素类型
return list; * @return
} */
public static <T> List<T> parseListResponse(ResponseEntity<?> entity, Class<T> clazz) {
return JSON.parseArray(JSON.toJSONString(list), clazz); List list = parseResponse(entity, List.class);
} if (null == list) {
return list;
/** }
* return JSON.parseArray(JSON.toJSONString(list), clazz);
* @param entity }
* @param clazzK
* key class类型 /**
* @param clazzV * @param entity
* value class类型 * @param clazzK key class类型
* @param <K> * @param clazzV value class类型
* @param <V> * @param <K>
* @return * @param <V>
*/ * @return
public static <K, V> Map<K, V> parseMapResponse(ResponseEntity<?> entity, Class<K> clazzK, Class<V> clazzV) { */
Map map = parseResponse(entity, Map.class); public static <K, V> Map<K, V> parseMapResponse(ResponseEntity<?> entity, Class<K> clazzK, Class<V> clazzV) {
if (null == map) { Map map = parseResponse(entity, Map.class);
return map; if (null == map) {
} return map;
}
Map<K, V> result = new HashMap<>(); Map<K, V> result = new HashMap<>();
for (Object o : map.entrySet()) { for (Object o : map.entrySet()) {
Map.Entry entry = (Map.Entry) o; Map.Entry entry = (Map.Entry) o;
K k = JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK); K k = JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK);
V v = JSON.parseObject(JSON.toJSONString(entry.getValue()), clazzV); V v = JSON.parseObject(JSON.toJSONString(entry.getValue()), clazzV);
result.put(k, v); result.put(k, v);
} }
return result; return result;
} }
/** /**
* list转换 * list转换
* *
* @param entity * @param entity
* @param clazz * @param clazz
* @return * @return
*/ */
public static <T> List<T> parseList(ResponseEntity<?> entity, Class<T> clazz) { public static <T> List<T> parseList(ResponseEntity<?> entity, Class<T> clazz) {
Object object = parseResponse(entity); Object object = parseResponse(entity);
if (object == null) { if (object == null) {
return null; return null;
} }
return JSONArray.parseArray(JSON.toJSONString(object), clazz); return JSONArray.parseArray(JSON.toJSONString(object), clazz);
} }
/** /**
* Map转换 * Map转换
* *
* @param entity * @param entity
* @param clazzK * @param clazzK
* @param clazzV * @param clazzV
* @return * @return
*/ */
public static <K, V> Map<K, V> parseMap(ResponseEntity<?> entity, Class<K> clazzK, Class<V> clazzV) { public static <K, V> Map<K, V> parseMap(ResponseEntity<?> entity, Class<K> clazzK, Class<V> clazzV) {
Object object = parseResponse(entity, Map.class); Object object = parseResponse(entity, Map.class);
if (object == null) { if (object == null) {
return null; return null;
} }
return getMapData(object, clazzK, clazzV); return getMapData(object, clazzK, clazzV);
} }
/** /**
* Map List转换 * Map List转换
*
* @param entity 需要解析的数据对象 * @param entity 需要解析的数据对象
* @param clazzK key元素类型 * @param clazzK key元素类型
* @param clazzT value中list元素类型 * @param clazzT value中list元素类型
...@@ -205,43 +219,131 @@ public class ResponseHandleUtil { ...@@ -205,43 +219,131 @@ public class ResponseHandleUtil {
} }
return getMapListData(object, clazzK, clazzT); return getMapListData(object, clazzK, clazzT);
} }
public static Object parseResponse(ResponseEntity<?> entity) { /**
if (null == entity) { * 内部接口返回值解析
LOGGER.warn("null response object"); *
return null; * @param entity
} * @return
int statusCode = entity.getStatusCodeValue(); */
Object errMsg = entity.getBody(); public static Object parseResponse(ResponseEntity<?> entity) {
if (statusCode == 500) { if (null == entity) {
LOGGER.error("【内部调用异常】异常信息: " + errMsg); LOGGER.warn("null response object");
throw new RuntimeException("【内部调用异常】异常信息: " + errMsg); return null;
} else if (statusCode == 400) { }
LOGGER.warn("【请求异常】异常信息: " + errMsg); int statusCode = entity.getStatusCodeValue();
throw new RuntimeException("【请求异常】异常信息: " + errMsg); Object errMsg = entity.getBody();
} else if (statusCode != 200) { if (statusCode == 500) {
LOGGER.warn("【请求错误】错误信息: " + errMsg); LOGGER.error("【内部调用异常】异常信息: " + errMsg);
throw new RuntimeException("【请求错误】错误信息: " + errMsg); throw new RuntimeException("【内部调用异常】异常信息: " + errMsg);
} } else if (statusCode == 400) {
ResponseDto<?> body = (ResponseDto<?>) entity.getBody(); LOGGER.warn("【请求异常】异常信息: " + errMsg);
if (body == null) { throw new RuntimeException("【请求异常】异常信息: " + errMsg);
LOGGER.warn("【请求NULL】ResponseDto==null"); } else if (statusCode != 200) {
return null; LOGGER.warn("【请求错误】错误信息: " + errMsg);
} throw new RuntimeException("【请求错误】错误信息: " + errMsg);
if (body.getErrCode() != 0) { }
LOGGER.warn("【业务异常】:" + body.getErrCode() + ", 异常信息: " + body.getMessage()); ResponseDto<?> body = (ResponseDto<?>) entity.getBody();
throw new BizException(body.getErrCode(), body.getMessage()); if (body == null) {
} LOGGER.warn("【请求NULL】ResponseDto==null");
return body.getData(); return null;
} }
if (body.getErrCode() != 0) {
public static <T> ResponseEntity<ResponseDto<T>> toResponse(T data) { LOGGER.warn("【业务异常】:" + body.getErrCode() + ", 异常信息: " + body.getMessage());
if (null == data) { throw new BizException(body.getErrCode(), body.getMessage());
LOGGER.warn("null data object"); }
return null; // 获取当前的整个链路的调用类和方法,发送服务调用链queue
} try {
ResponseDto<T> body = new ResponseDto<>(data); StackTraceElement[] stackTraceElements = new Exception().getStackTrace();
return new ResponseEntity<ResponseDto<T>>(body, HttpStatus.OK); ThreadUtil.FIXED_POOL.execute(() -> sendServerLink(body, stackTraceElements));
} } catch (Exception e) {
LOGGER.warn("【内部调用】获取调用类和方法失败:" + e.getMessage(), e);
}
return body.getData();
}
/**
* 发送服务调用链queue
*
* @param body
*/
private static void sendServerLink(ResponseDto<?> body, StackTraceElement[] stackTraceElements) {
ServerLinkDTO serverLinkDTO = new ServerLinkDTO();
int index = 2;
// 如果第二级是parseMap或parseList表明是当前类的方法,则索引更新为3
String methodName = stackTraceElements[index].getMethodName();
if (methodName.startsWith("parseMap") || methodName.startsWith("parseList")) {
index = 3;
}
serverLinkDTO.setConsumerClassName(stackTraceElements[index].getClassName());
serverLinkDTO.setConsumerMethodName(stackTraceElements[index].getMethodName());
serverLinkDTO.setConsumerAppName(Application.APP_NAME);
serverLinkDTO.setConsumerIp(NginxUtils.getLocalAddress());
serverLinkDTO.setProduceAppName(body.getProduceAppName());
serverLinkDTO.setProduceIp(body.getProduceIp());
serverLinkDTO.setProduceClassName(body.getProduceClassName());
serverLinkDTO.setProduceMethodName(body.getProduceMethodName());
serverLinkDTO.setLinks(appendLinks(stackTraceElements, index));
try {
ServerLinkQueue.send(serverLinkDTO);
} catch (Exception e) {
LOGGER.warn("【内部调用】发送服务调用链QUEUE失败:" + e.getMessage(), e);
}
}
/**
* 拼接整个调用链路
*
* @param stackTraceElements
* @return
*/
private static String appendLinks(StackTraceElement[] stackTraceElements, int index) {
if (stackTraceElements == null || stackTraceElements.length == 0) {
return null;
}
int i = 0;
StringBuilder links = new StringBuilder();
for (StackTraceElement element : stackTraceElements) {
// 过滤index以前的数据,index以前的数据都是本类的方法,无需记录
if (i < index) {
i++;
continue;
}
String className = element.getClassName();
// 只拼接调用的业务类和方法,JDK的类不拼接
if (!className.startsWith("com.pcloud") || className.indexOf("$$FastClassBySpringCGLIB$$") > 0 || className
.indexOf("$$EnhancerBySpringCGLIB$$") > 0) {
continue;
}
links.append(className).append(".").append(element.getMethodName()).append("->");
}
return links.toString();
}
/**
* 内部接口封装返回类型
*
* @param data
* @param <T>
* @return
*/
public static <T> ResponseEntity<ResponseDto<T>> toResponse(T data) {
if (null == data) {
LOGGER.warn("null data object");
return null;
}
ResponseDto<T> body = new ResponseDto<>(data);
try {
body.setProduceAppName(Application.APP_NAME);
body.setProduceIp(NginxUtils.getLocalAddress());
StackTraceElement stackTraceElement = new Exception().getStackTrace()[1];
body.setProduceClassName(stackTraceElement.getClassName());
body.setProduceMethodName(stackTraceElement.getMethodName());
} catch (Exception e) {
LOGGER.warn("【内部接口】封装返回类型失败:" + e.getMessage(), e);
}
return new ResponseEntity<>(body, HttpStatus.OK);
}
} }
\ No newline at end of file
...@@ -2,6 +2,8 @@ package com.pcloud.common.utils; ...@@ -2,6 +2,8 @@ package com.pcloud.common.utils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* @描述: * @描述:
...@@ -10,26 +12,31 @@ import java.util.Map; ...@@ -10,26 +12,31 @@ import java.util.Map;
* @版本:1.0 * @版本:1.0
*/ */
public class ThreadUtil { public class ThreadUtil {
@SuppressWarnings("rawtypes") /**
private static final ThreadLocal ctx = new ThreadLocal(); * 固定长度程池
*/
@SuppressWarnings({ "rawtypes", "unchecked" }) public static final ExecutorService FIXED_POOL = Executors.newFixedThreadPool(3);
public static void put(Object key, Object value) {
Map m = (Map) ctx.get(); @SuppressWarnings("rawtypes")
if (m == null) { private static final ThreadLocal ctx = new ThreadLocal();
m = new HashMap();
} @SuppressWarnings({"rawtypes", "unchecked"})
m.put(key, value); public static void put(Object key, Object value) {
ctx.set(m); Map m = (Map) ctx.get();
} if (m == null) {
m = new HashMap();
public static Object get(Object key) { }
@SuppressWarnings("rawtypes") m.put(key, value);
Map m = (Map) ctx.get(); ctx.set(m);
if (m != null) { }
return m.get(key);
} public static Object get(Object key) {
return null; @SuppressWarnings("rawtypes")
} Map m = (Map) ctx.get();
if (m != null) {
return m.get(key);
}
return null;
}
} }
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