当前位置:网站首页>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;
}
边栏推荐
- Test shift left and right
- B high and beautiful code snippet sharing image generation
- drools动态增加、修改、删除规则
- Those logs in MySQL
- Mish-撼动深度学习ReLU激活函数的新继任者
- Input a three digit number and output its single digit, ten digit and hundred digit.
- (C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
- [QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
- YYGH-9-预约下单
- drools执行String规则或执行某个规则文件
猜你喜欢
Dynamic memory (advanced 4)
Pytorch builds LSTM to realize clothing classification (fashionmnist)
Yygh-9-make an appointment to place an order
CDH6之Sqoop添加数据库驱动
Addition, deletion, modification and query of MySQL table (Advanced)
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
记录一下MySql update会锁定哪些范围的数据
自然语言处理系列(一)——RNN基础
使用Sqoop把ADS层数据导出到MySQL
随机推荐
【C语言】杨辉三角,自定义三角的行数
Addition, deletion, modification and query of MySQL table (Advanced)
This article takes you to understand the operation of vim
On data preprocessing in sklearn
ThreadLocal的简单理解
lombok常用注解
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
PyTorch搭建LSTM实现服装分类(FashionMNIST)
还不会安装WSL 2?看这一篇文章就够了
小程序链接生成
Time format display
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
drools执行String规则或执行某个规则文件
Filtre de profondeur de la série svo2
Differences between nodes and sharding in ES cluster
[C language] Yang Hui triangle, customize the number of lines of the triangle
How to Add P-Values onto Horizontal GGPLOTS
SCM power supply
Dynamic memory (advanced 4)
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic