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

io.github.hpsocket.soa.starter.skywalking.async.TracingCallableWrapper Maven / Gradle / Ivy

package io.github.hpsocket.soa.starter.skywalking.async;

import java.util.concurrent.Callable;

import org.apache.skywalking.apm.toolkit.trace.CallableWrapper;
import org.apache.skywalking.apm.toolkit.trace.TraceCrossThread;

import io.github.hpsocket.soa.framework.core.mdc.MdcAttr;

/** {@linkplain Callable} 包装类(注入 {@linkplain org.slf4j.MDC MDC} 和 traceId 调用链跟踪信息) */
@TraceCrossThread
public class TracingCallableWrapper extends CallableWrapper
{
    private final MdcAttr mdcAttr;
    
    public TracingCallableWrapper(Callable c)
    {
        this(c, MdcAttr.fromMdc());
    }
    
     public TracingCallableWrapper(Callable c, MdcAttr mdcAttr)
    {
        super(c);
        
        this.mdcAttr = mdcAttr;
    }
    
   @Override
    public V call() throws Exception
    {
        try
        {
            mdcAttr.putMdc();
            
            return super.call();
        }
        finally
        {
            mdcAttr.removeMdc();
        }
    }

    public static  TracingCallableWrapper of(Callable r)
    {
        return new TracingCallableWrapper<>(r);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy