JavaMelody能够在运行环境监测Java或Java EE应用程序服务器。并以图表的形式显示:Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http请求、sql请求、jsp页面与业务接口方法(EJB3、Spring、Guice)的执行数量,平均执行时间,错误百分比等。图表可以按天,周,月,年或自定义时间段查看。
监控效果图见本文结尾部分.
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
至此,基本配置已完成,可以进行简单的性能监控,下面配置是javamelody与struts2和spring3的集成配置:
<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>
<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>然后配置:
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]; }
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="exposeSchedulerInRepository" value="true" /> ... </bean>12.javamelody中配置可选参数:
<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>其他可选参数:
更多内容请参考:http://code.google.com/p/javamelody/wiki/UserGuide
效果图: