【问题标题】:JMS - Message redlivery on failJMS - 失败时重新传递消息
【发布时间】:2016-05-13 21:30:03
【问题描述】:

我有这个场景:

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

目前,我的行为是失败的消息立即重新传递 10 次,我无法自定义。

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

【问题讨论】:

    标签: java jakarta-ee jms wildfly-10


    【解决方案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);
    

    【讨论】:

    • This 也可能有帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 2013-06-22
    • 2020-03-29
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2011-07-01
    相关资源
    最近更新 更多