重试相同消息但未发送给dlq

时间:2018-10-12 14:30:52

标签: java rabbitmq spring-rabbitmq

我已经定义了一个队列blah.queue,并为其定义了死信路由键。但是,当我收到带有失败的异常失败消息时,该消息将重新排队并重新尝试(在无限循环中)。 我希望在重试3次之后,该消息应该与死信路由键一起放在交换机上。但这似乎没有发生。

我有以下设置:

    <rabbit:queue name="blah.queue" auto-delete="false" durable="true">
    <rabbit:queue-arguments>
        <entry key="x-dead-letter-exchange" value="" />
        <entry key="x-dead-letter-routing-key" value="blah.queue.dlq.route" />
        <entry key="x-ha-policy" value="all" />
    </rabbit:queue-arguments>
</rabbit:queue>
<rabbit:direct-exchange name="${rabbit.idesk.exchange}">
    <rabbit:bindings>
        <rabbit:binding queue="blah.queue" key="blah.route" />
    </rabbit:bindings>
</rabbit:direct-exchange>

<bean id="myConsumer" class="com.ankit.CustomConsumer" />

<bean id="myConsumerMessageListenerAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
    <constructor-arg ref="myConsumer" />
    <constructor-arg ref="myMessageConverter" />
</bean>

<bean id="myConsumerMessageListenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
    <property name="queueNames" value="blah.queue" />
    <property name="connectionFactory" ref="queueConnectionFactory" />
    <property name="messageListener" ref="myConsumerMessageListenerAdapter" />
    <property name="errorHandler" ref="loggingErrorHandler" />
    <property name="adviceChain">
        <list>
            <ref bean="retryAdvice" />
        </list>
    </property>
</bean>

<bean id="loggingErrorHandler" class="org.springframework.scheduling.support.TaskUtils.LoggingErrorHandler" />

<bean id="myMessageConverter"
    class="org.springframework.amqp.support.converter.JsonMessageConverter">
    <property name="classMapper">
        <bean class="com.ankit.queue.mapper.NamedClassMapper">
            <constructor-arg
                value="com.ankit.dto.EventDTO" />
        </bean>
    </property>
    <property name="createMessageIds" value="true" />
</bean>

<bean id="retryAdvice" class="org.springframework.amqp.rabbit.config.StatefulRetryOperationsInterceptorFactoryBean">
    <property name="messageRecoverer" ref="rejectAndDontRequeueRecoverer" />
    <property name="retryOperations" ref="retryTemplate" />
</bean>

<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
    <property name="retryPolicy" ref="simpleRetryPolicy" />
    <property name="backOffPolicy">
        <bean class="org.springframework.retry.backoff.FixedBackOffPolicy">
            <property name="backOffPeriod" value="5000" />
        </bean>
    </property>
</bean>

<bean id="simpleRetryPolicy" class="org.springframework.retry.policy.SimpleRetryPolicy">
    <property name="maxAttempts" value="3" />
</bean>

1 个答案:

答案 0 :(得分:0)

您没有看到恢复器发出的WARN消息吗?

@Override
public void recover(Message message, Throwable cause) {
    if (this.logger.isWarnEnabled()) {
        this.logger.warn("Retries exhausted for message " + message, cause);
    }
    throw new ListenerExecutionFailedException("Retry Policy Exhausted",
                new AmqpRejectAndDontRequeueException(cause), message);
}

打开调试日志记录以查看重试行为。