ActiveMQ消息重发策略和DLQ处理(转载)

文章源地址:http://sharong.iteye.com/blog/1987171

在以下三种情况中,ActiveMQ消息会被重发给客户端/消费者:
1.使用一个事务session,并且调用了rollback()方法;
2.一个事务session,关闭之前调用了commit;
3.在session中使用CLIENT_ACKNOWLEDGE签收模式,并且调用了Session.recover()方法。

Broker根据自己的规则,通过BrokerInfo命令包和客户端建立连接,向客户端传送缺省发送策略。但是客户端可以使用ActiveMQConnection.getRedeliveryPolicy()方法覆盖override这个策略设置。

Java代码  
  1. RedeliveryPolicy policy = connection.getRedeliveryPolicy();  
  2. policy.setInitialRedeliveryDelay(500);  
  3. policy.setBackOffMultiplier(2);  
  4. policy.setUseExponentialBackOff(true);  
  5. 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"属性来修改.
Java代码  
  1. <broker...>  
  2.   <destinationPolicy>  
  3.     <policyMap>  
  4.       <policyEntries>  
  5.         <!-- Set the following policy on all queues using the '>' wildcard -->  
  6.         <policyEntry queue=">">  
  7.           <deadLetterStrategy>  
  8.             <!--  
  9.               Use the prefix 'DLQ.' for the destination name, and make  
  10.               the DLQ a queue rather than a topic  
  11.             -->  
  12.             <individualDeadLetterStrategy  
  13.               queuePrefix="DLQ." useQueueForQueueMessages="true" />  
  14.           </deadLetterStrategy>  
  15.         </policyEntry>  
  16.       </policyEntries>  
  17.     </policyMap>  
  18.   </destinationPolicy>  
  19.   ...  
  20. </broker>  


自动丢弃过期消息(Expired Messages)
一些应用可能只是简单的丢弃过期消息,而不想将它们放到DLQ中,完全跳过了DLQ。在dead letter strategy死信策略上配置processExpired属性为false,可以实现这个功能。
Java代码  
  1. <broker...>  
  2.   <destinationPolicy>  
  3.    <policyMap>  
  4.      <policyEntries>  
  5.        <!-- Set the following policy on all queues using the '>' wildcard -->  
  6.        <policyEntry queue=">">  
  7.          <!--  
  8.            Tell the dead letter strategy not to process expired messages  
  9.            so that they will just be discarded instead of being sent to  
  10.            the DLQ  
  11.          -->  
  12.          <deadLetterStrategy>  
  13.            <sharedDeadLetterStrategy processExpired="false" />  
  14.          </deadLetterStrategy>  
  15.        </policyEntry>  
  16.      </policyEntries>  
  17.    </policyMap>  
  18.   </destinationPolicy>  
  19. ...  
  20. </broker>  


将非持久消息(non-persistent messages)放入死信队列
ActiveMQ缺省不会将未发到的非持久消息放入死信队列。如果一个应用程序并不想将消息message设置为持久的,那么记录下来那些未发送到的消息对它来说往往也是没有价值的。不过如果想实现这个功能,可以在dead-letter strategy死信策略上设置processNonPersistent="true"
Java代码  
  1. <broker...>  
  2.   <destinationPolicy>  
  3.    <policyMap>  
  4.      <policyEntries>  
  5.        <!-- Set the following policy on all queues using the '>' wildcard -->  
  6.        <policyEntry queue=">">  
  7.          <!--  
  8.            Tell the dead letter strategy to also place non-persisted messages  
  9.            onto the dead-letter queue if they can't be delivered.  
  10.          -->  
  11.          <deadLetterStrategy>  
  12.            <sharedDeadLetterStrategy processNonPersistent="true" />  
  13.          </deadLetterStrategy>  
  14.        </policyEntry>  
  15.      </policyEntries>  
  16.    </policyMap>  
  17.   </destinationPolicy>  
  18. ...  
  19. </broker> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值