Reputation: 1045
In the JMS documentation I read that Message Driven Beans doesn't support CLIENT_ACKNOWLEDGE
mode, only DUPS_OK_ACKNOWLEDGE
and AUTO_ACKNOWLEDGE
.
As I understand it, in AUTO_ACKNOWLEDGE
mode, the message is acknowledged (deleted from the destination) when the onMessage method is invoked. What I want is to tell my broker not to delete messages from the destination (queue or topic) when something bad happens
There must be some way to do this. Anyway, why is CLIENT_ACKNOWLEDGE
not supported in Message Drven Beans.
Upvotes: 3
Views: 4389
Reputation: 518
In Mdb you can tell the broker to not delete the message trough 'MessageDrivenContext'. (Without throwing exception - which also rollbacks the transaction)
Some example code, Where you can handle all exceptions in method 'onMessaage' and set a rollback in case of Exception:
@MessageDriven(name = "queueMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "TestQueue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
}
public class TestMdb implements MessageListener {
@Resource
private MessageDrivenContext messageDrivenContext;
@Override
public void onMessage(Message message) {
try {
// Some usefull code...
}
catch (Exception e) {
messageDrivenContext.setRollbackOnly();
}
}
}
Upvotes: 0
Reputation: 3696
Just make sure you are using (JTA) transactions (the default is that you are using them). Then make sure your applications server has a redelivery setting and or a Dead Letter Queue (DLQ). Any exceptions thrown from your MDB will then trigger a redelivery and after a certain amount of failures the message will be moved to the DLQ.
The redeilvery setting is called "max-delivery-attempts" in Wildfly for instance
Upvotes: 0
Reputation: 32056
What I want is to tell my broker not to delete messages from the destination (queue or topic) when something bad happens.
If you're configured to use PERSISTENT
messages, any exceptions in onMessage()
will persist the message for redelivery based on broker and destination settings. If you're using NON_PERSISTENT
messages, any exception in onMessage()
typically discards the message.
Anyway, why is CLIENT_ACKNOWLEDGE not supported in Message Driven Beans.
Message-driven beans are managed by the J2EE container; as such, the container handles acknowledgements. Typically, only stand-alone JMS receivers should use CLIENT_ACKNOWLEDGE
.
What messaging middleware are you using?
Upvotes: 7