0

我有这种情况:

  • 通过 MDB 制定 JMS 消息可能会失败(RuntimeException在这种情况下抛出 a)
  • 应该重新传递消息,尽管在延迟之后(理想,但并非绝对必要:根据失败次数增加延迟之后)
  • 在 x 次失败后,该消息应该被忽略并且不再发送

现在,我的行为是失败的消息立即重新传递了 10 次,而我无法自定义它。

有没有办法可以通过@JMSdefinition(或其他注释)或在消息中设置正确的属性来实现这一点?如果是这样,怎么办?

4

1 回答 1

1

您可以使用_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);
于 2016-05-13T13:30:03.280 回答