Synesso
Synesso

Reputation: 39018

How to enable MessageDrivenContext injection?

I'd like to explicity set a transaction to rollback in a JavaEE MDB:

private MessageDrivenContext context;
@MessageDriven(mappedName = "jms/ReaderQueue", activationConfig =  {
        @ActivationConfigProperty(
            propertyName = "acknowledgeMode", 
            propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(
            propertyName = "destinationType", 
            propertyValue = "javax.jms.Queue")
    })
public class MessageReaderBean implements MessageListener {
    public void onMessage(Message message) {
        ctx.setRollbackOnly(); // <-- see here, my good fellow!
    }
    public void setMessageDrivenContext(MessageDrivenContext ctx) throws EJBException {
        this.context = ctx;
    }
}

However the container does not call setMessageDrivenContext for me and I get a NullPointerException. What magic sauce do I need to get the context injected?

Upvotes: 2

Views: 6550

Answers (2)

Pieter Kuijpers
Pieter Kuijpers

Reputation: 7307

You should annotate the MessageDrivenBeanContext with @Resource:

@Resource private MessageDrivenContext context;

Then the context will be injected by the container. You don't need the setMessageDrivenContext method.

Upvotes: 7

Synesso
Synesso

Reputation: 39018

I needed to also implement javax.ejb.MessageDrivenBean before it would recognise that callback method. (Even though it was functioning as a legitimate MDB without that interface).

Upvotes: 2

Related Questions