This commit is contained in:
wudashan 2018-03-06 00:28:43 +08:00
parent 903c4ce255
commit 57c8d0ced3
4 changed files with 45 additions and 47 deletions

View File

@ -1,11 +1,12 @@
package cn.wudashan;
import cn.wudashan.executor.BaseThreadPoolExecutor;
import cn.wudashan.executor.MDCRunnable;
import cn.wudashan.util.MDCUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 主函数
@ -13,26 +14,36 @@ import java.util.UUID;
*/
public class Main {
private static final String KEY = "requestId";
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
public static void main(String[] args) {
MDCUtil.put(KEY, UUID.randomUUID().toString());
// 入口传入请求ID
MDCUtil.putRequestId();
// 主线程打印日志
logger.debug("log in main thread");
BaseThreadPoolExecutor executor = new BaseThreadPoolExecutor();
executor.execute(new Runnable() {
// 异步线程打印日志用MDCRunnable装饰Runnable
new Thread(new MDCRunnable(new Runnable() {
@Override
public void run() {
logger.debug("log in base thread pool");
logger.debug("log in other thread");
}
});
executor.shutdown();
})).start();
MDCUtil.remove(KEY);
// 异步线程池打印日志用MDCRunnable装饰Runnable
EXECUTOR.execute(new MDCRunnable(new Runnable() {
@Override
public void run() {
logger.debug("log in other thread pool");
}
}));
EXECUTOR.shutdown();
// 出口移除请求ID
MDCUtil.removeRequestId();
}

View File

@ -1,31 +0,0 @@
package cn.wudashan.executor;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 基本线程池类
* @author wudashan
*/
public class BaseThreadPoolExecutor {
private final ThreadPoolExecutor threadPoolExecutor;
public BaseThreadPoolExecutor() {
this.threadPoolExecutor = new ThreadPoolExecutor(10, 10, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));
}
public void execute(Runnable runnable) {
runnable = new BaseRunnable(runnable);
threadPoolExecutor.execute(runnable);
}
public void shutdown() {
threadPoolExecutor.shutdown();
}
}

View File

@ -1,6 +1,7 @@
package cn.wudashan.executor;
import cn.wudashan.util.MDCUtil;
import org.slf4j.MDC;
import java.util.Map;
@ -8,28 +9,30 @@ import java.util.Map;
* 基本任务类
* @author wudashan
*/
public class BaseRunnable implements Runnable {
public class MDCRunnable implements Runnable {
private final Runnable runnable;
private final Map<String, String> map;
public BaseRunnable(Runnable runnable) {
public MDCRunnable(Runnable runnable) {
this.runnable = runnable;
this.map = MDCUtil.getCopyOfContextMap();
// 保存当前线程的MDC值
this.map = MDC.getCopyOfContextMap();
}
@Override
public void run() {
// 传入已保存的MDC值
for (Map.Entry<String, String> entry : map.entrySet()) {
MDCUtil.put(entry.getKey(), entry.getValue());
}
// 装饰器模式执行run方法
runnable.run();
// 移除已保存的MDC值
for (Map.Entry<String, String> entry : map.entrySet()) {
MDCUtil.remove(entry.getKey());
}
}
}

View File

@ -4,6 +4,7 @@ import org.slf4j.MDC;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
/**
* MDC工具类屏蔽异常和空指针
@ -11,6 +12,8 @@ import java.util.Map;
*/
public class MDCUtil {
private static final String REQUEST_ID = "requestId";
public static Map<String, String> getCopyOfContextMap() {
Map<String, String> result;
try {
@ -36,4 +39,16 @@ public class MDCUtil {
} catch (RuntimeException ignored) {}
}
public static void putRequestId() {
try {
MDC.put(REQUEST_ID, UUID.randomUUID().toString());
} catch (RuntimeException ignored) {}
}
public static void removeRequestId() {
try {
MDC.remove(REQUEST_ID);
} catch (RuntimeException ignored) {}
}
}