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).
精彩评论