赞
踩
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.springframework.stereotype.Component;
-
- import com.alibaba.common.lang.diagnostic.Profiler;
-
- @Aspect
- @Component
- public class ProfilerAspectJ {
-
- private static final String DOT_SPLIT = ".";
-
- //执行时间超过3s 的,输出出来
- private static final String THREADHOLD = "THREADHOLD";
-
- private static final long DEFAULTTHREADHOLD = 3000L;
-
- @Around("within(com.aliyun.activity.biz.*.impl..*)")
- public Object around(ProceedingJoinPoint jp) throws java.lang.Throwable {
-
- String className = jp.getTarget().getClass().getName();
- String method = jp.getSignature().getName();
- final String key = className + DOT_SPLIT + method;
- System.out.println("className: "+className);
- try {
- // 开始 Profiler
- Profiler.start(key);
- Profiler.enter(key);
- // 调用被代理类的方法
- return jp.proceed();
-
- } finally {
- Profiler.release();
- // 释放 start 的栈
- Profiler.release();
- try {
- long threadHold = DEFAULTTHREADHOLD;
- final long duration = Profiler.getDuration();
-
- if (duration > threadHold) {
- final String detail = Profiler.dump("Detail: ", " ");
-
- StringBuffer msg = new StringBuffer();
- msg.append("调用服务:").append(className).append("的方法").append(method);
-
- if (null != jp.getArgs()) {
- msg.append(" 参数列表 [");
-
- for (int i = 0; i < jp.getArgs().length; i++) {
- Object obj = jp.getArgs()[i];
- msg.append(obj);
- if (i < jp.getArgs().length - 1) {
- msg.append(",");
- }
- }
- msg.append("]");
- }
-
- msg.append("耗时:").append(duration).append("ms,超过预期\n").append(detail).append("\n");
-
-
- }
-
- } finally {
- Profiler.reset();
- }
-
- }
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。