JAMon监控web工程方法的调用性能

JAMon简介

JAMon的全名是:Java Application Monitor。它是一个小巧的,免费的,高性能的,线程安全的性能监测工具。

它可以用来测定系统的性能瓶颈,也可以用来监视用户和应用程序之间的交互情况。 

Jamon主要是用来检测jee的应用程序。

 

JAMon集成到项目中

假设现在有一个项目名为bookShop,目录结构如下:

bookshop

  java resources

    src

      com.allen.bookshop

        filter

          PageMonFilter

  webContent

    jamon

    WEB-INF

      web.xml 

 

1.到官网去下载两个包:jamon.rar和jamon-sample.rar

http://sourceforge.net/projects/jamonapi/files/

jamon.rar里面有源码和api。

jamon-sample.rar里面有基本示例。

解压jamon-sample.rar,把解压后的文件jamon直接拷贝到webContent下,具体文件如下图:

 

2.把解压jamon-sample.rar后jamon文件里的webContent下的lib下的jar包,拷贝到自己工程的lib下。

 

3.新建一个PageMonFilter类,如上目录结构:

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.jamonapi.JAMonFilter;
import com.jamonapi.MonKeyImp;
import com.jamonapi.Monitor;
import com.jamonapi.MonitorFactory;

public class PageMonFilter extends JAMonFilter
{
    private static final long serialVersionUID = 5746197114960908454L;

    private FilterConfig filterConfig = null;

    public void init( FilterConfig filterConfig ) throws ServletException
    {
        this.filterConfig = filterConfig;
    }

    public void destroy()
    {
        this.filterConfig = null;
    }

    public void doFilter( ServletRequest request, ServletResponse response,
            FilterChain filterChain ) throws IOException, ServletException
    {
        Monitor allPages = MonitorFactory.start( new MonKeyImp(
                "jammon.webui.allPages", getURI( request ), "ms." ) );
        Monitor monitor = MonitorFactory.start( getURI( request ) );

        try
        {
            filterChain.doFilter( request, response );
        } 
        finally
        {
            monitor.stop();
            allPages.stop();
        }
    }

    protected String getURI( ServletRequest request )
    {
        if ( request instanceof HttpServletRequest )
        {
            return ((HttpServletRequest)request).getRequestURI();
        } else
        {
            return "Not an HttpServletRequest";
        }
    }
}

 

4.在web.xml上增加如下代码:

    <filter>
        <filter-name>JAMonFilter</filter-name>
        <filter-class>com.allen.bookshop.filter.PageMonFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>JAMonFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

5.增加jamon_bean.xml文件, 用于配置你要监听哪些类。action方法不用配置,默认会监听。

<?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:osgi="http://www.springframework.org/schema/osgi"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/osgi
         http://www.springframework.org/schema/osgi/spring-osgi.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">

    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>bookshopService</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>jamonInterceptor</value>
            </list>
        </property>
    </bean>
    
    <bean id="jamonInterceptor" class="org.springframework.aop.interceptor.JamonPerformanceMonitorInterceptor">
</bean>
</beans>

 

6.在log4j.properties中添加如下配置:

log4j.logger.org.springframework.aop.interceptor.JamonPerformanceMonitorInterceptor = TRACE

 

重新启动工程。

至此,配置完成,现在可以访问http://localhost:8080/bookshop/jamon/menu.jsp访问jamon了

    

JAMon监控web工程方法的调用性能,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。