异步线程RequestContextHolder.getRequestAttributes()为null

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

posted @ 2020-04-22 19:05  明天,你好啊  阅读(10028)  评论(2编辑  收藏  举报