Retry With the RabbitMQ Binder

在 binder 中启用重试时,侦听器容器线程将暂停以进行配置的任何后退时间段。当使用单个消费者需要严格排序时,这可能很重要。然而,对于其他用例,它阻止其他消息在那条线程上进行处理。使用 binder 重试的替代方法是设置死信转发,在死信队列 (DLQ) 上使用生存时间以及在 DLQ 本身上设置死信配置。有关此处讨论属性的更多信息,请参见 “RabbitMQ Binder Properties”。您可以使用以下示例配置来启用此功能:

When retry is enabled within the binder, the listener container thread is suspended for any back off periods that are configured. This might be important when strict ordering is required with a single consumer. However, for other use cases, it prevents other messages from being processed on that thread. An alternative to using binder retry is to set up dead lettering with time to live on the dead-letter queue (DLQ) as well as dead-letter configuration on the DLQ itself. See “RabbitMQ Binder Properties” for more information about the properties discussed here. You can use the following example configuration to enable this feature:

  • Set autoBindDlq to true. The binder create a DLQ. Optionally, you can specify a name in deadLetterQueueName.

  • Set dlqTtl to the back off time you want to wait between redeliveries.

  • Set the dlqDeadLetterExchange to the default exchange. Expired messages from the DLQ are routed to the original queue, because the default deadLetterRoutingKey is the queue name (destination.group). Setting to the default exchange is achieved by setting the property with no value, as shown in the next example.

若要强制使消息变为死信,请抛出 AmqpRejectAndDontRequeueException 或将 requeueRejected 设置为 false(默认值),并抛出任何异常。

To force a message to be dead-lettered, either throw an AmqpRejectAndDontRequeueException or set requeueRejected to false (the default) and throw any exception.

该循环将无休止地继续下去,这对于瞬态问题来说很好,但您可能希望在尝试多次后放弃。幸运的是,RabbitMQ 提供了 x-death 标头,让您确定已发生的循环数。

The loop continue without end, which is fine for transient problems, but you may want to give up after some number of attempts. Fortunately, RabbitMQ provides the x-death header, which lets you determine how many cycles have occurred.

若要在放弃后确认消息,请抛出 ImmediateAcknowledgeAmqpException

To acknowledge a message after giving up, throw an ImmediateAcknowledgeAmqpException.