org.macrocloud.kernel.context.RunnableWrapper Maven / Gradle / Ivy
package org.macrocloud.kernel.context;
import org.slf4j.MDC;
import org.macrocloud.kernel.toolkit.utils.ThreadLocalUtil;
import org.springframework.lang.Nullable;
import java.util.Map;
/**
* 多线程中传递 context 和 mdc
*
* @author macro
*/
public class RunnableWrapper implements Runnable {
private final Runnable delegate;
private final Map tlMap;
/**
* logback 下有可能为 null
*/
@Nullable
private final Map mdcMap;
public RunnableWrapper(Runnable runnable) {
this.delegate = runnable;
this.tlMap = ThreadLocalUtil.getAll();
this.mdcMap = MDC.getCopyOfContextMap();
}
@Override
public void run() {
if (!tlMap.isEmpty()) {
ThreadLocalUtil.put(tlMap);
}
if (mdcMap != null && !mdcMap.isEmpty()) {
MDC.setContextMap(mdcMap);
}
try {
delegate.run();
} finally {
tlMap.clear();
if (mdcMap != null) {
mdcMap.clear();
}
ThreadLocalUtil.clear();
MDC.clear();
}
}
}