1.在Mycelipse 中创建maven 工程
2.在pom中添加spring依赖
主要是spring包,cglib包,log4j包等,详细查看pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cyou</groupId>
<artifactId>performence-monitor</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>performence-monitor</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<commons-lang.version>2.6</commons-lang.version>
<spring.version>3.2.4.RELEASE</spring.version>
<aspectj.version>1.7.3</aspectj.version>
<cglib.version>3.0</cglib.version>
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<junit.version>3.8.1</junit.version>
<slf4j.version>1.7.7</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<target>1.6</target>
<source>1.6</source>
<encoding>utf-8</encoding>
<fork>true</fork>
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
<compilerArguments>
<verbose />
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.在resources创建applicationContext-aop.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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="com.henry" />
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
4.创建aop切面类
/*
* 文 件 名: PerformenceMonitor.java
* 描 述: <描述>
* 修 改 人: Administrator
* 修改时间: 2014-10-22
* 跟踪单号: <跟踪单号>
* 修改单号: <修改单号>
* 修改内容: <修改内容>
*/
package com.henry.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* 监控service接口的运行时间
* <功能详细描述>
*
* @author Administrator
* @version [版本号, 2014-10-22]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@Aspect
@Component
public class PerformenceMonitor {
private static final Logger logger = LoggerFactory.getLogger("monitor");
/**
* 监控com.henry.advertising.web.service包及其子包的所有public方法
* <功能详细描述>
* @see [类、类#方法、类#成员]
*/
@Pointcut("execution(* com.henry.advertising.web.service..*.*(..))")
private void pointCutMethod() {
}
//声明环绕通知
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long begin = System.nanoTime();
Object o = pjp.proceed();
long end = System.nanoTime();
logger.debug("{}:{}",pjp.getTarget().getClass()+"."+pjp.getSignature().getName(),(end-begin)/1000000);
return o;
}
}
5.然后运行maven clean install 打包
这样就生成了一个能够监控方法执行时间的监控。可以对切面类的pointcut重新进行正则,这样就能写出一个通用的监控类
6.在别的工程中引用该jar包,实现监控
6.1在工程中引入改jar包
<dependency>
<groupId>com.cyou</groupId>
<artifactId>performence-monitor</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
6.2在工程的application.xml中引入该jar包的配置文件
<import resource="classpath:/applicationContext-aop.xml"/>
6.3在工程的log4j中配置日志文件路径
要注意顺序
<appender name="performence.monitor.file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/performece" />
<param name="Append" value="true" />
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} %r %-5p [%F:%L] : %m%n" />
</layout>
</appender>
<logger name="monitor">
<level value="debug" />
<appender-ref ref="stdout" />
<appender-ref ref="performence.monitor.file" />
</logger>
这样,在启动这个工程的时候,监控模块就能把所拦截的方法执行时间信息打印到指定的文件中,然后供开发人员分析
结果如下:
2014-10-22 12:55:15,249 2541 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:79
2014-10-22 12:55:16,632 3924 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:43
2014-10-22 12:55:17,080 4372 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:17,529 4821 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:47
2014-10-22 12:55:17,973 5265 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:18,416 5708 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:18,868 6160 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:45
2014-10-22 12:55:19,314 6606 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:45
2014-10-22 12:55:19,757 7049 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:20,203 7495 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:20,647 7939 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:45
2014-10-22 12:55:21,088 8380 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:21,531 8823 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:45
2014-10-22 12:55:21,975 9267 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:22,416 9708 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:22,867 10159 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:45
2014-10-22 12:55:23,322 10614 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:47
2014-10-22 12:55:23,768 11060 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:45
2014-10-22 12:55:24,215 11507 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:44
2014-10-22 12:55:24,662 11954 DEBUG [PerformenceMonitor.java:49] : class com.henry.advertising.web.service.ad.impl.PictureServiceImpl.getPicture:47