整合所有内容

以下配置创建一个交换机myDestination,其中队列myDestination.consumerGroup绑定到具有通配符路由键#的主题交换机

---
spring.cloud.stream.bindings.input.destination=myDestination
spring.cloud.stream.bindings.input.group=consumerGroup
#disable binder retries
spring.cloud.stream.bindings.input.consumer.max-attempts=1
#dlx/dlq setup
spring.cloud.stream.rabbit.bindings.input.consumer.auto-bind-dlq=true
spring.cloud.stream.rabbit.bindings.input.consumer.dlq-ttl=5000
spring.cloud.stream.rabbit.bindings.input.consumer.dlq-dead-letter-exchange=
---

此配置创建一个绑定到直接交换机 (DLX) 的 DLQ,路由键为myDestination.consumerGroup。当消息被拒绝时,它们将被路由到 DLQ。5 秒后,消息过期并使用队列名称作为路由键路由到原始队列,如下例所示

Spring Boot 应用程序
@SpringBootApplication
public class XDeathApplication {

    public static void main(String[] args) {
        SpringApplication.run(XDeathApplication.class, args);
    }

    @Bean
    public Consumer<Message<String>> listen() {
        return message -> {
            Map<?,?> death = message.getHeaders().get("x-death");
            if (death != null && death.get("count").equals(3L)) {
                // giving up - don't send to DLX
                throw new ImmediateAcknowledgeAmqpException("Failed after 4 attempts");
            }
            throw new AmqpRejectAndDontRequeueException("failed");
        };
    }

}

请注意,x-death 头中的计数属性是Long类型。