Q:

Filter Class for Message Driven Bean's

I have a requirement like below:
We have 20 Message Driven Beans in our application, when ever message has been arrived for each bean I need to log that information to database about the message. I can add this to each bean, but I need to change each and every class for each.
Is there a way where we can add some Filter class which will be fired before execution of MDB onMessage method. So that, I can have one class for logging all the MDB messages.

A:

In general you should use some AOP technique. In detail, EJB provides an easy way to apply interceptors on MDBs:
public class LoggingInterceptor {
    ...
    @AroundInvoke
    protected Object myInterceptor(InvocationContext ctx) throws Exception {
        //do logging here...
        return ctx.proceed();
    }
}

@Interceptors(LoggingInterceptor.class)
public class SomeBean implements MessageListener {
    public void onMessage(Message message) {
        //....
    }
}

Example taken from Configuring an Interceptor Class for an EJB 3.0 MDB.
To address your question from comment: you have access to InvocationContext inside an interceptor which exposes all required attributes:
String class = ctx.getMethod().getDeclaringClass().getName();
Message msg = (Message)ctx.getParameters()[0];

Note that you can even alter the parameter or use a different one in interceptor.
You tagged your message with ejb-3.0 and spring. In Spring AOP options are much more flexible, but the general idea still applies.

