使用Spring的AOP实现接口方法执行时间记录
2016-06-14 09:45
791 查看
项目使用RPC提供的内部服务,需要监控每个接口方法的调用情况以及响应时间,如果接口方法开始和结束时都计时并将两个时间相减得到响应时间,势必对代码的入侵太大。使用AOP刚好能很优雅的解决这个问题!
1.MethodTimeAdvice.java 用来记录时间
Java代码
package yourpackage.utils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MethodTimeAdvice implements MethodInterceptor {
private static final Logger Log = LoggerFactory
.getLogger(MethodTimeAdvice.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// 用 commons-lang 提供的 StopWatch 计时
StopWatch clock = new StopWatch();
clock.start(); // 计时开始
Object result = invocation.proceed();
clock.stop(); // 计时结束
// 方法参数类型,转换成简单类型
Class[] params = invocation.getMethod().getParameterTypes();
String[] simpleParams = new String[params.length];
for (int i = 0; i < params.length; i++) {
simpleParams[i] = params[i].getSimpleName();
}
Object[] args = invocation.getArguments();
Log.info("Takes:" + clock.getTime() + " ms ["
+ invocation.getThis().getClass().getName() + "."
+ invocation.getMethod().getName() + "("
+ StringUtils.join(simpleParams, ",") + ")("
+ StringUtils.join(args, ",") + ")] ");
return result;
}
}
3.applicationContext.xml
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true">
<aop:aspectj-autoproxy />
<bean id="methodTimeAdvice" class="cn.hiluo.openfire.utils.MethodTimeAdvice" />
<aop:config>
<!-- 用 AspectJ 的语法定义 Pointcut,这里拦截 service 包中的所有方法 -->
<aop:advisor id="methodTimeLog" advice-ref="methodTimeAdvice"
pointcut="execution(* yourpackage.service.impl..*.*(..))" />
</aop:config>
<bean id="yourservice" class="**.impl.yourServiceImpl">
</bean>
。。。。。yourbeans
</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
1.MethodTimeAdvice.java 用来记录时间
Java代码
package yourpackage.utils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MethodTimeAdvice implements MethodInterceptor {
private static final Logger Log = LoggerFactory
.getLogger(MethodTimeAdvice.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// 用 commons-lang 提供的 StopWatch 计时
StopWatch clock = new StopWatch();
clock.start(); // 计时开始
Object result = invocation.proceed();
clock.stop(); // 计时结束
// 方法参数类型,转换成简单类型
Class[] params = invocation.getMethod().getParameterTypes();
String[] simpleParams = new String[params.length];
for (int i = 0; i < params.length; i++) {
simpleParams[i] = params[i].getSimpleName();
}
Object[] args = invocation.getArguments();
Log.info("Takes:" + clock.getTime() + " ms ["
+ invocation.getThis().getClass().getName() + "."
+ invocation.getMethod().getName() + "("
+ StringUtils.join(simpleParams, ",") + ")("
+ StringUtils.join(args, ",") + ")] ");
return result;
}
}
3.applicationContext.xml
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true">
<aop:aspectj-autoproxy />
<bean id="methodTimeAdvice" class="cn.hiluo.openfire.utils.MethodTimeAdvice" />
<aop:config>
<!-- 用 AspectJ 的语法定义 Pointcut,这里拦截 service 包中的所有方法 -->
<aop:advisor id="methodTimeLog" advice-ref="methodTimeAdvice"
pointcut="execution(* yourpackage.service.impl..*.*(..))" />
</aop:config>
<bean id="yourservice" class="**.impl.yourServiceImpl">
</bean>
。。。。。yourbeans
</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
相关文章推荐
- Java 枚举7常见种用法
- 使用Spring MVC统一异常处理实战
- Eclipse SVN插件的帐号、密码修改
- Eclipse 无法识别插件的处理办法(转载)
- Spring源码分析——Ioc实现原理
- Spring 4.2框架中注释驱动的事件监听器详解
- Spring3.2.0之后各个版本完整包下载地址
- Java开发中的23种设计模式详解(转)
- java代码优化29个点
- Android Eclipse上传svn时,不传的内容
- java ClassLoader 思考
- Struts2 Result 返回JSON对象详解
- static 及java初始化顺序
- Java基础03——位运算符
- eclipse svn operation in process 无法同步 更新 提交
- java-json日期字符串转换
- 接口的注意事项
- Java 完成水吧点饮品系统开发 集合List<E>的学习
- 十分钟理解Java中的动态代理
- maven学习(三)——eclipse集成maven