开发者

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.jm开发者_如何学Gos.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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜