- 浏览: 328498 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
zhengqiweiyi:
请问这里的客户端和服务器是指同一台电脑上的吗?
JSP动态显示服务器时间和客户端时间 -
18101095336:
一语道破天机[color=blue][/c ...
restlet使用中遇到的一些问题 -
jjjssh:
废话一大堆,就是没说到少了那个jar包
The hierarchy of the type is inconsistent -
junjielee:
重点还没说啊~~~~~~~~
The hierarchy of the type is inconsistent -
幽绿琵琶:
太棒了
loadrunner 9.5 9.0 8.1下载地址 和license
项目使用RPC提供的内部服务,需要监控每个接口方法的调用情况以及响应时间,如果接口方法开始和结束时都计时并将两个时间相减得到响应时间,势必对代码的入侵太大。使用AOP刚好能很优雅的解决这个问题!
1.log4j的配置log4j.xml。这里使用xml风格的配置
<appender name="info-out" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="/data/logs/hiluo-service/info.log" /> <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss} [%C{1}:%M] %m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMax" value="info" /> <param name="LevelMin" value="info" /> <param name="AcceptOnMatch" value="true" /> </filter> </appender>
log4j的配置参见:http://stonexmx.blog.163.com/blog/static/1221585872009716112428738/
PatternLayout输出格式参见:http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
value="%d{yyyy.MM.dd HH:mm:ss} [%C{1}:%M] %m%n",%C{1}-类名,如果使用%C将输出类的全路径名,%M-方法名
输出类似以下内容:
2.MethodTimeAdvice.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 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
因为对spring的aop不是很熟,在实现上有细节问题,在此记录下!
发表评论
-
ConcurrentHashMap的高性能
2012-07-17 18:11 1426ConcurrentHashMap 是一个并发散列映射表 ... -
Hash散列及冲突解决
2012-07-17 17:56 2690先看看英文的维基百科上的解释: A hash func ... -
调用谷歌接口实现基站转经纬度
2012-07-02 15:34 6464Cell2GPSUtil.java,最近项目中用到的,记录一下 ... -
JSP动态显示服务器时间和客户端时间
2012-05-22 16:29 6342server-time.jsp内容如下: <%@ pa ... -
禁止浏览器缓存当前文档内容
2011-06-08 17:21 1596IE8的设置中有这么一个选项: 默认是“自动 ... -
java正则匹配微博@和话题#话题#
2011-05-26 11:46 7904/** * 微博内容中的at正则表达式 ... -
restlet使用中遇到的一些问题
2011-02-22 17:37 18100x20 is not a legal NCName cha ... -
spring 2.5.6和hibernate3.2.2集成(5)
2010-02-08 15:48 1922这一次说一下hibernate tools这个工具的使用,OR ... -
spring 2.5.6和hibernate3.2.2集成(4)
2010-02-04 16:41 2710这个列子的来源是springside下的miniweb项目,算 ... -
spring 2.5.6和hibernate3.2.2集成(3)
2010-02-04 16:10 1669最近看了一下springside,学习很多,从思想到编码风格, ... -
spring 2.5.6和hibernate3.2.2集成(2)
2010-02-04 15:52 1277上一篇文章写了如何使用spring的注解,这一篇记录一下和hi ... -
spring 2.5.6和hibernate3.2.2集成(1)
2010-02-04 15:33 1244spring注解的使用,这个例子来源于网上,我也不知道原创是哪 ... -
The project was not built since the source file could not be read
2010-01-25 10:25 3771包缺失 重新加入JRE System Library 类 ... -
The hierarchy of the type is inconsistent
2010-01-22 10:02 14791这两天在尝试spring2.5.6和hibernate3.2的 ... -
activemq-5.3集成spring2.5以及quartz的调用
2010-01-06 10:42 1446前几天做的demo,记录一下,希望对大家有帮助,内容均参考网上 ... -
Dom4j的使用,存着以后看
2008-12-05 10:21 1067Dom4j的使用(全而好的文 ... -
JVM中的堆和栈
2007-12-29 16:19 2209JVM是基于堆栈的虚拟机. ... -
Struts2.0上传练习
2007-11-09 16:09 6729终于上次的项目告一段落了,可以重新学习Struts了,于是终于 ... -
struts2.0使用时的一个奇怪的问题
2007-10-21 22:31 1046近来struts2.0很流行哦,于是下了更流行的max大侠的教 ... -
DAO+Factory+Hibernate的学习。。
2007-09-30 16:48 2997看了很多的书了,想着实践一下,就参考了书上的例子写了一点,呵呵 ...
相关推荐
日志 不使用通用日志 使用SLF4J 使用Log4j 二世。 什么是新的在春季3 2。 新特性和增强功能在Spring框架3.0 2.1。 Java 5 2.2。 改进文档 2.3。 新的文章和教程 2.4。 新模块的组织和构建系统 2.5。 概述的新特性 2...
org.apache.log4j org.apache.openjpa org.apache.axis org.apache.catalina org.apache.commons org.apache.coyote org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache....
log4j-1.2.8.jar naming-factory.jar poi-3.0-alpha2-20060616.jar poi-contrib-3.0-alpha2-20060616.jar poi-scratchpad-3.0-alpha2-20060616.jar spring-aop.jar spring-test.jar spring-webmvc-struts.jar spring...
org.apache.log4j org.apache.openjpa org.apache.axis org.apache.catalina org.apache.commons org.apache.coyote org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache....
1.3.2.3 使用Log4J .. 19 第二部分 Spring 3 的新特性.. 21 第2 章 Spring 3.0 的新特性和增强 21 2.1 Java 5.. 21 2.2 改进的文档. 21 2.3 新的文章和教程. 21 2.4 新的模块组织方式...
Spring强调面向接口编程,所以我们将所有对Tfile的数据操作的方法定义在TfileDAO接口中,这些接口方法分别是: •findByFildId(String fileId) •save(Tfile tfile) •List findAll() TfileDAOHibernate...
本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说...
20020414.jar,slf4j-api-1.5.8.jar,slf4j-log4j12-1.5.8.jar,spring-aop-3.0.7.RELEASE.jar,spring-asm-3.0.7.RELEASE.jar,spring-beans-3.0.7.RELEASE.jar,spring-context-3.0.7.RELEASE.jar,spring-core-3.0.7....
log4j-core-2.0-rc1.jar log4j-core-2.3.jar log4jdbc-remix-0.2.7.jar logback-classic-1.0.0.jar logback-core-1.0.0.jar mvel2-2.0.11.jar mybatis-3.1.1.jar mybatis-3.2.2.jar mybatis-3.2.7.jar mybatis-...
41,log4j-1.2.16.jar 42,lucene-3.5.0.jar 43,mail-1.4.4.jar 44,mchange-commons-java-0.2.3.4.jar 45,mysql-jdbc-5.1.25.jar 46,oracle-jdbc-11.2.jar 47,pjl-comp-filter-1.7.jar 48,poi-3.8.jar 49,...
com.springsource.org.apache.log4j-sources-1.2.15.jar commons-digester3-3.0-sources.jar commons-fileupload-1.2.2-sources.jar hessian-4.0.7-src.jar spring-security-acl-3.0.3.RELEASE-sources.jar spring-...
使用.ashx文件处理IHttpHandler实现发送文本及二进制数据的方法 制作一个简单的多页Tab功能 一完美的关于请求的目录不存在而需要url重写的解决方案! 在C#中实现MSN消息框的功能 XmlHttp实现无刷新三联动ListBox 鼠标...
1.3.2.3 使用Log4J ............................................................................................................................. 20 第二部分 Spring 3的新特性...............................
-- Spring的log4j监听器 --> <listener-class>org.springframework.web.util.Log4jConfigListener <!-- 字符集 过滤器 --> <filter-name>CharacterEncodingFilter <filter-class>org.springframework....
14.Other: Log4j,Velocity,Sitemesh,Jfreechart,Jforum,Solr 15.Maven 2 3.BPMX3的组件构建方式 BPMX3同时也是基于组件构构建,整个系统的构建如下所示: 【图三】BPMX3组件构建 系统提供在线流程设计器,...
Using Log4J ............................................................................................. 14 II. What’s New in Spring Framework 4.x ......................................................
Using Log4J ............................................................................................. 14 II. What’s New in Spring Framework 4.x ......................................................
log4j-1.2.6.jar logback-classic-1.0.6.jar logback-classic-1.2.2.jar logback-core-1.2.3.jar logkit-1.0.1.jar logkit-2.0.jar maven-archiver-2.3.jar maven-artifact-2.0.4.jar maven-artifact-2.0.5.jar ...