com.arextest.schedule.mdc.AbstractTracedRunnable Maven / Gradle / Ivy
package com.arextest.schedule.mdc;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.MDC;
/**
* @author jmo
* @since 2021/11/5
*/
@Deprecated
@Slf4j
public abstract class AbstractTracedRunnable implements Runnable {
private final Map traceMap;
public AbstractTracedRunnable() {
this.traceMap = MDC.getCopyOfContextMap();
}
@Override
public final void run() {
Map old = mark();
try {
this.doWithTracedRunning();
} catch (Throwable ex) {
LOGGER.error(ex.getMessage(), ex);
} finally {
removeMark(old);
}
}
protected abstract void doWithTracedRunning();
private Map mark() {
if (MapUtils.isEmpty(this.traceMap)) {
return null;
} else {
Map old = MDC.getCopyOfContextMap();
MDC.setContextMap(this.traceMap);
return old;
}
}
private void removeMark(Map prev) {
if (MapUtils.isEmpty(prev)) {
MDC.clear();
} else {
MDC.setContextMap(prev);
}
}
}