adam bien's blog

What Is The @Interceptors Overhead? 📎

The RESTful EJB 3.1 InterceptedHelloService:

@Path("intercepted")
@Interceptors({LoggingInterceptor.class,AnotherInterceptor.class})
@Stateless
@Produces(MediaType.TEXT_PLAIN)
public class InterceptedHelloService {
    @GET
    public String call(){
        return System.currentTimeMillis() + " ";
    }    
}

is intercepted with two, empty, interceptors:

public class AnotherInterceptor {
    @AroundInvoke
    public Object intercept(InvocationContext ic) throws Exception{
        return ic.proceed();
    }
}

public class LoggingInterceptor {
    @AroundInvoke
    public Object intercept(InvocationContext ic) throws Exception{
        return ic.proceed();
    }
}


The execution overhead is compared to a plain EJB 3.1 without any interception:

@Path("plain")
@Stateless
@Produces(MediaType.TEXT_PLAIN)
public class PlainHelloService {
    @GET
    public String call(){
        return System.currentTimeMillis() + " ";
    }
}

…and the results are:
TypeAverageMinMaxTransactions/Sec
Plain EJB 3.11082833.66
Intercepted EJB 3.11092818.88

Even applying two interceptors on a @Stateless EJB 3.1 does not make a significant performance impact. In worst case the performance difference is 1 ms and so negligible.

The tests were performed with JDK 1.6.0_29-b11 and Glassfishv3.1.1 without any modifications.The project InterceptorOverhead together with JMeter load script was pushed into: http://kenai.com/projects/javaee-patterns/