Interceptors used for logging app performance

A good idea when we test performance is to not touch the actual production code.

In this case the best thing would be to use a bit of AOP in our case an  EJB3 interceptor which we can remove (or override in XML).

All you need to do is add one line in the Facade /Manager you need to test:

@Interceptors(PerformanceLoggingInterceptor.class)

And have the interceptor as below in your project :

package com.snef.si.util;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

import org.apache.log4j.Logger;

/**
* Simple performance logging
*
*
*
*/
public class PerformanceLoggingInterceptor {
private static final Logger LOGGER = Logger
.getLogger(PerformanceLoggingInterceptor.class);

@AroundInvoke
public Object trackExecution(InvocationContext context) throws Exception {
LOGGER.info(“Method is ” + context.getMethod() + ” “);
long startTime = System.currentTimeMillis();
Object invocation = context.proceed();
long endTime = System.currentTimeMillis();
LOGGER.info(“Method  ” + context.getMethod() + “took ”
+ (endTime – startTime) + ” miliseconds”);
return invocation;
}

}

The important thing in the code above is the @AroundInvoke annotation which tells the container to wrap the ejb3 call with this interceptor (no matter which business method is called).

All you need to remember is to remove the logging line from the EJB class before the actual release.