使用Spring的AOP实现接口方法执行时间记录

项目使用RPC提供的内部服务,需要监控每个接口方法的调用情况以及响应时间,如果接口方法开始和结束时都计时并将两个时间相减得到响应时间,势必对代码的入侵太大。使用AOP刚好能很优雅的解决这个问题!

 

1.MethodTimeAdvice.java 用来记录时间

 

Java代码   收藏代码
  1. package yourpackage.utils;  
  2.   
  3. import org.aopalliance.intercept.MethodInterceptor;  
  4. import org.aopalliance.intercept.MethodInvocation;  
  5. import org.apache.commons.lang.StringUtils;  
  6. import org.apache.commons.lang.time.StopWatch;  
  7. import org.slf4j.Logger;  
  8. import org.slf4j.LoggerFactory;  
  9.   
  10. public class MethodTimeAdvice implements MethodInterceptor {  
  11.   
  12.     private static final Logger Log = LoggerFactory  
  13.             .getLogger(MethodTimeAdvice.class);  
  14.   
  15.     @Override  
  16.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  17.         // 用 commons-lang 提供的 StopWatch 计时  
  18.         StopWatch clock = new StopWatch();  
  19.         clock.start(); // 计时开始  
  20.         Object result = invocation.proceed();  
  21.         clock.stop(); // 计时结束  
  22.   
  23.         // 方法参数类型,转换成简单类型  
  24.         Class[] params = invocation.getMethod().getParameterTypes();  
  25.         String[] simpleParams = new String[params.length];  
  26.         for (int i = 0; i < params.length; i++) {  
  27.             simpleParams[i] = params[i].getSimpleName();  
  28.         }  
  29.         Object[] args = invocation.getArguments();  
  30.   
  31.         Log.info("Takes:" + clock.getTime() + " ms ["  
  32.                 + invocation.getThis().getClass().getName() + "."  
  33.                 + invocation.getMethod().getName() + "("  
  34.                 + StringUtils.join(simpleParams, ",") + ")("  
  35.                 + StringUtils.join(args, ",") + ")] ");  
  36.         return result;  
  37.     }  
  38.   
  39. }  

 

3.applicationContext.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  8.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  
  9.     default-lazy-init="true">  
  10.         <aop:aspectj-autoproxy />  
  11.     <bean id="methodTimeAdvice" class="cn.hiluo.openfire.utils.MethodTimeAdvice" />  
  12.     <aop:config>  
  13.         <!-- 用 AspectJ 的语法定义 Pointcut,这里拦截 service 包中的所有方法 -->  
  14.         <aop:advisor id="methodTimeLog" advice-ref="methodTimeAdvice"  
  15.             pointcut="execution(* yourpackage.service.impl..*.*(..))" />  
  16.     </aop:config>  
  17.         <bean id="yourservice" class="**.impl.yourServiceImpl">  
  18.           
  19.     </bean>  
  20.         。。。。。yourbeans  
  21. </beans>  

 

4.注意需要加入Spring相关包,比较容易遗漏的:org.springframework.aop-3.0.6.RELEASE.jar,org.springframework.aspects-3.0.6.RELEASE.jar,aspectjweaver-1.5.3.jar,aopalliance-1.0.jar

 

5.applicationContext.xml注意xml中和AOP相关的dtd的引入,还要注意pointcut="execution(* yourpackage.service.impl..*.*(..))" 的配置语法

 

6.最后在info.log里面可以看到如上图的输出,最后再用shell统计即可。

 

参考:

http://jportal.iteye.com/blog/945725

 

http://www.java63.com/spring/configure_implement_aop.html 


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值