2016-05-13 167 views
0

我有这样的场景:JMS - 消息redlivery上失败

  • 通过MDB JMS消息的阐述可能会失败(在这种情况下抛出一个RuntimeException
  • 消息应该交还,但后延迟(理想的,但不是绝对必要的:这取决于数量增大延迟失败后)
  • 后X失败,该消息应该被忽视,并且不会再发送

右ñ我的行为是,失败的消息被立即重新发送10次,我一直无法自定义这一点。

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

回答

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); 
+0

[这](http://activemq.apache.org/delay-and-schedule-message-delivery.html)也帮助。 – fhofmann

+0

谢谢,作品像魅力! –