当前位置:网站首页>子线程获取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;
}
边栏推荐
- php 根据经纬度查询距离
- 输入一个三位的数字,输出它的个位数,十位数、百位数。
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- Codeforces 771-div2 C (trouble, permutation is not very good)
- PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- Summary of flutter problems
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
- YYGH-BUG-05
- Data analysis - Matplotlib sample code
猜你喜欢
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
ESP32 Arduino 引入LVGL 碰到的一些问题
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Cluster Analysis in R Simplified and Enhanced
【2022 ACTF-wp】
还不会安装WSL 2?看这一篇文章就够了
自然语言处理系列(一)——RNN基础
HR wonderful dividing line
Mish-撼动深度学习ReLU激活函数的新继任者
Esp32 audio frame esp-adf add key peripheral process code tracking
随机推荐
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)
【C语言】杨辉三角,自定义三角的行数
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
How to Create a Beautiful Plots in R with Summary Statistics Labels
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
多文件程序X32dbg动态调试
Dynamic debugging of multi file program x32dbg
[geek challenge 2019] upload
Cluster Analysis in R Simplified and Enhanced
Log4j2
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
PX4 Position_ Control RC_ Remoter import
6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
自然语言处理系列(一)——RNN基础
Power Spectral Density Estimates Using FFT---MATLAB
数据分析 - matplotlib示例代码
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
The selected cells in Excel form have the selection effect of cross shading
Read the Flink source code and join Alibaba cloud Flink group..