JMS - 失败时的邮件重新传递

时间:2016-05-13 09:10:04

标签: java java-ee jms wildfly-10

我有这种情况:

  • 通过MDB进行的JMS消息详细说明可能会失败(在这种情况下会抛出RuntimeException
  • 该消息应该重新传递,但是在延迟之后(理想但非严格必要:根据失败次数增加延迟后)
  • 在x失败后,该消息应该被忽略,永远不会再发送

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

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

1 个答案:

答案 0 :(得分: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);
相关问题