有些参数想在日志中显示,但是不想在方法中处理,就想根据线程保存起来,然后从action向下面传递的时候不需要再处理此参数。
首先写个使用threadlocal保存参数的工具类
public class LogThreadLocal {
private static Logger log = Logger.getLogger(LogThreadLocal.class);
private static final ThreadLocal threadParam = new ThreadLocal();
public static void getParam(HashMap... paramMap) {
InParam s = (InParam) threadParam.get();
try {
if (s == null && paramMap.length == 1) {
s = new InParam(Thread.currentThread().getName(),paramMap[0].get(BaseConstant.SQ_ID).toString(),paramMap[0]);
threadParam.set(s);
log.info("threadLocal is null , SQ_ID = " + paramMap[0].toString());
} else if (s != null) {
if (s.getSQ_ID() == null ) {
log.error("thread name ="+Thread.currentThread().getName()+" log SQ_ID is null ");
}else if (paramMap[0].get(BaseConstant.SQ_ID)==null){
log.info("thread name ="+Thread.currentThread().getName()+" log SQ_ID is " + s.getSQ_ID());
}else {
s = new InParam(Thread.currentThread().getName(),paramMap[0].get(BaseConstant.SQ_ID).toString(), paramMap[0]);
log.info("new thread param = thread name =" + Thread.currentThread().getName()+"SQ_ID = "+paramMap[0].get(BaseConstant.SQ_ID).toString());
threadParam.set(s);
}
} else {
log.error("s == null and paramMap.length()= " + paramMap.length);
}
} catch (Exception ex) {
log.error("Medthod:getParam error", ex);
}
}
}
然后写个AOP的类
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
long procTime = System.currentTimeMillis();
try {
// LogThreadLocal.getParam();
Object[] args = methodInvocation.getArguments();
System.out.println("args = "+ Arrays.toString(args));
HashMap<String, String> tempMap = new HashMap<String, String>();
for(Object o : args){
log.info("o="+o.toString());
if(o.toString().contains("$")){
// Map tempMap = new HashMap();
tempMap.put(BaseConstant.SQ_ID,o.toString());
}
if(o instanceof User){
User tempUser = (User)o;
tempMap.put("email",tempUser.getEmail());
tempMap.put("pwd",tempUser.getPwd());
}
}
LogThreadLocal.getParam(tempMap);
return methodInvocation.proceed();
}catch (Exception e){
log.error("error ",e);
}
finally {
log.info(getMsg(methodInvocation, procTime));
return methodInvocation.proceed();
}
}
在spring中配置
<bean id="logAop" class="com.easy.todo.util.LogAop"/>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Dao,*Service,*Manager</value>
</property>
<property name="interceptorNames">
<list>
<value>logAop</value>
</list>
</property>
</bean>
然后写方法进行测试
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
final Semaphore semp = new Semaphore(thread_num);
for (int index = 0; index < client_num; index++) {
final int NO = index;
Runnable run = new Runnable() {
public void run() {
try {
//http://localhost:8080/login/reg_register.action?email=ztrea@si.com&pwd=111&SQ_ID=3123
semp.acquire();
System.out.println("Thread:" + NO);
String host = "http://localhost:8080/login/reg_register.action?";
String para = "email=ztrea@si.com&pwd=111&SQ_ID=$" + Math.random();
System.out.println(host + para);
URL url = new URL(host);// 此处填写供测试的url
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print(para);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
String result = "";
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println("第:" + NO + " 个");
semp.release();
} catch (Exception e) {
e.printStackTrace();
}
}
};
exec.execute(run);
}
// 退出线程池
exec.shutdown();
}
分享到:
相关推荐
AopLogAopLog是基于SpringAop和ThreadLocal实现的一个对请求方法埋点记录与处理的日志工具包。设计目的和场景:使用Spring Aop拦截程序,基本上都是同一个小异,不想日后每个项目都柏林都写一份这样的Aop拦截处理...
python + selenium + unittest + PO + BeautifulReport + redis + mysql + ParamUnittest + 多线程 + 截图/日志 + 多浏览器支持 + RemoteWebDriver +文件读取 + 全参数化构建 搭建过程中非常感谢李鹏飞大侠的技术...
j2ee项目经常需要记录操作者的日志,本demo在ssh框架上运用了spring的aop,加上threadlocal(用户信息,请求信息)完成了日志记录。
ThreadLocal保证一个类的实例变量在各个线程中都有一份单独的拷贝, 从而不会影响其他线程中的实例变量
ThreadLocal入门教程。 讲解了线程安全和ThreadLocal的使用的基本知识。
ThreadLocal
ThreadLocal应用示例及理解,这个写了相关的示例,可以参考一下。
理解ThreadLocal 理解ThreadLocal 理解ThreadLocal 理解ThreadLocal
学习ThreadLocal,了解其中的原理,以及学习其中的优点!避免坑点!!
正确理解ThreadLocal.pdf
DbUTils中用ThreadLocal类
主要介绍ThreadLocal的原理,实例分析以及注意事项
java 简单的ThreadLocal示例
ThreadLocal的几种误区ThreadLocal的几种误区ThreadLocal的几种误区
本例以序列号生成的程序为例,展示ThreadLocal的使用
Synchronized与ThreadLocal
设计模式及ThreadLocal详细讲解资料,想要学习java或者提升自己技术的同学可以下载观看
详解java底层实现原理,ThreadLocal底层实现的数据结构,为什么不会导致内存泄露
JDBC事务的封装和Threadlocal实例,参考博客:http://blog.csdn.net/daijin888888/article/details/50988053