验证中...
码云 IDEA 插件最新版本发布,支持 Pull Request
语言: Java
分类: 常用工具包
最后更新于 2019-01-12 17:44
ThreadLocal
原始数据 复制代码
package com.shuwei.jmt.publicdata.mvc.message;
import javax.servlet.http.HttpServletRequest;
/**
* 任务管理业务处理请求上下文
* Created by yangpeng on 2018/12/19 21:38
*/
public class JmtRequestContext {
private static ThreadLocal<JmtRequestContext> jmtRequestContextThreadLocal = new ThreadLocal<JmtRequestContext>();
private JmtRequestUser jmtRequestUser;
public JmtRequestContext(HttpServletRequest request) {
//todo this.jmtRequestUser = getJmtRequestUser(request);
}
private JmtRequestUser getJmtRequestUser(HttpServletRequest request){
String userId = request.getHeader("UserId");
String userName = request.getHeader("UserName");
JmtRequestUser jmtRequestUser = new JmtRequestUser();
jmtRequestUser.setUserId(Integer.valueOf(userId));
jmtRequestUser.setUserName(userName);
return jmtRequestUser;
}
/**
* 设置请求上下文
* @param jmtRequestContext
*/
public static void set(JmtRequestContext jmtRequestContext){
jmtRequestContextThreadLocal.set(jmtRequestContext);
}
/**
* 获取请求上下文
*/
public static JmtRequestContext getCurrentJmtRequestContext(){
return jmtRequestContextThreadLocal.get();
}
/**
* 清理请求上下文
*/
public static void clear(){
jmtRequestContextThreadLocal.remove();
}
/**
* 获取当前请求操作用户
* 在使用多线程时,需要启动线程前先将上下文传入线程对象中,在线程运行前进行初始化
*/
public static JmtRequestUser getCurrentOpUser(){
return jmtRequestContextThreadLocal.get().getJmtRequestUser();
}
public JmtRequestUser getJmtRequestUser(){
return this.jmtRequestUser;
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助