配置方式:
1.下载地址:http://code.google.com/p/javamelody/downloads/list
2.下载后可以解压找到javamelody.jar和jrobin-1.5.9.1.jar,引用到项目中.
3.在web.xml中配置filter:
<filter>
<filter-name>monitoring</filter-name>
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
<init-param>
<param-name>log</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>monitoring</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>net.bull.javamelody.SessionListener</listener-class>
</listener>
4.重启项目:打开链接:http://<host>:<port>/projectname/monitoring
例如:http://127.0.0.1:8080/myproj/monitoring
至此,基本配置已完成,可以进行简单的性能监控,下面配置是javamelody与struts2和spring3的集成配置:
5.javamelody和struts2集成时配置:
<package name="base" namespace="/" extends="struts-default">
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult" />
</result-types>
<interceptors>
<!-- javamelody -->
<interceptor name="monitoring" class="net.bull.javamelody.StrutsInterceptor" />
<interceptor-stack name="myStack">
<interceptor-ref name="monitoring" />
<!--
struts默认的拦截器
-->
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack" />
其他struts配置文件继承base,如下所示:
<package name="example" namespace="/" extends="base">
<action name="student_*" class="studentAction" method="{1}">
<result name="input">/pub_error.jsp</result>
</action>
</package>
6.javamelody与Spring集成:
spring的配置文件中配置:
<bean id= "facadeMonitoringAdvisor" class="net.bull.javamelody.MonitoringSpringAdvisor" >
<property name ="pointcut" >
<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut" >
<property name ="pattern" value="com.proj.*.service.*" /><!--正则表达式-->
</bean>
</property>
</bean>
然后配置:
方式一,web.xml中加入如下配置:
<context-param>
<param-name> contextConfigLocation</param-name>
<param-value>
classpath:applicationContext*.xml<!--Spring配置文件-->
classpath:net/bull/javamelody/monitoring-spring.xml<!--和spring集成时添加-->
</param-value>
</context-param>
方式二,在spring.xml中加入如下配置:
<import resource="classpath:net/bull/javamelody/monitoring-spring.xml" />
7.如果你的所有spring管理的bean都实现了接口,用步骤6中的配置一切OK,但如果有些bean没有实现接口,使用步骤6中的配置将会出错.
则将步骤6中的配置改为(这样将使用cglib代理):
spring配置文件中配置:
<bean id= "facadeMonitoringAdvisor" class="net.bull.javamelody.MonitoringSpringAdvisor" >
<property name ="pointcut" >
<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut" >
<property name ="pattern" value="com.proj.*.service.*" /><!--正则表达式-->
</bean>
</property>
</bean>
方式一,web.xml中加入如下配置:
<context-param>
<param-name> contextConfigLocation</param-name>
<param-value>
classpath:applicationContext*.xml<!--Spring配置文件-->
classpath:net/bull/javamelody/monitoring-spring-aspectj.xml<!--和spring集成时添加-->
</param-value>
</context-param>
方式二,在spring.xml中加入如下配置:
<import resource="classpath:net/bull/javamelody/monitoring-spring-aspectj.xml" />
8.在项目中经常这样得到泛型类:
private Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
使用步骤7配置的cglib代理后会以上代码会出错,可以改为下面这种方式得到泛型类:
private Class<T> entityClass = (Class<T>) getSuperClassGenricType(this
.getClass(), 0);
public static Class<?> getSuperClassGenricType(Class<?> clazz, int index)
throws IndexOutOfBoundsException {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
return Object.class;
}
if (!(params[index] instanceof Class<?>)) {
return Object.class;
}
return (Class<?>) params[index];
}
9.javamelody缓存文件的清除:默认路径一般在tomcat_home/temp/javamelody中.
10.如果需要将结果导出为pdf,只要在项目中引用iText.jar即可自动加上导出pdf的链接.
11.javamelody与Quartz集成需要配置:
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="exposeSchedulerInRepository" value="true" />
...
</bean>
12.javamelody中配置可选参数:
web.xml中配置,以日志记录为例,如下配置将开启info级别的日志记录:
<filter>
<filter-name>monitoring</filter-name>
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
<init-param>
<param-name>log</param-name>
<param-value>true</param-value>
</init-param>
</filter>
其他可选参数:
system-actions-enabled:默认为true,控制启动或停止system actions garbage collector, http sessions, heap dump, memory histogram, process list, jndi tree, opened jdbc connections, database.
url-exclude-pattern:可配置正则表达式,排除一些url不监控.
http-transform-pattern,sql-transform-pattern,ejb-transform-pattern, spring-transform-pattern, guice-transform-pattern, error-transform-pattern, log-transform-pattern, job-transform-pattern, jsf-transform-pattern, struts-transform-pattern and jsp-transform-pattern :可配置正则表达式,用于合并javamelody统计数据.
storage-directory: 默认为以javamelody,tomcat为例,默认存储路径为TOMCAT_HOME/temp/javamelody
monitoring-path:默认为:/monitoring,可以更改为其他地址,例如改为/mo,则监控地址改为:http://<host>:<port>/projectname/mo
no-database:默认false,配置为true将不监控和数据有关的行为.
disabled:默认false,配置为true将不再监控.
更多内容请参考:http://code.google.com/p/javamelody/wiki/UserGuide