Over a million developers have joined DZone.
Platinum Partner

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

· Performance Zone

The Performance Zone is brought to you in partnership with New Relic. Quickly learn how to use Docker and containers in general to create packaged images for easy management, testing, and deployment of software.

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

The Performance Zone is brought to you in partnership with New Relic. Read more about providing a framework that gets you started on the right path to move your IT services to cloud computing, and give you an understanding as to why certain applications should not move to the cloud.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}