在 Mdb 中,您可以告诉代理不要通过“MessageDrivenContext”删除消息。(不抛出异常 - 这也会回滚事务)
一些示例代码,您可以在其中处理方法“onMessaage”中的所有异常并在出现异常时设置回滚:
@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();
}
}
}