All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.vality.woody.api.flow.concurrent.WCallable Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
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());
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy