当前位置:网站首页>子线程获取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;
}
边栏推荐
- (C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
- 行业的分析
- ESP32 Arduino 引入LVGL 碰到的一些问题
- GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
- to_ Bytes and from_ Bytes simple example
- GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
- Pytorch builds LSTM to realize clothing classification (fashionmnist)
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- How to Create a Nice Box and Whisker Plot in R
- Fabric.js 3个api设置画布宽高
猜你喜欢

SVO2系列之深度濾波DepthFilter

ESP32 Arduino 引入LVGL 碰到的一些问题

K-Means Clustering Visualization in R: Step By Step Guide

Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)

GGPlot Examples Best Reference

Dynamic debugging of multi file program x32dbg

HOW TO ADD P-VALUES TO GGPLOT FACETS

The selected cells in Excel form have the selection effect of cross shading

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
![[geek challenge 2019] upload](/img/04/731323142161a4994c14fedae38b81.jpg)
[geek challenge 2019] upload
随机推荐
CMake交叉编译
B high and beautiful code snippet sharing image generation
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
Log4j2
How to Create a Nice Box and Whisker Plot in R
HR wonderful dividing line
[untitled] how to mount a hard disk in armbian
YYGH-9-预约下单
Larvel modify table fields
基于Arduino和ESP8266的连接手机热点实验(成功)
From scratch, develop a web office suite (3): mouse events
File operation (detailed!)
Orb-slam2 data sharing and transmission between different threads
CONDA common command summary
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
C # method of obtaining a unique identification number (ID) based on the current time
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
How does Premiere (PR) import the preset mogrt template?
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R