1

Given the following Message Driven Bean, is it possible to define a global decorator from the CDI specification to add additional behaviour?

@MessageDriven
public class MyMessageDrivenBean implements MessageListener {

    @Override
    public void onMessage(Message m) {

    }
}

The decorator looks like this:

@Decorator
@Priority(Interceptor.Priority.APPLICATION)
public abstract DecorateMyMessageDrivenBean implements MessageListener {

    @Inject
    @Delegate
    @Any
    private MessageListener delegate;

    @Override
    public void onMessage(Message m) {

    }
}

Currently, the decorator is not being executed. I have added a beans.xml file to my module.

Glains
  • 2,773
  • 3
  • 16
  • 30

1 Answers1

1

The short answer (but don't lose hope) is no, because a @MessageDrivenBean is not a CDI managed bean, and only CDI managed beans can be decorators.

Now, what you might be able to do (and I don't have experience doing this myself) is something like this:

  • use @javax.annotation.Resource to call for a MessageListener to be injected into a field by Java EE, not by CDI (so it will be looked up in JNDI)
  • write a producer method that @Produces MessageListener instances by using a combination of that field's contents and the createInterceptionFactory method
  • @Inject the MessageListener so produced wherever you want to use it

An InterceptionFactory is really the only mechanism you have to dynamically add interception (and a decorator is just a very special case of interception) to Things That Are Not CDI Managed Beans.

Finally, this will only work if you use CDI 2.0 (which is in Java EE 8 or higher).

Laird Nelson
  • 15,321
  • 19
  • 73
  • 127