diff --git a/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCCallable.java b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCCallable.java new file mode 100644 index 0000000000000000000000000000000000000000..b44949067a7b378c3fbf8cd58df0e3b75eca872b --- /dev/null +++ b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCCallable.java @@ -0,0 +1,33 @@ +package org.hongxi.whatsmars.common.mdc; + +import org.slf4j.MDC; + +import java.util.Map; +import java.util.concurrent.Callable; + +/** + * Created by shenhongxi on 2020/8/16. + */ +public class MDCCallable implements Callable { + + private final Callable callable; + + private transient final Map _cm = MDC.getCopyOfContextMap(); + + public MDCCallable(Callable callable) { + this.callable = callable; + } + + @Override + public V call() throws Exception { + if (_cm != null) { + MDC.setContextMap(_cm); + } + try { + return callable.call(); + } finally { + MDC.clear(); + } + } + +} \ No newline at end of file diff --git a/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCRunnable.java b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCRunnable.java new file mode 100644 index 0000000000000000000000000000000000000000..891db1738b81fc0a2ad68a678f8326c1e87d2fa8 --- /dev/null +++ b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCRunnable.java @@ -0,0 +1,32 @@ +package org.hongxi.whatsmars.common.mdc; + +import org.slf4j.MDC; + +import java.util.Map; + +/** + * Created by shenhongxi on 2020/8/16. + */ +public class MDCRunnable implements Runnable { + + private final Runnable runnable; + + private transient final Map _cm = MDC.getCopyOfContextMap(); + + public MDCRunnable(Runnable runnable) { + this.runnable = runnable; + } + + @Override + public void run() { + if (_cm != null) { + MDC.setContextMap(_cm); + } + try { + runnable.run(); + } finally { + MDC.clear(); + } + } + +} \ No newline at end of file diff --git a/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCSupplier.java b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCSupplier.java new file mode 100644 index 0000000000000000000000000000000000000000..9f9f04e711ae9ab8b70944dab024ca3fe9d08f42 --- /dev/null +++ b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCSupplier.java @@ -0,0 +1,33 @@ +package org.hongxi.whatsmars.common.mdc; + +import org.slf4j.MDC; + +import java.util.Map; +import java.util.function.Supplier; + +/** + * Created by shenhongxi on 2020/8/16. + */ +public class MDCSupplier implements Supplier { + + private transient final Map _cm = MDC.getCopyOfContextMap(); + + private final Supplier supplier; + + public MDCSupplier(Supplier supplier) { + this.supplier = supplier; + } + + @Override + public T get() { + if (_cm != null) { + MDC.setContextMap(_cm); + } + try { + return supplier.get(); + } finally { + MDC.clear(); + } + } + +} \ No newline at end of file diff --git a/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCTool.java b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCTool.java new file mode 100644 index 0000000000000000000000000000000000000000..6d142ff75a80520467b77ec5e3fdecf358560a58 --- /dev/null +++ b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/mdc/MDCTool.java @@ -0,0 +1,35 @@ +package org.hongxi.whatsmars.common.mdc; + +import java.util.concurrent.Callable; +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + * Created by shenhongxi on 2020/8/16. + */ +public final class MDCTool { + + public static MDCSupplier supplier(Supplier supplier) { + return new MDCSupplier<>(supplier); + } + + + public static MDCRunnable runnable(Runnable runnable) { + return new MDCRunnable(runnable); + } + + public static MDCRunnable runnable(Consumer consumer) { + return new MDCRunnable(() -> { + consumer.accept(null); + }); + } + + public static MDCCallable callable(Callable callable) { + return new MDCCallable<>(callable); + } + + public static MDCCallable callable(Supplier supplier) { + return new MDCCallable<>(() -> supplier.get()); + } + +} \ No newline at end of file