zoukankan      html  css  js  c++  java
  • aop计算方法耗时

    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------------");
    }

    }
  • 相关阅读:
    使用FileReader在浏览器读取预览文件(image和txt)
    移动端Vue图片获取,压缩,预览组件-upload-img(H5+获取照片,压缩,预览)
    文件(图片)转base64
    Vue单页面应用打包app处理返回按钮
    hbuilder/hbuilderx 无法检测到模拟器
    不启动AndroidStudio直接启动其模拟器
    ES6,箭头函数 (=>)注意点
    ES6,扩展运算符
    strcmp使用注意
    android11 gc5035前置摄像头当作后置使用左右镜像问题
  • 原文地址:https://www.cnblogs.com/wlhebut/p/8182206.html
Copyright © 2011-2022 走看看