当前位置:网站首页>子线程获取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;
}
边栏推荐
- 【C语言】杨辉三角,自定义三角的行数
- QT获取某个日期是第几周
- 行業的分析
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- to_bytes与from_bytes简单示例
- Dynamic debugging of multi file program x32dbg
- GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
- K-Means Clustering Visualization in R: Step By Step Guide
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
- 自然语言处理系列(一)——RNN基础
猜你喜欢
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
ES集群中节点与分片的区别
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)
Some problems encountered in introducing lvgl into esp32 Arduino
Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
深入理解P-R曲线、ROC与AUC
随机推荐
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
easyExcel和lombok注解以及swagger常用注解
Implementation of address book (file version)
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
PHP query distance according to longitude and latitude
小程序链接生成
php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
Filtre de profondeur de la série svo2
B high and beautiful code snippet sharing image generation
Pytorch builds LSTM to realize clothing classification (fashionmnist)
K-Means Clustering Visualization in R: Step By Step Guide
The computer screen is black for no reason, and the brightness cannot be adjusted.
Natural language processing series (II) -- building character level language model using RNN
SCM power supply
R HISTOGRAM EXAMPLE QUICK REFERENCE
Flesh-dect (media 2021) -- a viewpoint of material decomposition
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
Tiktok overseas tiktok: finalizing the final data security agreement with Biden government
Codeforces 771 div2 B (no one FST, refers to himself)