JAMon監控web工程方法的呼叫效能

水之原發表於2014-06-13

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了

    

相關文章