ThreadLocal使用的坑

问题分析:

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

public class JSONPRequestResponseBodyMethodProcessor extends RequestResponseBodyMethodProcessor {

    public final static ThreadLocal JSONP_WEB_REQUEST_THREAD_LOCAL = new ThreadLocal();

    public JSONPRequestResponseBodyMethodProcessor(List> messageConverters) {
        super(messageConverters);
    }

    public JSONPRequestResponseBodyMethodProcessor(List> messageConverters, ContentNegotiationManager contentNegotiationManager) {
        super(messageConverters, contentNegotiationManager);
    }

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return returnType.getMethodAnnotation(ResponseJSONPBody.class) != null;
    }

    @Override
    public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException {
        ServletServerHttpRequest inputMessage = this.createInputMessage(webRequest);
        HttpServletRequest request = inputMessage.getServletRequest();
        String callback = request.getParameter("callback");
        if (StringUtils.isNotBlank(callback) && returnValue != null && !(returnValue instanceof JSONPObject)) {
            returnValue = new JSONPObject(callback.replaceAll(">","").replaceAll("<",""), returnValue);
        }
        JSONP_WEB_REQUEST_THREAD_LOCAL.set(returnValue); //返回值设置到线程变量中
        super.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    }
}

原因:在DispatcherServlet的自定义类中需要使用到JSONP_WEB_REQUEST_THREAD_LOCAL这个线程里的变量副本,JSONPRequestResponseBodyMethodProcessor.JSONP_WEB_REQUEST_THREAD_LOCAL.get()得到每个Action请求返回的结果值;正常请求情况下,上面的RequestResponseBodyMethodProcessor会被spring执行,但是非正常请求,eg:入参对象需要Integer型的变量,请求传入的是String,会被spring拦截器拦截掉,数据绑定异常会被内部try…catch…至此不会执行RequestResponseBodyMethodProcessor这个子类;在并发情况下线程被重复使用JSONP_WEB_REQUEST_THREAD_LOCAL这个值里的变量副本还是上一次线程里的set值
处理方案:在JSONP_WEB_REQUEST_THREAD_LOCAL.get()后需要remove();

备注:并发是发生在栈里的,堆里面的东西不存在并发

https://www.cnblogs.com/yxysuanfa/p/7125761.html

你可能感兴趣的:(java,线程)