多线程之--主线程结束,子线程无法获取主线程请求头信息 解决方案

当在主线程中,使用子线程时.

当主线程未结束时, 子线程是可以获取到主线程的request中的信息的.

当主线程结束时,子线程就无法获取主线程request中的信息了

ThreadLocal:线程隔离,子线程无法获取主线的中的数据
InheritableThreadLocal:子线程可以获取主线程中的数据,但当主线程结束后,子线程就获取不到了
TransmittableThreadLocal(alibaba):InheritableThreadLocal的子类,可以解决当主线程结束后,子线程获取主线程数据的问题

代码实现


    com.alibaba
    transmittable-thread-local
    2.11.4

import com.alibaba.ttl.TransmittableThreadLocal;

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

/**
 * 使用alibaba中的 可传输ThreadLocal,可保证主线程结束后,子线程扔可获取主线程中的数据
 */
public class RequestHeaderHolder {
//    private static final ThreadLocal> COPY_ON_INHERIT_THREAD_LOCAL = new InheritableThreadLocal<>();
    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();
    }
}

package com.biz.service.interceptors;

import com.haier.hopm.biz.service.utils.RequestHeaderHolder;
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();
    }
}

package com.biz.service.config;

import com.haier.hopm.biz.service.interceptors.RequestHandlerInterceptor;
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("/**");
    }
}

你可能感兴趣的:(java,java,开发语言)