当前位置:网站首页>子线程获取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;
}
边栏推荐
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- Summary of flutter problems
- Implementation of address book (file version)
- SVO2系列之深度滤波DepthFilter
- GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
- [multithreading] the main thread waits for the sub thread to finish executing, and records the way to execute and obtain the execution result (with annotated code and no pit)
- PHP query distance according to longitude and latitude
- Dynamic debugging of multi file program x32dbg
- Develop scalable contracts based on hardhat and openzeppelin (I)
- Cmake cross compilation
猜你喜欢

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

A sharp tool for exposing data inconsistencies -- a real-time verification system

Some problems encountered in introducing lvgl into esp32 Arduino

How does Premiere (PR) import the preset mogrt template?

R HISTOGRAM EXAMPLE QUICK REFERENCE

PyTorch nn.RNN 参数全解析

HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R

Filtre de profondeur de la série svo2

YYGH-BUG-04

File operation (detailed!)
随机推荐
GGPlot Examples Best Reference
【2022 ACTF-wp】
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
MSI announced that its motherboard products will cancel all paper accessories
【C语言】杨辉三角,自定义三角的行数
Fabric. JS 3 APIs to set canvas width and height
How to Create a Beautiful Plots in R with Summary Statistics Labels
Power Spectral Density Estimates Using FFT---MATLAB
行业的分析
PHP query distance according to longitude and latitude
Depth filter of SvO2 series
Log4j2
b格高且好看的代码片段分享图片生成
How to Easily Create Barplots with Error Bars in R
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
ES集群中节点与分片的区别
深入理解P-R曲线、ROC与AUC
Develop scalable contracts based on hardhat and openzeppelin (I)
YYGH-BUG-04
进入前六!博云在中国云管理软件市场销量排行持续上升