3

我正在使用 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

4 回答 4

4

我同意蒂姆的回答:策略是在底层连接工厂对象上定义的。对于您的场景,我认为您需要使用自己的策略定义 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>
于 2012-12-18T08:54:59.820 回答
3

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

于 2012-12-13T23:51:18.017 回答
3

经过几次重试,我得到了一个使用 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>
于 2013-12-04T11:35:56.307 回答
2

与杰伊的回答相同,但是当我使用属性时,将香草弹簧配置作为 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> 
于 2017-09-08T12:50:11.783 回答