SpringCloud 整合在一起

2023-11-29 16:03 更新

以下配置創(chuàng)建一個交換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=
---

此配置將創(chuàng)建一個綁定到直接交換(DLX)的DLQ,其路由密鑰為myDestination.consumerGroup。當(dāng)郵件被拒絕時,它們將被路由到DLQ。5秒后,該消息到期,并通過使用隊列名稱作為路由鍵將其路由到原始隊列,如以下示例所示:

Spring Boot應(yīng)用程序。 

@SpringBootApplication
@EnableBinding(Sink.class)
public class XDeathApplication {

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

    @StreamListener(Sink.INPUT)
    public void listen(String in, @Header(name = "x-death", required = false) Map<?,?> 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標(biāo)頭中的count屬性是Long。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號