ActiveMQ 重发机制(消息发送失败后的重新发送)

一、重新传递消息的情况

ActiveMQ在接收消息的Client有以下几种操作的时候,需要重新传递消息:

 1:Client用了transactions(事务),且在session中调用了rollback()

 2:Client用了transactions,且在调用commit()之前关闭

 3:Client在CLIENT_ACKNOWLEDGE的传递模式下,在session中调用了recover()

二、写RedeliveryPolicy配置文件

<!-- 定义ReDelivery(重发机制)机制 ,重发时间间隔是100毫秒,最大重发次数是3次 -->  
    <bean id="activeMQRedeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">  
        <!--是否在每次尝试重新发送失败后,增长这个等待时间 -->  
        <property name="useExponentialBackOff" value="true"></property>  
        <!--重发次数,默认为6次   这里设置为1次 -->  
        <property name="maximumRedeliveries" value="1"></property>  
        <!--重发时间间隔,默认为1秒 -->  
        <property name="initialRedeliveryDelay" value="1000"></property>  
        <!--第一次失败后重新发送之前等待500毫秒,第二次失败再等待500 * 2毫秒,这里的2就是value -->  
        <property name="backOffMultiplier" value="2"></property>  
        <!--最大传送延迟,只在useExponentialBackOff为true时有效(V5.5),假设首次重连间隔为10ms,倍数为2,那么第   
            二次重连时间间隔为 20ms,第三次重连时间间隔为40ms,当重连时间间隔大的最大重连时间间隔时,以后每次重连时间间隔都为最大重连时间间隔。 -->  
        <property name="maximumRedeliveryDelay" value="1000"></property>  
    </bean>  

 

三、引用RedeliveryPolicy的配置:

<!--创建连接工厂 -->  
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
    <property name="brokerURL" value="tcp://localhost:61616"></property>  
    <property name="redeliveryPolicy" ref="activeMQRedeliveryPolicy" />  <!-- 引用重发机制 -->  
</bean>  

 

posted @ 2018-04-23 09:13  技术宅home  阅读(6252)  评论(0编辑  收藏  举报