JMS - 失败时重新传递消息
JMS - Message redlivery on fail
我有这个场景:
- 通过 MDB 的 JMS 消息阐述可能会失败(在这种情况下抛出
RuntimeException
)
- 消息应该被重新传送,尽管在延迟之后(理想的,但不是绝对必要的:在根据失败次数增加延迟之后)
- 在 x 次失败后,该消息应被忽略,不再发送
现在,我的行为是失败的消息立即重新传递 10 次,我无法自定义它。
有没有办法通过 @JMSdefinition
(或其他注释)或在消息中设置正确的属性来实现此目的?如果可以,怎么办?
您可以使用 _AMQ_SCHED_DELIVERY 安排消息 属性:
Queue q = (Queue) ServiceLocator.getInstance().getDestination("QUEUE");
QueueConnectionFactory factory = (QueueConnectionFactory) ServiceLocator.getInstance().getConnectionFactory(
"java:/ConnectionFactory");
QueueConnection connection = factory.createQueueConnection();
QueueSession session = null;
QueueSender sender = null;
session = connection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
sender = session.createSender(q);
ObjectMessage msg = session.createObjectMessage();
if (redelivedCount > 0) {
msg.setIntProperty("redelivedCount", redelivedCount);
// schedule to run in 10 secs
msg.setLongProperty("_AMQ_SCHED_DELIVERY", System.currentTimeMillis() + 10000);
}
msg.setStringProperty("action", action);
msg.setObject(params);
sender.send(msg);
我有这个场景:
- 通过 MDB 的 JMS 消息阐述可能会失败(在这种情况下抛出
RuntimeException
) - 消息应该被重新传送,尽管在延迟之后(理想的,但不是绝对必要的:在根据失败次数增加延迟之后)
- 在 x 次失败后,该消息应被忽略,不再发送
现在,我的行为是失败的消息立即重新传递 10 次,我无法自定义它。
有没有办法通过 @JMSdefinition
(或其他注释)或在消息中设置正确的属性来实现此目的?如果可以,怎么办?
您可以使用 _AMQ_SCHED_DELIVERY 安排消息 属性:
Queue q = (Queue) ServiceLocator.getInstance().getDestination("QUEUE");
QueueConnectionFactory factory = (QueueConnectionFactory) ServiceLocator.getInstance().getConnectionFactory(
"java:/ConnectionFactory");
QueueConnection connection = factory.createQueueConnection();
QueueSession session = null;
QueueSender sender = null;
session = connection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
sender = session.createSender(q);
ObjectMessage msg = session.createObjectMessage();
if (redelivedCount > 0) {
msg.setIntProperty("redelivedCount", redelivedCount);
// schedule to run in 10 secs
msg.setLongProperty("_AMQ_SCHED_DELIVERY", System.currentTimeMillis() + 10000);
}
msg.setStringProperty("action", action);
msg.setObject(params);
sender.send(msg);