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;
import java.io.Serializable;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.io.Serializable;
/**
* @描述:服务响应实体类
* @作者:shichunshan
......@@ -12,107 +13,159 @@ import java.io.Serializable;
*/
@JsonInclude(value = Include.NON_NULL)
public class ResponseDto<T> implements Serializable {
/**
*
*/
private static final long serialVersionUID = -9004186626234235043L;
/**
* 默认操作成功,成功代码
*/
private static final int SUCCESS = 0;
/**
* 默认成功消息
*/
private static final String SUCCESS_MSG = "操作成功!";
/**
* 错误码
*/
private int errCode;
/**
* 消息
*/
private String message;
/**
* 数据
*/
private T data;
/**
* 默认成功
*/
public ResponseDto() {
super();
this.errCode = SUCCESS;
this.message = SUCCESS_MSG;
}
/**
* @param errCode
* @param message
*/
public ResponseDto(int errCode, String message) {
super();
this.errCode = errCode;
this.message = message;
}
/**
* @param errCode
* @param message
* @param data
*/
public ResponseDto(int errCode, String message, T data) {
super();
this.errCode = errCode;
this.message = message;
this.data = data;
}
/**
* 默认成功
*
* @param data
*/
public ResponseDto(T data) {
super();
this.errCode = SUCCESS;
this.message = SUCCESS_MSG;
this.data = data;
}
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
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;
}
@Override
public String toString() {
return "ResponseDto [errCode=" + errCode + ", message=" + message + ", data=" + data + "]";
}
/**
*
*/
private static final long serialVersionUID = -9004186626234235043L;
/**
* 默认操作成功,成功代码
*/
private static final int SUCCESS = 0;
/**
* 默认成功消息
*/
private static final String SUCCESS_MSG = "操作成功!";
/**
* 错误码
*/
private int errCode;
/**
* 消息
*/
private String message;
/**
* 数据
*/
private T data;
/**
* 生产者名称
*/
private String produceAppName;
/**
* 生产者IP
*/
private String produceIp;
/**
* 生产者类名
*/
private String produceClassName;
/**
* 生产者方法名
*/
private String produceMethodName;
/**
* 默认成功
*/
public ResponseDto() {
super();
this.errCode = SUCCESS;
this.message = SUCCESS_MSG;
}
/**
* @param errCode
* @param message
*/
public ResponseDto(int errCode, String message) {
super();
this.errCode = errCode;
this.message = message;
}
/**
* @param errCode
* @param message
* @param data
*/
public ResponseDto(int errCode, String message, T data) {
super();
this.errCode = errCode;
this.message = message;
this.data = data;
}
/**
* 默认成功
*
* @param data
*/
public ResponseDto(T data) {
super();
this.errCode = SUCCESS;
this.message = SUCCESS_MSG;
this.data = data;
}
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.pcloud.common.config.Application;
import com.pcloud.common.dto.ResponseDto;
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.
*/
public class ResponseHandleUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ResponseHandleUtil.class);
/**
* 解析一层list包装泛型数据
*
* @param obj
* 需要解析的数据对象
* @param clazz
* list内元素类型
* @param <T>
* @return
*/
public static <T> List<T> getListData(Object obj, Class<T> clazz) {
if (null == obj) {
return null;
}
return JSON.parseArray(JSON.toJSONString(obj), clazz);
}
public static <K, V> Map<K, V> getMapData(Object obj, Class<K> clazzK, Class<V> clazzV) {
if (null == obj) {
return null;
}
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;
}
private static final Logger LOGGER = LoggerFactory.getLogger(ResponseHandleUtil.class);
/**
* 解析一层list包装泛型数据
*
* @param obj 需要解析的数据对象
* @param clazz list内元素类型
* @param <T>
* @return
*/
public static <T> List<T> getListData(Object obj, Class<T> clazz) {
if (null == obj) {
return null;
}
return JSON.parseArray(JSON.toJSONString(obj), clazz);
}
/**
* @param obj
* @param clazzK
* @param clazzV
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map<K, V> getMapData(Object obj, Class<K> clazzK, Class<V> clazzV) {
if (null == obj) {
return null;
}
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的泛型数据
*
* @param obj 需要解析的数据对象
* @param clazzK key元素类型
* @param clazzT value中list元素类型
* @return
*/
public static <K, T> Map<K, List<T>> getMapListData(Object obj, Class<K> clazzK, Class<T> clazzT) {
if (null == obj) {
return null;
}
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(obj));
Map<K, List<T>> resultMap = Maps.newHashMap();
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
resultMap.put(JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK),
public static <K, T> Map<K, List<T>> getMapListData(Object obj, Class<K> clazzK, Class<T> clazzT) {
if (null == obj) {
return null;
}
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(obj));
Map<K, List<T>> resultMap = Maps.newHashMap();
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
resultMap.put(JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK),
JSON.parseArray(JSON.toJSONString(entry.getValue()), clazzT));
}
return resultMap;
}
public static <T> T parseResponse(ResponseEntity<?> entity, Class<T> clazz) {
Object obj = parseResponse(entity);
if (null == obj) {
return null;
}
if (clazz.equals(Long.class)) {
if (obj instanceof Long) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(((Number) obj).longValue());
}
} else if (clazz.equals(Integer.class)) {
if (obj instanceof Integer) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(((Number) obj).intValue());
}
} else if (clazz.equals(Double.class)) {
if (obj instanceof Double) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(((Number) obj).doubleValue());
}
} else if (clazz.equals(Float.class)) {
if (obj instanceof Float) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(((Number) obj).floatValue());
}
} else if (clazz.equals(BigDecimal.class)) {
if (obj instanceof BigDecimal) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(BigDecimal.valueOf(((Number) obj).doubleValue()));
}
}
try {
return clazz.cast(obj);
} catch (ClassCastException e) {
return JSON.parseObject(JSON.toJSONString(obj), clazz);
}
}
/**
* List数据解析
*
* @param entity
* @param clazz
* @param <T>
* List元素类型
* @return
*/
public static <T> List<T> parseListResponse(ResponseEntity<?> entity, Class<T> 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
* value class类型
* @param <K>
* @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);
if (null == map) {
return map;
}
Map<K, V> result = new HashMap<>();
for (Object o : map.entrySet()) {
Map.Entry entry = (Map.Entry) o;
K k = JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK);
V v = JSON.parseObject(JSON.toJSONString(entry.getValue()), clazzV);
result.put(k, v);
}
return result;
}
/**
* list转换
*
* @param entity
* @param clazz
* @return
*/
public static <T> List<T> parseList(ResponseEntity<?> entity, Class<T> clazz) {
Object object = parseResponse(entity);
if (object == null) {
return null;
}
return JSONArray.parseArray(JSON.toJSONString(object), clazz);
}
/**
* Map转换
*
* @param entity
* @param clazzK
* @param clazzV
* @return
*/
public static <K, V> Map<K, V> parseMap(ResponseEntity<?> entity, Class<K> clazzK, Class<V> clazzV) {
Object object = parseResponse(entity, Map.class);
if (object == null) {
return null;
}
return getMapData(object, clazzK, clazzV);
}
}
return resultMap;
}
/**
* @param entity
* @param clazz
* @param <T>
* @return
*/
public static <T> T parseResponse(ResponseEntity<?> entity, Class<T> clazz) {
Object obj = parseResponse(entity);
if (null == obj) {
return null;
}
if (clazz.equals(Long.class)) {
if (obj instanceof Long) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(((Number) obj).longValue());
}
} else if (clazz.equals(Integer.class)) {
if (obj instanceof Integer) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(((Number) obj).intValue());
}
} else if (clazz.equals(Double.class)) {
if (obj instanceof Double) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(((Number) obj).doubleValue());
}
} else if (clazz.equals(Float.class)) {
if (obj instanceof Float) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(((Number) obj).floatValue());
}
} else if (clazz.equals(BigDecimal.class)) {
if (obj instanceof BigDecimal) {
return clazz.cast(obj);
} else if (obj instanceof Number) {
return clazz.cast(BigDecimal.valueOf(((Number) obj).doubleValue()));
}
}
try {
return clazz.cast(obj);
} catch (ClassCastException e) {
return JSON.parseObject(JSON.toJSONString(obj), clazz);
}
}
/**
* List数据解析
*
* @param entity
* @param clazz
* @param <T> List元素类型
* @return
*/
public static <T> List<T> parseListResponse(ResponseEntity<?> entity, Class<T> 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 value class类型
* @param <K>
* @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);
if (null == map) {
return map;
}
Map<K, V> result = new HashMap<>();
for (Object o : map.entrySet()) {
Map.Entry entry = (Map.Entry) o;
K k = JSON.parseObject(JSON.toJSONString(entry.getKey()), clazzK);
V v = JSON.parseObject(JSON.toJSONString(entry.getValue()), clazzV);
result.put(k, v);
}
return result;
}
/**
* list转换
*
* @param entity
* @param clazz
* @return
*/
public static <T> List<T> parseList(ResponseEntity<?> entity, Class<T> clazz) {
Object object = parseResponse(entity);
if (object == null) {
return null;
}
return JSONArray.parseArray(JSON.toJSONString(object), clazz);
}
/**
* Map转换
*
* @param entity
* @param clazzK
* @param clazzV
* @return
*/
public static <K, V> Map<K, V> parseMap(ResponseEntity<?> entity, Class<K> clazzK, Class<V> clazzV) {
Object object = parseResponse(entity, Map.class);
if (object == null) {
return null;
}
return getMapData(object, clazzK, clazzV);
}
/**
* Map List转换
*
* @param entity 需要解析的数据对象
* @param clazzK key元素类型
* @param clazzT value中list元素类型
......@@ -205,43 +219,131 @@ public class ResponseHandleUtil {
}
return getMapListData(object, clazzK, clazzT);
}
public static Object parseResponse(ResponseEntity<?> entity) {
if (null == entity) {
LOGGER.warn("null response object");
return null;
}
int statusCode = entity.getStatusCodeValue();
Object errMsg = entity.getBody();
if (statusCode == 500) {
LOGGER.error("【内部调用异常】异常信息: " + errMsg);
throw new RuntimeException("【内部调用异常】异常信息: " + errMsg);
} else if (statusCode == 400) {
LOGGER.warn("【请求异常】异常信息: " + errMsg);
throw new RuntimeException("【请求异常】异常信息: " + errMsg);
} else if (statusCode != 200) {
LOGGER.warn("【请求错误】错误信息: " + errMsg);
throw new RuntimeException("【请求错误】错误信息: " + errMsg);
}
ResponseDto<?> body = (ResponseDto<?>) entity.getBody();
if (body == null) {
LOGGER.warn("【请求NULL】ResponseDto==null");
return null;
}
if (body.getErrCode() != 0) {
LOGGER.warn("【业务异常】:" + body.getErrCode() + ", 异常信息: " + body.getMessage());
throw new BizException(body.getErrCode(), body.getMessage());
}
return body.getData();
}
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);
return new ResponseEntity<ResponseDto<T>>(body, HttpStatus.OK);
}
/**
* 内部接口返回值解析
*
* @param entity
* @return
*/
public static Object parseResponse(ResponseEntity<?> entity) {
if (null == entity) {
LOGGER.warn("null response object");
return null;
}
int statusCode = entity.getStatusCodeValue();
Object errMsg = entity.getBody();
if (statusCode == 500) {
LOGGER.error("【内部调用异常】异常信息: " + errMsg);
throw new RuntimeException("【内部调用异常】异常信息: " + errMsg);
} else if (statusCode == 400) {
LOGGER.warn("【请求异常】异常信息: " + errMsg);
throw new RuntimeException("【请求异常】异常信息: " + errMsg);
} else if (statusCode != 200) {
LOGGER.warn("【请求错误】错误信息: " + errMsg);
throw new RuntimeException("【请求错误】错误信息: " + errMsg);
}
ResponseDto<?> body = (ResponseDto<?>) entity.getBody();
if (body == null) {
LOGGER.warn("【请求NULL】ResponseDto==null");
return null;
}
if (body.getErrCode() != 0) {
LOGGER.warn("【业务异常】:" + body.getErrCode() + ", 异常信息: " + body.getMessage());
throw new BizException(body.getErrCode(), body.getMessage());
}
// 获取当前的整个链路的调用类和方法,发送服务调用链queue
try {
StackTraceElement[] stackTraceElements = new Exception().getStackTrace();
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;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @描述:
......@@ -10,26 +12,31 @@ import java.util.Map;
* @版本:1.0
*/
public class ThreadUtil {
@SuppressWarnings("rawtypes")
private static final ThreadLocal ctx = new ThreadLocal();
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void put(Object key, Object value) {
Map m = (Map) ctx.get();
if (m == null) {
m = new HashMap();
}
m.put(key, value);
ctx.set(m);
}
public static Object get(Object key) {
@SuppressWarnings("rawtypes")
Map m = (Map) ctx.get();
if (m != null) {
return m.get(key);
}
return null;
}
/**
* 固定长度程池
*/
public static final ExecutorService FIXED_POOL = Executors.newFixedThreadPool(3);
@SuppressWarnings("rawtypes")
private static final ThreadLocal ctx = new ThreadLocal();
@SuppressWarnings({"rawtypes", "unchecked"})
public static void put(Object key, Object value) {
Map m = (Map) ctx.get();
if (m == null) {
m = new HashMap();
}
m.put(key, value);
ctx.set(m);
}
public static Object get(Object key) {
@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