Skip to content

Spring Annotation Driven Performance Monitors

michajlo edited this page Nov 20, 2012 · 1 revision

Annotation Driven PerformanceMonitors

Introduction

JRugged 3.x brought about a new way to configure your classes with PerformanceMonitors which eliminates a lot of the ceremony associated with the traditional setup. That being said, there is some work, just not as much!

Prequisites

You will need JRugged 3.0.3 - this may work for earlier versions, but 3.0.3 is recommended. If you are using Maven, adding the following dependencies to your project's pom.xml should be sufficient:

<dependency>
  <groupId>org.fishwife</groupId>
  <artifactId>jrugged-core</artifactId>
  <version>3.0.5</version>
</dependency>
<dependency>
  <groupId>org.fishwife</groupId>
  <artifactId>jrugged-aspects</artifactId>
  <version>3.0.5</version>
</dependency>
<dependency>
  <groupId>org.fishwife</groupId>
  <artifactId>jrugged-spring</artifactId>
  <version>3.0.5</version>
</dependency>

It is also assumed you have a Spring application configured, or know how to do so.

Additionally, you will need to enable AspectJ support. See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html section 7.2.1 for more information.

Details

First, you must create a PerformanceMonitorBeanFactory to create and manage your PerformanceMonitorBeans.

<bean name="performanceMonitorBeanFactory" class="org.fishwife.jrugged.spring.PerformanceMonitorBeanFactory" lazy-init="false">
    <property name="packageScanBase" value="..." />
    <property name="initialPerformanceMonitors">
        <!-- List<String> of initial PerformanceMonitor names -->
    </property>
    <property name="mBeanExporter" ref="..." />
</bean>

The properties are as follows:

name type default description
packageScanBase String null A package under which all public methods on all classes will be searched for @PerformanceMonitor annotations. PerformanceMonitorBeans for each found annotation will be pre-created
initialPerformanceMonitors List emptyList A List of names of PerformanceMonitorBeans to precreate
mBeanExporter MBeanExporter null If set this will be used to expose any created PerformanceMonitorBeans

Next, you need to create a PerformanceMonitorAspect which will be responsible for proxying any methods annotated with @PerformanceMonitor to run through a PerformanceMonitor (Note: the PerformanceMonitor class and annotation have the same name but belong to different packages). This will require your PerformanceMonitorBeanFactory.

<bean name="performanceMonitorAspect" class="org.fishwife.jrugged.aspects.PerformanceMonitorAspect">
    <property name="performanceMonitorFactory" ref="performanceMonitorBeanFactory" />
</bean>

Finally annotate methods you want to monitor with @PerformanceMonitor and enjoy the magic!

public class MyWidget {

    @PerformanceMonitor("myMonitoredMethod")
    public void toMonitor() {
        ...
    }

    public void notToMonitor() {
        ...
    }

}

The value of the @PerformanceMonitor is the name of the PerformanceMonitorBean it will update. Multiple methods may share the same PerformanceMonitorBean by using the same value in @PerformanceMonitor. Also, PerformanceMonitorBeans will be created on demand as @PerformanceMonitor annotations are encountered.

Caveats

  • Only @PerformanceMonitor annotations on classes which exist as beans will be honored
  • As a result of above, @PerformanceMonitor annotations found and pre-created when using the package scan method may not be updated unless it is attached to a method on a class which exists as a bean
  • Not adding the items in the spring config (Namely the factory and aspect config) will cause the annotation to not work correctly, leading to an empty performance monitor.