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:


And have the interceptor as below in your project :


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

public Object trackExecution(InvocationContext context) throws Exception {“Method is ” + context.getMethod() + ” “);
long startTime = System.currentTimeMillis();
Object invocation = context.proceed();
long endTime = System.currentTimeMillis();“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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.