ActiveMQ的消息重發策略和DLQ處理

在如下三種狀況中,ActiveMQ消息會被重發給客戶端/消費者:
1.使用一個事務session,而且調用了rollback()方法;
2.一個事務session,關閉以前調用了commit;
3.在session中使用CLIENT_ACKNOWLEDGE簽收模式,而且調用了Session.recover()方法。

Broker根據本身的規則,經過BrokerInfo命令包和客戶端創建鏈接,向客戶端傳送缺省發送策略。可是客戶端可使用ActiveMQConnection.getRedeliveryPolicy()方法覆蓋override這個策略設置。
RedeliveryPolicy policy = connection.getRedeliveryPolicy();
policy.setInitialRedeliveryDelay(500);
policy.setBackOffMultiplier(2);
policy.setUseExponentialBackOff(true);
policy.setMaximumRedeliveries(2);



一旦消息重發嘗試超太重發策略中配置的maximumRedeliveries(缺省爲6次)時,會給broker發送一個"Poison ack",通知它,這個消息被認爲是一個毒丸(a poison pill),接着broker會將這個消息發送到DLQ(Dead Letter Queue),以便後續分析處理。

缺省死信隊列(Dead Letter Queue)叫作ActiveMQ.DLQ;全部的未送達消息都會被髮送到這個隊列,以至會很是難於管理。你能夠設置activemq.xml文件中的destination policy map的"individualDeadLetterStrategy"屬性來修改。
<broker...>
  <destinationPolicy>
    <policyMap>
      <policyEntries>
        <!-- Set the following policy on all queues using the '>' wildcard -->
        <policyEntry queue=">">
          <deadLetterStrategy>
            <!--
              Use the prefix 'DLQ.' for the destination name, and make
              the DLQ a queue rather than a topic
            -->
            <individualDeadLetterStrategy
              queuePrefix="DLQ." useQueueForQueueMessages="true" />
          </deadLetterStrategy>
        </policyEntry>
      </policyEntries>
    </policyMap>
  </destinationPolicy>
  ...
</broker>



自動丟棄過時消息(Expired Messages)
一些應用可能只是簡單的丟棄過時消息,而不想將它們放到DLQ中,徹底跳過了DLQ。在dead letter strategy死信策略上配置processExpired屬性爲false,能夠實現這個功能。
<broker...>
  <destinationPolicy>
   <policyMap>
     <policyEntries>
       <!-- Set the following policy on all queues using the '>' wildcard -->
       <policyEntry queue=">">
         <!--
           Tell the dead letter strategy not to process expired messages
           so that they will just be discarded instead of being sent to
           the DLQ
         -->
         <deadLetterStrategy>
           <sharedDeadLetterStrategy processExpired="false" />
         </deadLetterStrategy>
       </policyEntry>
     </policyEntries>
   </policyMap>
  </destinationPolicy>
...
</broker>



將非持久消息(non-persistent messages)放入死信隊列
ActiveMQ缺省不會將未發到的非持久消息放入死信隊列。若是一個應用程序並不想將消息message設置爲持久的,那麼記錄下來那些未發送到的消息對它來講每每也是沒有價值的。不過若是想實現這個功能,能夠在dead-letter strategy死信策略上設置processNonPersistent="true"
<broker...>
  <destinationPolicy>
   <policyMap>
     <policyEntries>
       <!-- Set the following policy on all queues using the '>' wildcard -->
       <policyEntry queue=">">
         <!--
           Tell the dead letter strategy to also place non-persisted messages
           onto the dead-letter queue if they can't be delivered.
         -->
         <deadLetterStrategy>
           <sharedDeadLetterStrategy processNonPersistent="true" />
         </deadLetterStrategy>
       </policyEntry>
     </policyEntries>
   </policyMap>
  </destinationPolicy>
...
</broker>
相關文章
相關標籤/搜索