有些参数想在日志中显示,但是不想在方法中处理,就想根据线程保存起来,然后从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();
}