JMS - 失败时重新传递消息

JMS - Message redlivery on fail

我有这个场景:

现在,我的行为是失败的消息立即重新传递 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);