当前位置:网站首页>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;
}
边栏推荐
- 机械臂速成小指南(七):机械臂位姿的描述方法
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- Larvel modify table fields
- On data preprocessing in sklearn
- CDA数据分析——Excel数据处理的常见知识点归纳
- 二分刷题记录(洛谷题单)区间的甄别
- mysql数据库基础
- (C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
- Log4j2
- How to Easily Create Barplots with Error Bars in R
猜你喜欢
PyTorch搭建LSTM实现服装分类(FashionMNIST)
HR wonderful dividing line
Larvel modify table fields
Applet link generation
From scratch, develop a web office suite (3): mouse events
Deep understanding of NN in pytorch Embedding
PyTorch nn.RNN 参数全解析
Natural language processing series (II) -- building character level language model using RNN
Mysql database foundation
倍增 LCA(最近公共祖先)
随机推荐
Yygh-10-wechat payment
post请求体内容无法重复获取
Jenkins用户权限管理
高德地图测试用例
How to Create a Beautiful Plots in R with Summary Statistics Labels
使用Sqoop把ADS层数据导出到MySQL
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
Log4j2
Orb-slam2 data sharing and transmission between different threads
CDH6之Sqoop添加数据库驱动
mysql索引和事务
Leetcode122 the best time to buy and sell stocks II
b格高且好看的代码片段分享图片生成
How to Visualize Missing Data in R using a Heatmap
CONDA common command summary
Uniapp uni list item @click, uniapp uni list item jump with parameters
How to Easily Create Barplots with Error Bars in R
YYGH-BUG-05
Fabric. JS 3 APIs to set canvas width and height
PyTorch nn. Full analysis of RNN parameters