MessageDriverBean - 重试机制
MessageDriverBean - Retry mechanism
我一直在阅读 SO 和其他一些 google 结果,我感到困惑,有人说我必须调用 context.setRollbackOnly();
其他人说这不是必需的,因为 MDB 会这样做一个人
所以,我有一个 MessageDrivenBean
class 从 JMS Queue
.
接收消息
@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 是否会将 message
(或我所说的事件)放回队列中,或者我有在每次捕获时调用 context.setRollbackOnly();
将消息放回?
我是否必须在每次捕获时调用 context.setRollbackOnly() 才能放回消息?
这取决于我们,如果您想回滚所有异常的消息,是的。
如果是 bad/poison 消息,没有回滚事务的意义,最好通过记录异常和消息的有效负载来丢弃。
对于您的第一次查询,如果onMessage出现异常,消息是否会被放回Queue,请参考以下几点:
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/
我一直在阅读 SO 和其他一些 google 结果,我感到困惑,有人说我必须调用 context.setRollbackOnly();
其他人说这不是必需的,因为 MDB 会这样做一个人
所以,我有一个 MessageDrivenBean
class 从 JMS Queue
.
@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 是否会将 message
(或我所说的事件)放回队列中,或者我有在每次捕获时调用 context.setRollbackOnly();
将消息放回?
我是否必须在每次捕获时调用 context.setRollbackOnly() 才能放回消息?
这取决于我们,如果您想回滚所有异常的消息,是的。 如果是 bad/poison 消息,没有回滚事务的意义,最好通过记录异常和消息的有效负载来丢弃。
对于您的第一次查询,如果onMessage出现异常,消息是否会被放回Queue,请参考以下几点:
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/