ActiveMQ 5.7.0 redeliveryPolicy未应用

时间:2012-12-12 19:59:48

标签: java spring jms activemq

我正在使用ActiveMQ 5.7.0并尝试实施重新传递策略。我有两个队列需要设置重新传递策略。但是在测试中,它不适用我的政策。它不是按照下面的配置,而是以1秒的间隔重试7次(不是我指定的)。

  <!--  ActiveMQ destinations to use  -->
  <amq:queue id="destinationOne" physicalName="${activemq.one.queuename}">
    <amq:properties>
        <amq:redeliveryPolicy destination="One.DLQ" maximumRedeliveries="5" initialRedeliveryDelay="300000" useExponentialBackOff="true" backOffMultiplier="2" maximumRedeliveryDelay="3600000"/>
    </amq:properties>
  </amq:queue>
  <amq:queue id="destinationTwo" physicalName="${activemq.two.queuename}">
    <amq:properties>
        <amq:redeliveryPolicy destination="Two.DLQ" maximumRedeliveries="5" initialRedeliveryDelay="300000" useExponentialBackOff="true" backOffMultiplier="2" maximumRedeliveryDelay="3600000"/>
    </amq:properties>
  </amq:queue>

我定义了两个侦听器,他们正在应用如下队列:

  <bean id="oneMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
    <property name="destination" ref="destinationOne"/>
    <property name="messageListener" ref="jmsOneListener" />
    <property name="autoStartup" value="false" />    
    <property name="sessionTransacted" value="true"/>
    <property name="concurrentConsumers" value="2" />
  </bean>
  <bean id="twoMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
    <property name="destination" ref="destinationTwo"/>
    <property name="messageListener" ref="jmsTwoListener" />
    <property name="autoStartup" value="false" />    
    <property name="sessionTransacted" value="true"/>
    <property name="concurrentConsumers" value="2" />
  </bean>

4 个答案:

答案 0 :(得分:4)

我同意Tim的回答:策略是在底层连接工厂对象上定义的。对于您的场景,我认为您需要使用自己的策略定义2个单独的ActiveMQ连接工厂,然后为每个工厂定义一个单独的Spring连接工厂,然后将其正确使用

这是我之前在ActiveMQ v5.5上使用过的一个例子:

<bean id="rawConnectionFactory1" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="${broker.url}" />
    <property name="redeliveryPolicy" ref="policy1" />
    <property name="useCompression" value="true" />
</bean>
<bean id="rawConnectionFactory2" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="${broker.url}" />
    <property name="redeliveryPolicy" ref="policy2" />
    <property name="useCompression" value="true" />
</bean>


<bean id="policy1" class="org.apache.activemq.RedeliveryPolicy">
    <property name="initialRedeliveryDelay" value="20000" />
    <property name="useExponentialBackOff" value="false" />
    <property name="useCollisionAvoidance" value="false" />
    <property name="maximumRedeliveries" value="0" />
</bean>
<bean id="policy2" class="org.apache.activemq.RedeliveryPolicy">
    <property name="initialRedeliveryDelay" value="60000" />
    <property name="useExponentialBackOff" value="false" />
    <property name="useCollisionAvoidance" value="false" />
    <property name="maximumRedeliveries" value="5" />
</bean>


<bean id="connectionFactory1" class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory" ref="rawConnectionFactory1" />
    <property name="sessionCacheSize" value="30" />
</bean>
<bean id="connectionFactory2" class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory" ref="rawConnectionFactory2" />
    <property name="sessionCacheSize" value="10" />
</bean>

答案 1 :(得分:3)

通过ActiveMQConnectionFactory RedeliveryPolicy属性设置在MessageConsumers上设置重新传递策略实例。我不确定你的配置是什么,但它看起来不正确。

答案 2 :(得分:2)

经过几次重试,我得到了一个使用amq命名空间的工作版本:

<amq:connectionFactory brokerURL="..." id="connectionFactory">
    <amq:redeliveryPolicyMap>
        <amq:redeliveryPolicyMap><!-- Don't know why a double nesting -->
            <amq:defaultEntry>
                <!-- default policy, 5 times with 10s delay each -->
                <amq:redeliveryPolicy maximumRedeliveries="5" initialRedeliveryDelay="10000" />
            </amq:defaultEntry>
            <amq:redeliveryPolicyEntries>
                <!-- three times with exponential back-off, that is, 1s, 2s, 4s, 8s. "queue" references to the "physicalName" defined in amq:queue -->
                <amq:redeliveryPolicy queue="queue.thread.autopay" maximumRedeliveries="3" initialRedeliveryDelay="1000" backOffMultiplier="2" useExponentialBackOff="true" />
                <!-- another policy mapping -->
                <amq:redeliveryPolicy queue="queue.thread.doc" maximumRedeliveries="2" initialRedeliveryDelay="4000" />
            </amq:redeliveryPolicyEntries>
        </amq:redeliveryPolicyMap>
    </amq:redeliveryPolicyMap>
</amq:connectionFactory>

答案 3 :(得分:0)

与Jay的答案相同,但是当我使用${}

的属性时amq命名空间对我来说没有用。
<spring:bean name="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <spring:property name="brokerURL" value="${activemq.broker.url}"/>
        <spring:property name="userName" value="${activemq.username}"/>
        <spring:property name="password" value="${activemq.passwd}"/>
        <spring:property name="redeliveryPolicyMap">
            <spring:bean id="redeliveryPolicyMap" class="org.apache.activemq.broker.region.policy.RedeliveryPolicyMap">
                <spring:property name="defaultEntry">                  
                <spring:bean class="org.apache.activemq.RedeliveryPolicy"/>
                </spring:property>
                <spring:property name="redeliveryPolicyEntries">
                    <spring:list>
                        <spring:bean class="org.apache.activemq.RedeliveryPolicy">        
                            <spring:property name="maximumRedeliveries" value="${max.redelivery}"/>
                            <spring:property name="redeliveryDelay" value="${retry.delay}"/>
                            <spring:property name="initialRedeliveryDelay" value="${retry.delay.initial}"/>
                            <spring:property name="queue" value="${my.queue}"/>
                        </spring:bean>
                    </spring:list>
                </spring:property>
            </spring:bean>
        </spring:property>
    </spring:bean>