Performance Zone is brought to you in partnership with:

Marius has posted 1 posts at DZone. You can read more from them at their website. View Full User Profile

Adding PMI (PerformanceMonitorInterceptor) to your application in 3 easy steps

04.04.2013
| 4134 views |
  • submit to reddit

Yesterday I found out that Spring cares about showing how fast it is, but not only that, it does make it easy for you, and without further ado let's get to business.

1. Declare the interceptor (PerformanceMonitorInterceptor) and its pointcut (declaring a pointcut) within the aspect configuration:

Save the below configuration into a config file (aop-config.xml).

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd">
        
 <aop:aspectj-autoproxy />
 
 <bean id="performanceMonitor" class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor">
  <property name="useDynamicLogger" value="true"/>
 </bean>
 
 <aop:config>
  <aop:pointcut id="allServiceMethods" expression="execution(* com.mypackage.service..*.*(..))" />
  <aop:advisor pointcut-ref="allServiceMethods" advice-ref="performanceMonitor" order="2" />
 </aop:config>
 
</beans>

2. Include the aop-config.xml file in the spring general configuration file: 

<import resource="classpath:spring-config/aop-config.xml" />

3. And enable logging for:

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

The logging has to be to TRACE in order for you to see the following:

Line 22734: 178062 ["http-bio-8079"-exec-2] TRACE org.springframework.aop.interceptor.PerformanceMonitorInterceptor  - StopWatch ' com.mypackage.service.ApplicationParametersService.getApplicationParam': running time (millis) = 12

Line 23155: 178551 ["http-bio-8079"-exec-2] TRACE org.springframework.aop.interceptor.PerformanceMonitorInterceptor  - StopWatch 'com.mypackage.service.common.PaginatedTableService.getData': running time (millis) = 467

Published at DZone with permission of its author, Marius Cristea.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Tom Briers replied on Fri, 2013/04/05 - 1:39am

 Are you sure that this configuration is correct?

You say to use a dynamic logger but next you only set the log level for the interceptor class.  When reading the api this does not seems correct:

"Used to determine which Log instance should be used to write log messages for a particular method invocation: a dynamic one for the Class getting called, or a static one for the Class of the trace interceptor. "


Marius Cristea replied on Fri, 2013/04/05 - 3:08am in response to: Tom Briers

The configuration is correct - it's just not complete when we we want to use the useDynamicLogger. We can look for further configurations in this area, but they will depend on the logging framework that we are going to use.

For easiness I would remove the property <propertyname="useDynamicLogger"value="true"/>

which will fallback to log4j.

The complete java doc for useDynamicLogger:

'Set whether to use a dynamic logger or a static logger. Default is a static logger for this trace interceptor. 

Used to determine which Log instance should be used to write log messages for a particular method invocation:  a dynamic one for the Class getting called, or a static one for the Class of the trace interceptor. NOTE: Specify either this property or "loggerName", not both. See Also:

getLoggerForInvocation(org.aopalliance.intercept.MethodInvocation)'

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.