2

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?

MaDa
  • 10,511
  • 9
  • 46
  • 84
Synesso
  • 37,610
  • 35
  • 136
  • 207

2 Answers2

7

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.

Pieter Kuijpers
  • 7,247
  • 5
  • 28
  • 36
2

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).

Synesso
  • 37,610
  • 35
  • 136
  • 207