2016-05-13 163 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

謝謝,作品像魅力! –