refactor
This commit is contained in:
		| @@ -1,11 +1,12 @@ | |||||||
| package cn.wudashan; | package cn.wudashan; | ||||||
|  |  | ||||||
| import cn.wudashan.executor.BaseThreadPoolExecutor; | import cn.wudashan.executor.MDCRunnable; | ||||||
| import cn.wudashan.util.MDCUtil; | import cn.wudashan.util.MDCUtil; | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | 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 { | public class Main { | ||||||
|  |  | ||||||
|     private static final String KEY = "requestId"; |  | ||||||
|  |  | ||||||
|     private static final Logger logger = LoggerFactory.getLogger(Main.class); |     private static final Logger logger = LoggerFactory.getLogger(Main.class); | ||||||
|  |     private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor(); | ||||||
|  |  | ||||||
|     public static void main(String[] args) { |     public static void main(String[] args) { | ||||||
|  |  | ||||||
|         MDCUtil.put(KEY, UUID.randomUUID().toString()); |         // 入口传入请求ID | ||||||
|  |         MDCUtil.putRequestId(); | ||||||
|  |  | ||||||
|  |         // 主线程打印日志 | ||||||
|         logger.debug("log in main thread"); |         logger.debug("log in main thread"); | ||||||
|  |  | ||||||
|         BaseThreadPoolExecutor executor = new BaseThreadPoolExecutor(); |         // 异步线程打印日志,用MDCRunnable装饰Runnable | ||||||
|         executor.execute(new Runnable() { |         new Thread(new MDCRunnable(new Runnable() { | ||||||
|             @Override |             @Override | ||||||
|             public void run() { |             public void run() { | ||||||
|                 logger.debug("log in base thread pool"); |                 logger.debug("log in other thread"); | ||||||
|             } |             } | ||||||
|         }); |         })).start(); | ||||||
|         executor.shutdown(); |  | ||||||
|  |  | ||||||
|         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(); | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| } |  | ||||||
| @@ -1,6 +1,7 @@ | |||||||
| package cn.wudashan.executor; | package cn.wudashan.executor; | ||||||
| 
 | 
 | ||||||
| import cn.wudashan.util.MDCUtil; | import cn.wudashan.util.MDCUtil; | ||||||
|  | import org.slf4j.MDC; | ||||||
| 
 | 
 | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| @@ -8,28 +9,30 @@ import java.util.Map; | |||||||
|  * 基本任务类 |  * 基本任务类 | ||||||
|  * @author wudashan |  * @author wudashan | ||||||
|  */ |  */ | ||||||
| public class BaseRunnable implements Runnable { | public class MDCRunnable implements Runnable { | ||||||
| 
 | 
 | ||||||
|     private final Runnable runnable; |     private final Runnable runnable; | ||||||
| 
 | 
 | ||||||
|     private final Map<String, String> map; |     private final Map<String, String> map; | ||||||
| 
 | 
 | ||||||
|     public BaseRunnable(Runnable runnable) { |     public MDCRunnable(Runnable runnable) { | ||||||
|         this.runnable = runnable; |         this.runnable = runnable; | ||||||
|         this.map = MDCUtil.getCopyOfContextMap(); |         // 保存当前线程的MDC值 | ||||||
|  |         this.map = MDC.getCopyOfContextMap(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void run() { |     public void run() { | ||||||
|  |         // 传入已保存的MDC值 | ||||||
|         for (Map.Entry<String, String> entry : map.entrySet()) { |         for (Map.Entry<String, String> entry : map.entrySet()) { | ||||||
|             MDCUtil.put(entry.getKey(), entry.getValue()); |             MDCUtil.put(entry.getKey(), entry.getValue()); | ||||||
|         } |         } | ||||||
|  |         // 装饰器模式,执行run方法 | ||||||
|         runnable.run(); |         runnable.run(); | ||||||
|  |         // 移除已保存的MDC值 | ||||||
|         for (Map.Entry<String, String> entry : map.entrySet()) { |         for (Map.Entry<String, String> entry : map.entrySet()) { | ||||||
|             MDCUtil.remove(entry.getKey()); |             MDCUtil.remove(entry.getKey()); | ||||||
|         } |         } | ||||||
| 
 |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| } | } | ||||||
| @@ -4,6 +4,7 @@ import org.slf4j.MDC; | |||||||
|  |  | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
|  | import java.util.UUID; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * MDC工具类,屏蔽异常和空指针 |  * MDC工具类,屏蔽异常和空指针 | ||||||
| @@ -11,6 +12,8 @@ import java.util.Map; | |||||||
|  */ |  */ | ||||||
| public class MDCUtil { | public class MDCUtil { | ||||||
|  |  | ||||||
|  |     private static final String REQUEST_ID = "requestId"; | ||||||
|  |  | ||||||
|     public static Map<String, String> getCopyOfContextMap() { |     public static Map<String, String> getCopyOfContextMap() { | ||||||
|         Map<String, String> result; |         Map<String, String> result; | ||||||
|         try { |         try { | ||||||
| @@ -36,4 +39,16 @@ public class MDCUtil { | |||||||
|         } catch (RuntimeException ignored) {} |         } 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) {} | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user