import org.apache.commons.lang.time.StopWatch;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* 声明一个切面,记录每个Action的执行时间
*/
@Aspect
@Component
public class LogAspect { private static final Logger logger=LoggerFactory.getLogger(LogAspect.class); /**
* 切入点:配置在哪些类需要进行切入
*/
@Pointcut("execution(* com.netcai.admin.controller.*.*.*(..))")
public void pointcutExpression() {
logger.debug("配置切入点...");
} /**
* 1 前置通知
* @param joinPoint
*/
@Before("pointcutExpression()")
public void beforeMethod(JoinPoint joinPoint) {
logger.debug("前置通知...");
} /**
* 2 后置通知
* 在方法执行之后执行的代码. 无论该方法是否出现异常
*/
@After("pointcutExpression()")
public void afterMethod(JoinPoint joinPoint) {
logger.debug("后置通知... 有异常也会执行");
} /**
* 3 返回通知
* 在方法法正常结束受执行的代码
* 返回通知是可以访问到方法的返回值的!
* @param joinPoint
* @param returnValue
*/
@AfterReturning(value = "pointcutExpression()", returning = "returnValue")
public void afterRunningMethod(JoinPoint joinPoint, Object returnValue) {
logger.debug("返回通知执行,执行结果:" + returnValue);
}
/**
* 4 异常通知
* 在目标方法出现异常时会执行的代码.
* 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "pointcutExpression()", throwing = "e")
public void afterThrowingMethod(JoinPoint joinPoint, Exception e)
{
logger.debug("异常通知, 出现异常 " + e);
} /**
* 环绕通知需要携带 ProceedingJoinPoint 类型的参数.
* 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.
* 且环绕通知必须有返回值, 返回值即为目标方法的返回值
*/
@Around("pointcutExpression()")
public Object aroundMethod(ProceedingJoinPoint pjd)
{
StopWatch clock = new StopWatch();
//返回的结果
Object result = null;
//方法名称
String className=pjd.getTarget().getClass().getName(); String methodName = pjd.getSignature().getName(); try
{
// 计时开始
clock.start();
//前置通知
//执行目标方法
result = pjd.proceed();
//返回通知
clock.stop();
} catch (Throwable e)
{
//异常通知
e.printStackTrace();
}
//后置通知
if(!methodName.equalsIgnoreCase("initBinder"))
{
long constTime=clock.getTime(); logger.info("["+className+"]"+"-" +"["+methodName+"]"+" 花费时间: " +constTime+"ms"); if(constTime>500)
{
logger.error("["+className+"]"+"-" +"["+methodName+"]"+" 花费时间过长,请检查: " +constTime+"ms");
}
}
return result;
}
}

记录每个action执行时间的更多相关文章

  1. 在MVC或WEBAPI中记录每个Action的执行时间和记录下层方法调用时间

    刚才在博客园看了篇文章,http://www.cnblogs.com/cmt/p/csharp_regex_timeout.html  突然联想到以前遇到的问题,w3wp进程吃光CPU都挂起IIS进程 ...

  2. net 记录controller Action耗时

    可能有些时候需要记录Action的执行时间来优化系统功能,这时可以用过滤器来实现 第1个例子 using System; using System.Diagnostics; using System. ...

  3. 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录

    使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录 C#进阶系列——WebApi 异常处理解决方案 [ASP.NET Web API教程]4.3 AS ...

  4. Web API系列(四) 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录

    转自:https://www.cnblogs.com/hnsongbiao/p/7039666.html 需要demo在github中下载: https://github.com/shan333cha ...

  5. Action执行时间过滤器

    public class AccessStatisticsAttribute : ActionFilterAttribute { /// <summary> /// log4net 日志 ...

  6. 记录PHP的执行时间

    网上不少误导信息,实际上这个答案在PHP源码中的Zend文件夹下bench.php是有的 在此纠正下网络上复制粘贴造成的错误.希望后来人少踩点坑. function getmicrotime() { ...

  7. linux查看历史操作记录并且显示执行时间

    vim  ~/.bashrc 或者 ~/.bash_profile 增加:export HISTTIMEFORMAT="%F %T  " 查看历史记录之前先执行: 然后使用hist ...

  8. AOP记录方法的执行时间

    作用AOP监控方法的运行时间如下: @Component @Aspect public class LogAop { private Logger log = LoggerFactory.getLog ...

  9. 爱上MVC3系列~监视Action的运行时间,并提供超时记录机制

    回到目录 文章出现的原因 很久没写关于MVC的文章了,原因是将关注点移向了MVVM和DDD这边,而这篇文章完全是因为公司项目的需要,因为公司网站总是不定时的502,而这由可能是程序超时所引起的,为了分 ...

随机推荐

  1. python全栈开发,Day44(IO模型介绍,阻塞IO,非阻塞IO,多路复用IO,异步IO,IO模型比较分析,selectors模块,垃圾回收机制)

    昨日内容回顾 协程实际上是一个线程,执行了多个任务,遇到IO就切换 切换,可以使用yield,greenlet 遇到IO gevent: 检测到IO,能够使用greenlet实现自动切换,规避了IO阻 ...

  2. JavaScript 常用的技术(陆续更新)

    截取字符串(指定长度) var str = "abc-110001"; //str.substring(起始位置(0开始),截取的长度) str.substring(0,4); / ...

  3. File upload with cropping support using Cropper --jquery file upload

    File upload with cropping support using Cropper demo https://tkvw.github.io/jQuery-File-Upload/basic ...

  4. java 图片裁剪代码

    package com.actionsoft.apps.addons.invoice.pc.test; import java.awt.image.BufferedImage;import java. ...

  5. php中的构造函数与析构函数

    PHP面向对象——构造函数.析构函数 __construct.__destruct__construct 构造方法,当一个对象创建时调用此方法,使用此方法的好处是:可以使构造方法有一个独一无二的名称, ...

  6. 【转载】Spring boot学习记录(三)-启动原理解析

    前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/ 正文 我们开发任何一个Spring Boot项目,都会用到如下的启动类 @Sprin ...

  7. PHP防采集方法代码

    <?php /** * FileName:test.php * Summary: 防采集 */ $HTTP_REFERER = $_SERVER["HTTP_REFERER" ...

  8. Linear Regression and Gradient Descent

    随着所学算法的增多,加之使用次数的增多,不时对之前所学的算法有新的理解.这篇博文是在2018年4月17日再次编辑,将之前的3篇博文合并为一篇. 1.Problem and Loss Function ...

  9. Ubuntu下的图形化多线程下载器XDM

    目录 1.下载 2.安装 3.浏览器支持 使用Ubuntu下载东西经常过于缓慢,因此需要多进程下载器. 1.下载 下载链接:http://xdman.sourceforge.net/#download ...

  10. 《JAVA设计模式》之策略模式(Strategy)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述策略(Strategy)模式的: 策略模式属于对象的行为模式.其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它 ...