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

cn.ishow.starter.rpc.context.RpcContext Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package cn.ishow.starter.rpc.context;


import cn.ishow.starter.rpc.annotation.Decode;
import cn.ishow.starter.rpc.properties.RtDecode;
import feign.Request;
import lombok.extern.slf4j.Slf4j;

/**
 * feign上下文,存放和超时配置和解码器相关数据
 *
 * @author chongyin
 * @create 2022/5/10 下午6:03
 */
@Slf4j
public class RpcContext {
    /**
     * 配置相关ThreadLocal变量
     */
    private final static ThreadLocal CONFIG_CONTEXT = new ThreadLocal<>();

    public RpcContext() {
    }

    /**
     * 完成自定义对象初始化
     */
    public void preProcess() {
        Config properties = new Config();
        CONFIG_CONTEXT.set(properties);
    }

    /**
     * 添加HTTP超时配置
     *
     * @param options
     */
    public void addRequestOptions(Request.Options options) {
        CONFIG_CONTEXT.get().options = options;
    }

    /**
     * 获取远程调用响应解码器
     *
     * @return
     */
    public RtDecode getRtDecoder() {
        return CONFIG_CONTEXT.get() != null ? CONFIG_CONTEXT.get().decode : null;
    }

    /**
     * 获取HTTP相关超时配置
     *
     * @return
     */
    public Request.Options getRequestOptions() {
        return CONFIG_CONTEXT.get() != null ? CONFIG_CONTEXT.get().options : null;
    }

    /**
     * 清空相关自定义配置
     */
    public void postProcess() {
        CONFIG_CONTEXT.remove();
    }

    public void addRtDecoder(Decode decode) {
        CONFIG_CONTEXT.get().decode = RtDecode.parse(decode);
    }


    private static class Config {
        private Request.Options options;
        private RtDecode decode;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy