子线程获取主线程header值(token)

概念

ThreadLocal主要用于在各个线程中保存各自对象的值 , 互不相干.

InheritableThreadLocal可以子线程中访问到父线程中的值.

问题

接口调用,发送待办改为异步执行,所以改为线程调用,因为发送待办需要获取当前登录人的账号,发现子线程无法获取账号。

解决

  1. 开始时在子线程重新set attribute

RequestContextHolder.setRequestAttributes(attributes,true);

发现子线程循环中有时候不能获取header值,后来发现是因为主线程已经结束,而依赖attribute的引用已经为null,即使inheritable为true也不行,因为主线程的是值是ThreadLocal,不是 Inheritable ThreadLocal。

解决 使用拦截器set InheritableThreadLocal 值,因为使用加强版的InheritableThreadLocal需要引入jar


    com.alibaba
    transmittable-thread-local
    2.11.4
1.1 InheritableThreadLocal类
import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.HashMap;
import java.util.Map;

public class RequestHeaderHolder {
    public static TransmittableThreadLocal> COPY_ON_INHERIT_THREAD_LOCAL = new TransmittableThreadLocal>() {
        @Override
        protected Map initialValue() {
            return new HashMap<>();
        }
    };
    public static Map get() {
        return COPY_ON_INHERIT_THREAD_LOCAL.get();
    }

    public static String get(String key) {
        if (key != null) {
            key = key.toLowerCase();
        }
        return COPY_ON_INHERIT_THREAD_LOCAL.get().get(key);
    }

    public static void set(String key, String value) {
        COPY_ON_INHERIT_THREAD_LOCAL.get().put(key, value);
    }
    public static void remove() {
        COPY_ON_INHERIT_THREAD_LOCAL.remove();
    }
}
1.2 拦截器
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

public class RequestHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Enumeration headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            RequestHeaderHolder.set(name,request.getHeader(name));
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        RequestHeaderHolder.remove();
    }
}
1.3 配置拦截器
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestHandlerInterceptor()).addPathPatterns("/**");
    }
}

这样的话子线程可以直接获取header值

RequestHeaderHolder.get(CommonConstant.HEADER_AUTH);

注意 headerName取值为小写,所以RequestHeaderHolder.get把key转化为小写了。RequestHeaderHolder。这样子线程就可以获取父线程的值了,不限于header值。

你可能感兴趣的:(spring)