当前位置:网站首页>子线程获取Request
子线程获取Request
2022-07-02 09:42:00 【徘徊在深夜中的猫】
子线程获取Request
原文链接 https://zhhll.icu/2020/javaweb/问题/7.子线程获取Request/
有时候在进行业务处理时对于一些对于业务不那么重要且对于返回结果无关的情况会开启一个新的线程进行处理,但是在开启新线程进行处理时发现无法从RequestContextHolder中获取到当前的请求,取出来是null
这是因为RequestContextHolder中的信息都是存储在ThreadLocal中的,而ThreadLocal中的数据是使用线程进行查找的,不是该线程存储的,是无法查找到的
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes");
private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
new NamedInheritableThreadLocal<RequestAttributes>("Request context");
但是有时候子线程就是需要获取到当前请求怎么办呢?
此时就需要将RequestAttributes对象设置为子线程共享的,在开启子线程之前
// 主线程先获取到请求信息
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// 设置子线程共享
RequestContextHolder.setRequestAttributes(requestAttributes,true);
这是什么原理?
public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) {
if (attributes == null) {
resetRequestAttributes();
}
else {
if (inheritable) {
// 如果为true,则将信息存储在inheritableRequestAttributesHolder中
inheritableRequestAttributesHolder.set(attributes);
requestAttributesHolder.remove();
}
else {
requestAttributesHolder.set(attributes);
inheritableRequestAttributesHolder.remove();
}
}
}
可以看到NamedInheritableThreadLocal重写了getMap方法
ThreadLocalMap getMap(Thread t) {
return t.inheritableThreadLocals;
}
边栏推荐
- K-Means Clustering Visualization in R: Step By Step Guide
- 基于Arduino和ESP8266的连接手机热点实验(成功)
- The position of the first underline selected by the vant tabs component is abnormal
- Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)
- PHP query distance according to longitude and latitude
- How does Premiere (PR) import the preset mogrt template?
- Natural language processing series (III) -- LSTM
- Log4j2
- 史上最易懂的f-string教程,收藏这一篇就够了
- PyTorch nn.RNN 参数全解析
猜你喜欢

Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law

How to Add P-Values onto Horizontal GGPLOTS

Dynamic memory (advanced 4)

GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT

How to Easily Create Barplots with Error Bars in R

Esp32 audio frame esp-adf add key peripheral process code tracking

How to Visualize Missing Data in R using a Heatmap

基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R

MSI announced that its motherboard products will cancel all paper accessories
随机推荐
A sharp tool for exposing data inconsistencies -- a real-time verification system
Natural language processing series (III) -- LSTM
The selected cells in Excel form have the selection effect of cross shading
Yygh-9-make an appointment to place an order
jenkins 凭证管理
What week is a date obtained by QT
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
YYGH-9-预约下单
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
How to Easily Create Barplots with Error Bars in R
Fabric. JS 3 APIs to set canvas width and height
Tiktok overseas tiktok: finalizing the final data security agreement with Biden government
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
Leetcode14 最长公共前缀
Cmake cross compilation
【2022 ACTF-wp】
PyTorch nn.RNN 参数全解析
HR wonderful dividing line
Deep understanding of NN in pytorch Embedding