zoukankan      html  css  js  c++  java
  • ActriveMQ-消息重新传递和死信队列(DLQ)处理

    一、消息什么时候会被重新传递

      1、在一个事物性回话中,调用了rollback();

      2、在调用commit()之前,事务已经关闭

      3、回话正在使用ACK确认时,Session.recover()被调用

      4、客户端连接超时(可能正在执行的业务代码所需要的时间比配置超时时间要长)

    二、重传策略设置

      activeMq有自个的重传机制,当然客户端可以自己设置重传机制。

    RedeliveryPolicy policy = connection.getRedeliveryPolicy();
    policy.setInitialRedeliveryDelay(500);
    policy.setBackOffMultiplier(2);
    policy.setUseExponentialBackOff(true);
    policy.setMaximumRedeliveries(2);

      一旦消息重传次数超过配置的次数,Mq节点就会把这条消息发送到一个DLQ(死信)队列中。

       ActiveMQ.DLQ是AcvtiveMQ默认的DLQ,所有的不可再重传的的消息都会被发送到这个队列,那么就会显得比较难管理。

      所以,你可以通过activemq.xml配置文件,给特定的队列的DLQ配置前缀,这样就能所有的队列都能拥有自己的DLQ。

      官网的配置例子如下:

    <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>

    三、过期消息自动丢弃

      有些人可能更希望过期的消息能够被直接丢弃而不是进入DLQ。当然这对于DLQ的管理也变得更简单,而不用查找问题的时候在大量的过期的消息中进行排查。

      让ActiveMq丢弃过期消息,可以通过配置  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>

    四,将非持久的消息放入死信队列

      默认情况下,ActiveMQ默认情况下持久消息过期都会被送到DLQ,不会把非持久性的消息放入DLQ的。主要原因是,既然应用程序都不关心消息是否持久化,那么就没有必要是否关注这些非持久化的消息在重传失败时,是否要考虑放入DLQ。

      如果想要配置将非持久的消息放入死信队列,可以设置 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>

    五、设置DLQ中消息的过期时间

      默认情况,ActiveMQ对于DLQ的消息是不会过期的。然而,从5.12版本之后,deadLetterStrategy 支持一个过期时间的属性配置,单位是毫秒

      注意的是:

        1、在是否要设置消息过期的这个事情上是有选择性的。特别是不要通过在默认或包含通配符策略项上设置过期时间来将过期应用于DLQ目标。

        2、如果一个DLQ的消息过期了被转发到同一个队列或者另外一个过期队列,则会进入一个循环,如果策略审核被禁用或超出了它的滑动窗口,则可能会出现问题。

    <broker>
      
      <destinationPolicy>
       <policyMap>
         <policyEntries>
           <policyEntry queue="QueueWhereItIsOkToExpireDLQEntries">
             <deadLetterStrategy>
               <.... expiration="300000"/>
             </deadLetterStrategy>
           </policyEntry>
         </policyEntries>
       </policyMap>
      </destinationPolicy>
      
    </broker>

      消息审核

      死信策略有一个默认启用的消息审核。这可以防止重复的消息被添加到配置的DLQ中。从5.15.0开始,可以通过maxProducersToAudit和maxAuditDepth属性。可以使用enableAudit=“false”禁用审核

  • 相关阅读:
    Web开发快速上手
    前端概述
    Python语言进阶
    图像和办公文档处理
    网络编程
    进程和线程
    正则表达式
    面向对象进阶
    面向对象
    js 获取指定时间上月26 ,
  • 原文地址:https://www.cnblogs.com/HendSame-JMZ/p/13110172.html
Copyright © 2011-2022 走看看