Creating an interceptor which logs every entry/method call on an EJB

Submitted by Jochus on Tue, 11/08/2015 - 22:39 | Posted in: Java

I needed to log all the calls to all methods made to a certain EJB (because I wanted to know which methods were called from which place). The reason was locks I received on a stateless session bean. I could use a debugger, but because I'm talking about a lot of functions and a lot of calls, I created a LogInterceptor:

package be.jochenhebbrecht.ejb.interceptor;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class LogInterceptor{
	public Object intercept(InvocationContext context) throws Exception {
                // Log to console before executing method
		System.out.println("Entering method:" + context.getMethod().getName());
                // Execute method
		Object result = context.proceed();
                // Log to console after executing method
		System.out.println("Leaving method: " + context.getMethod().getName() );
		return result;

To use the interceptor, simply add the following code

package be.jochenhebbrecht.ejb;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import be.jochenhebbrecht.ejb.interceptor.LogInterceptor;
public class SomeStatelessEJB{
	public String foo(String bar) {
            // Do something ...

Add new comment

The content of this field is kept private and will not be shown publicly.


  • Lines and paragraphs break automatically.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.
  • Web page addresses and email addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <cpp>, <css>, <html5>, <java>, <javascript>, <php>, <sql>, <xml>. The supported tag styles are: <foo>, [foo].
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.