当前位置:网站首页>Sub thread get request
Sub thread get request
2022-07-02 12:09:00 【A cat wandering in the middle of the night】
The child thread gets Request
Link to the original text https://zhhll.icu/2020/javaweb/ problem /7. The child thread gets Request/
Sometimes during business processing, a new thread will be opened to handle situations that are not so important to the business and irrelevant to the returned results , However, when starting a new thread for processing, it is found that it cannot be accessed from RequestContextHolder Get the current request , Take it out is null
This is because RequestContextHolder The information in is stored in ThreadLocal Medium , and ThreadLocal The data in is searched by threads , Not stored by this thread , It's impossible to find
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes");
private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
new NamedInheritableThreadLocal<RequestAttributes>("Request context");
But sometimes the sub thread just needs to get the current request ?
This is where you're going to have to put RequestAttributes Object is set to be shared by child threads , Before starting the child thread
// The main thread gets the request information first
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// Set child thread sharing
RequestContextHolder.setRequestAttributes(requestAttributes,true);
What is the principle ?
public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) {
if (attributes == null) {
resetRequestAttributes();
}
else {
if (inheritable) {
// If true, Then store the information in inheritableRequestAttributesHolder in
inheritableRequestAttributesHolder.set(attributes);
requestAttributesHolder.remove();
}
else {
requestAttributesHolder.set(attributes);
inheritableRequestAttributesHolder.remove();
}
}
}
You can see NamedInheritableThreadLocal Rewrote getMap Method
ThreadLocalMap getMap(Thread t) {
return t.inheritableThreadLocals;
}
边栏推荐
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- 自然语言处理系列(二)——使用RNN搭建字符级语言模型
- (C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 全链路压测
- 浅谈sklearn中的数据预处理
- 5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- 深入理解P-R曲线、ROC与AUC
- Jenkins user rights management
- Maximum profit of jz63 shares
猜你喜欢

Dynamic memory (advanced 4)

还不会安装WSL 2?看这一篇文章就够了

二分刷题记录(洛谷题单)区间的甄别

【2022 ACTF-wp】

自然语言处理系列(二)——使用RNN搭建字符级语言模型

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R

From scratch, develop a web office suite (3): mouse events

conda常用命令汇总

Addition, deletion, modification and query of MySQL table (Advanced)
随机推荐
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
Leetcode122 the best time to buy and sell stocks II
CDH6之Sqoop添加数据库驱动
How to Visualize Missing Data in R using a Heatmap
mysql表的增删改查(进阶)
Jenkins用户权限管理
Repeat, tile and repeat in pytorch_ The difference between interleave
Easyexcel and Lombok annotations and commonly used swagger annotations
CONDA common command summary
Dynamic debugging of multi file program x32dbg
刷题---二叉树--2
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
drools执行String规则或执行某个规则文件
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
How to Add P-Values onto Horizontal GGPLOTS
【2022 ACTF-wp】
自然语言处理系列(二)——使用RNN搭建字符级语言模型
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT