dev.vality.woody.api.flow.concurrent.WCallable Maven / Gradle / Ivy
package dev.vality.woody.api.flow.concurrent;
import dev.vality.woody.api.MDCUtils;
import dev.vality.woody.api.trace.TraceData;
import dev.vality.woody.api.trace.context.TraceContext;
import java.util.concurrent.Callable;
public class WCallable implements Callable {
private final TraceData traceData;
private final Callable wrappedCallable;
public Callable getWrappedCallable() {
return wrappedCallable;
}
public TraceData getTraceData() {
return traceData;
}
public WCallable(Callable wrappedCallable, TraceData traceData) {
if (wrappedCallable == null || traceData == null) {
throw new NullPointerException("Null arguments're not allowed");
}
this.traceData = traceData;
this.wrappedCallable = wrappedCallable;
}
@Override
public T call() throws Exception {
TraceData originalTraceData = TraceContext.getCurrentTraceData();
TraceContext.setCurrentTraceData(getTraceData().cloneObject());
if (traceData != originalTraceData) {
MDCUtils.putSpanData(traceData.getActiveSpan().getSpan());
}
try {
return getWrappedCallable().call();
} finally {
TraceContext.setCurrentTraceData(originalTraceData);
MDCUtils.putSpanData(originalTraceData.getActiveSpan().getSpan());
}
}
}