使用多线程时,传递 request 对象丢失


1.原因描述

我们在工作中遇到耗时的一些操作时我们会使用多线程或者mq来解决以便提高程序的响应速度。但是使用多线程时遇到一个问题,我单独开一个线程去进行其他逻辑处理时,在发送消息之前(未开启多线程时)我们是可以获取到 request 信息的,但是在新开的线程中确是无法获取到 request 信息(request is  null)。

2.代码演示

主线程代码

 

 

 

子线程代码

 

 

 3.错误描述

 由上图可知子线程无法获取到 request 信息,直接报了 java.lang.NullPointerException。

原因:request 信息是存储在 ThreadLocal 中的,所以子线程根本无法获取到主线程的  request 信息。

/**
* 返回当前线程上下文request信息
*
* @return
*/
public static HttpServletRequest getCurrentRequestInfo() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return servletRequestAttributes.getRequest();
}

源码如下:

private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes");
/**
* Return the RequestAttributes currently bound to the thread.
* @return the RequestAttributes currently bound to the thread,
* or {@code null} if none bound
*/
public static RequestAttributes getRequestAttributes() {
RequestAttributes attributes = requestAttributesHolder.get();
if (attributes == null) {
attributes = inheritableRequestAttributesHolder.get();
}
return attributes;
}

4.解决方案

在新开子线程之前 新增二行代码,如下,

// 将RequestAttributes对象设置为子线程共享
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(sra, true);

子线程代码

 

子线程代码

 

 到此问题完美解决,如果想要深入了解原因  需要看源码。

 

获取当前线程上下文信息方法地址:https://www.cnblogs.com/ming-blogs/p/12754837.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM