commit e0087741bcea0fe504d8301c320c6467772cd989 Author: wudashan Date: Wed Feb 14 20:03:36 2018 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de4f6c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +target/ +*.iml +*.class \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_10_0.xml b/.idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_10_0.xml new file mode 100644 index 0000000..cbe5ca7 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_10_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_logging_log4j_log4j_core_2_10_0.xml b/.idea/libraries/Maven__org_apache_logging_log4j_log4j_core_2_10_0.xml new file mode 100644 index 0000000..f061a60 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_logging_log4j_log4j_core_2_10_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_logging_log4j_log4j_slf4j_impl_2_10_0.xml b/.idea/libraries/Maven__org_apache_logging_log4j_log4j_slf4j_impl_2_10_0.xml new file mode 100644 index 0000000..9a53275 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_logging_log4j_log4j_slf4j_impl_2_10_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_slf4j_slf4j_api_1_8_0_alpha2.xml b/.idea/libraries/Maven__org_slf4j_slf4j_api_1_8_0_alpha2.xml new file mode 100644 index 0000000..e678f37 --- /dev/null +++ b/.idea/libraries/Maven__org_slf4j_slf4j_api_1_8_0_alpha2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3e4fb7c --- /dev/null +++ b/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + cn.wudashan + slf4j-mdc-muti-thread + 1.0-SNAPSHOT + + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.10.0 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + \ No newline at end of file diff --git a/src/main/java/cn/wudashan/Main.java b/src/main/java/cn/wudashan/Main.java new file mode 100644 index 0000000..3c00abf --- /dev/null +++ b/src/main/java/cn/wudashan/Main.java @@ -0,0 +1,39 @@ +package cn.wudashan; + +import cn.wudashan.executor.BaseThreadPoolExecutor; +import cn.wudashan.util.MDCUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.UUID; + +/** + * 主函数 + * @author wudashan + */ +public class Main { + + private static final String KEY = "requestId"; + + private static final Logger logger = LoggerFactory.getLogger(Main.class); + + public static void main(String[] args) { + + MDCUtil.put(KEY, UUID.randomUUID().toString()); + + logger.debug("log in main thread"); + + BaseThreadPoolExecutor executor = new BaseThreadPoolExecutor(); + executor.execute(new Runnable() { + @Override + public void run() { + logger.debug("log in base thread pool"); + } + }); + executor.shutdown(); + + MDCUtil.remove(KEY); + + } + +} diff --git a/src/main/java/cn/wudashan/executor/BaseRunnable.java b/src/main/java/cn/wudashan/executor/BaseRunnable.java new file mode 100644 index 0000000..3bf0ffd --- /dev/null +++ b/src/main/java/cn/wudashan/executor/BaseRunnable.java @@ -0,0 +1,35 @@ +package cn.wudashan.executor; + +import cn.wudashan.util.MDCUtil; + +import java.util.Map; + +/** + * 基本任务类 + * @author wudashan + */ +public class BaseRunnable implements Runnable { + + private final Runnable runnable; + + private final Map map; + + public BaseRunnable(Runnable runnable) { + this.runnable = runnable; + this.map = MDCUtil.getCopyOfContextMap(); + } + + @Override + public void run() { + for (Map.Entry entry : map.entrySet()) { + MDCUtil.put(entry.getKey(), entry.getValue()); + } + runnable.run(); + for (Map.Entry entry : map.entrySet()) { + MDCUtil.remove(entry.getKey()); + } + + } + + +} diff --git a/src/main/java/cn/wudashan/executor/BaseThreadPoolExecutor.java b/src/main/java/cn/wudashan/executor/BaseThreadPoolExecutor.java new file mode 100644 index 0000000..4d607a0 --- /dev/null +++ b/src/main/java/cn/wudashan/executor/BaseThreadPoolExecutor.java @@ -0,0 +1,31 @@ +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(); + } + + + + +} diff --git a/src/main/java/cn/wudashan/util/MDCUtil.java b/src/main/java/cn/wudashan/util/MDCUtil.java new file mode 100644 index 0000000..078defb --- /dev/null +++ b/src/main/java/cn/wudashan/util/MDCUtil.java @@ -0,0 +1,39 @@ +package cn.wudashan.util; + +import org.slf4j.MDC; + +import java.util.Collections; +import java.util.Map; + +/** + * MDC工具类,屏蔽异常和空指针 + * @author wudashan + */ +public class MDCUtil { + + public static Map getCopyOfContextMap() { + Map result; + try { + result = MDC.getCopyOfContextMap(); + } catch (RuntimeException ignored) { + result = Collections.emptyMap(); + } + if (result == null) { + result = Collections.emptyMap(); + } + return result; + } + + public static void put(String key, String value) { + try { + MDC.put(key, value); + } catch (RuntimeException ignored) {} + } + + public static void remove(String key) { + try { + MDC.remove(key); + } catch (RuntimeException ignored) {} + } + +} diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..4285ede --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file