码迷,mamicode.com
首页 > 其他好文 > 详细

aop计算方法耗时

时间:2018-01-03 11:39:23      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:nat   frame   hat   layer   pwa   odi   col   roc   gettime   

package necs.omms.common.aop;

import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
@CommonsLog
public class PerformanceMonitor {
private static Log log = LogFactory.getLog(PerformanceMonitor.class);


/**
* A join point is in the controller layer if the method is
* modified by public and defined in a type in the
* com.shawn.service package or any sub-package under that
* and modified by public.execution(* com.baobaotao..*(..))l
*/
@Pointcut("execution( * necs.omms.controller..*(..))")
private void controllerLayer() {
}

/**
* Monitor the elapsed time of method on controller layer, in
* order to detect performance problems as soon as possible.
* If elapsed time > 1 s, log it as an error. Otherwise, log it
* as an info.
*/
@Around("controllerLayer()")
public Object monitorElapsedTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// Timing the method in controller layer
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object result = proceedingJoinPoint.proceed();
stopWatch.stop();

// Log the elapsed time
double elapsedTime = stopWatch.getTime() / 1000;
Signature signature = proceedingJoinPoint.getSignature();
String infoString = "[" + signature.toShortString() + "][Elapsed time: " + elapsedTime + " s]";
if (elapsedTime > 1) {
log.error(infoString + "[Note that it‘s time consuming!]");
} else {
log.info(infoString);
}

// Return the result
return result;
}

@Before(value = "controllerLayer()")
public void doBefore(){
System.out.println("-------------@BEFORE------------");
}

}

aop计算方法耗时

标签:nat   frame   hat   layer   pwa   odi   col   roc   gettime   

原文地址:https://www.cnblogs.com/wlhebut/p/8182206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!