当前位置:网站首页>Record context information through ThreadLocal (record user information to realize global operation)
Record context information through ThreadLocal (record user information to realize global operation)
2022-06-21 07:24:00 【In a flash】
ThreadLocal A copy of the variable is provided for each thread , And variables are valid throughout the life cycle of the thread , It forms the isolation between threads , Only the same thread can manipulate variables , It's a kind of ” Space for time ” In the form of , Can be used to record some context data .
ThreadLocal Through internal Map To store copies of variables for each thread ,map Of key Namely threadLocal,value It's us set The value of , Every time a thread is in get When , All take values from their own variables , So it must be 不 There are thread safety issues .
Use ThreadLocal after , Be sure to pay attention to manual remove() Otherwise, it will cause OOM abnormal .
Scenario as follows :
- Record each request for user information
ThreadLocal Code :
public class RequestHolder {
private static final ThreadLocal<SysUser> userHolder = new ThreadLocal<SysUser>();
public static void setUser(SysUser sysUser) {
userHolder.set(sysUser);
}
public static SysUser getUser() {
return userHolder.get();
}
public static void remove() {
userHolder.remove();
}
}
Application process :
The following process sections use Pseudo code Express
1、 Interceptors get user information
2、 It was recorded that ThreadLocal in
3、 Use through get() Method gets value
// 1、 Interceptors get user information
// 2、 It was recorded that ThreadLocal in
@Component
public class AuthenticationHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.debug(" Enter the interceptor ,URL:{}", request.getServletPath());
// Add user global information
RequestHolder.setUser(userInfo);
return true;
}
....
....
}
// 3、 Use through get() Method gets value
@RestController
@RequestMapping
public class Controller {
.....
@GetMapping("/test")
public String test() {
// from ThreadLocal get data
RequestHolder.getUserId();
return " Successful visit ";
}
.....
}
边栏推荐
- Wechat applet_ 5. Page configuration
- Google Earth Engine(GEE)——美国本土岩性数据集
- CUDA or FPGA for special purpose 3D graphics computations? [closed]
- Introduction to exceptions
- flutter jpush
- win10上vs2017配置Eigen3开发环境
- Open up the connection between production and marketing and build a new digital supply chain of agricultural and sideline products
- IDM mobile terminal function upgrade description
- Japanese programming learning website
- Wechat applet_ 6. Network data request
猜你喜欢

如何利用MES管理系统实现防错和预警

Using XAML only to realize the effect of ground glass background panel

Ztmao theme cat WordPress Theme classic lost version /wp website template download station source code + global SEO function setting

Mathematics is a tool for solving problems

Postman publishing API documentation

Unittest use

Wechat applet_ 5. Page configuration

【毕业季-进击的技术er】:即将大四在校生的技术分享,未来共勉

flutter jpush

数学是用于解决问题的工具
随机推荐
BOL波尔独创双币驱动模型 引领DeFi+NFT+Web3.0新热点
Two column set (map set)
Introduction to exceptions
Analyse des données: indicateurs communs pour différentes industries
微信小程序_5,页面配置
Type de contrôle qml: Drawer
C # basic knowledge series 8 (const and readonly keywords)
[mapbox] 基础
Yield Guild Games 与 Discord 上的第一款 MMORPG ——Tatsumeeko 达成合作
Hisilicon series mass production hardware commissioning record
Using XAML only to realize the effect of ground glass background panel
【毕业季-进击的技术er】:即将大四在校生的技术分享,未来共勉
Random random number class
2022 operation and maintenance failure case experience
Crack the simple login system with NOP method
Digital twin smart server: information security monitoring platform
海思系列量产硬件调试记录
远程辅助必备免费神器ToDesk远程控制软件(答辩,远程,调试,办公)必备远程工具
js操作Cookie,js设置Cookie值,js读取Cookie值
IDM移动端功能升级说明