当前位置:网站首页>Actual combat in ThreadLocal project
Actual combat in ThreadLocal project
2022-07-25 02:31:00 【WuSong1999】
ThreadLocal The function of is to share data in the same thread , Every request in the project comes in , It is the same thread from beginning to end , In the same thread, if the data put in memory by the previous thread is to be shared with the next thread , Then you can use it ThreadLocal
ThreadLocal The core principle of is , At the bottom of it is a Map<Thread,Object> threadLocal structure
/**
* Before executing the target method , First judge the user login status . And encapsulate user information to Controller
*/
public class CartInterceptor implements HandlerInterceptor {
public static ThreadLocal<UserInfoTo> threadLocal = new ThreadLocal<>();
/*
Intercept before target method
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
UserInfoTo userInfoTo = new UserInfoTo();
HttpSession session = request.getSession();
MemberRespVo member = (MemberRespVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
if (member != null) {
// The user login
userInfoTo.setUserId(member.getId());
}
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)) {
userInfoTo.setUserKey(cookie.getValue());
userInfoTo.setTempUser(true);
}
}
}
// Before the target method is executed
// If there are no temporary users , Assign temporary users
if (StringUtils.isEmpty(userInfoTo.getUserKey())) {
String uuid = UUID.randomUUID().toString();
userInfoTo.setUserKey(uuid);
}
threadLocal.set(userInfoTo);
return true;
}
/*
After business execution Assign temporary users
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
UserInfoTo userInfoTo = threadLocal.get();
// If there is no information , Create temporary user , preservation cookie
if (!userInfoTo.isTempUser()) {
Cookie cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfoTo.getUserKey());
cookie.setDomain("gulimall.com");
// Unit second
cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
response.addCookie(cookie);
}
}
}
边栏推荐
- English grammar_ Reflexive pronoun
- Genesis, the world's first "consumption investment" public chain, was invited to participate in consensus 2022
- Windows Server 2022 received a non security update in July: fix the sticking problem caused by defender
- 6-10 vulnerability exploitation SMTP experimental environment construction
- Solution to the occupation of project startup port
- Master jedispoolconfig parameter configuration and learn tuning skills
- CSRF attack principle scenario
- Automatically create partition tables by time and month in PostgreSQL
- "Ask the sky" is in place! Yu Jingchuan's "China Space Station" in 2013 is about to set sail in the sea of stars
- Inventory of well-known source code mall systems at home and abroad
猜你喜欢

Focus on improving women's and children's sense of gain, happiness and security! In the next ten years, Guangzhou Women's and children's undertakings will make such efforts

Computing network, AI first, shengteng AI helps operators' Digital Transformation -- work together to win-win Computing Era

Consul cluster deployment

VRRP virtual redundancy protocol configuration

After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability

DLL load failed: the page file is too small to complete the operation

BMW I3 based on clar architecture is not a simple "oil to electricity" product

Easy to master SSO single sign on, see this article

Creating elements of DOM series

Unable to display spline in UE4 (unreal engine4) terrain editing tool
随机推荐
QT realizes calendar beautification
Nacos service discovery data model
Data integration | what are the tools for data integration at home and abroad?
Keepalivetime=0 description of ThreadPoolExecutor
Detailed explanation of the principles and differences between static pages and dynamic pages
When does MySQL use table locks and row locks?
H5 common positioning function package
Origin and development of philosophy Hegel external historical form
Arthas case: dynamic update application logger level
Let's customize the loader
Text reading end judgment
After six years of precipitation, new consumption has found a digital transformation paradigm
After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
Computing network, AI first, shengteng AI helps operators' Digital Transformation -- work together to win-win Computing Era
Custom types in C language
Using ODBC to consume SAP ABAP CDs view in Excel
Cloudrev deploy your own public cloud disk (pagoda installation method)
When executing SQL query statements in MySQL database, the underlying implementation principle (ultra detailed)
Is it necessary to increase the number of milliseconds and save several KB of memory in the program?
How to communicate with aliens