我一直在阅读 SO 和其他一些谷歌结果,我很困惑,有人说我必须调用 context.setRollbackOnly();
其他人说这不是必需的,因为 MDB 会自己做。
因此,我有一个 MessageDrivenBean
类,它从 JMS 队列
接收消息。
@MessageDriven(name = "MyEventReceiverJMS", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/TheQueue"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
})
public class MyEventReceiverJMS implements MessageListener {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Resource
private MessageDrivenContext context;
@Override
public void onMessage(Message message) {
try {
// Some logic goes here
}
catch (JMSException ex) {
logger.error("JMSException|could not retrieve object from the message body - reason: {}", ex.getMessage());
context.setRollbackOnly();
}
catch (JSONException ex) {
logger.error("error while creating the JSON - reason: ", ex.getMessage());
context.setRollbackOnly();
}
catch (IOException ex) {
logger.error("could not communicate with the server - reason: {}", ex.getMessage());
context.setRollbackOnly();
}
}
}
我的问题是,如果onMessage
出现异常,消息
(或者我称之为事件)是否会被MDB放回队列或者我是否必须在每次捕获时调用 context.setRollbackOnly();
才能将消息放回?
最佳答案
我是否必须在每次捕获时调用 context.setRollbackOnly() 才能将消息放回?
这取决于我们,如果您想回滚所有异常的消息,是的。 如果它是坏消息/有毒消息,则没有回滚事务的意义,最好通过记录异常和消息的有效负载来丢弃。
对于您的第一个查询,如果 onMessage 出现异常,消息是否会被放回到队列中,请查找以下几点:
The JMS Server could redeliver messages because of:
A java.lang.Error or java.lang.RuntimeException has been thrown from the Receiver/MDB’s onMessage method
User has made a call to ejbcontext.setRollbackOnly() in his MDB’s onMessage method (this applies to Container Managed Transaction only)
MDB participating in a Transaction failed for some reason.
以下帖子非常好,您可以引用以了解更多详细信息:
http://weblogic-wonders.com/weblogic/2011/01/10/working-with-jms-and-the-standard-issues-in-jms/
关于java - MessageDriverBean - 重试机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32767374/