![](/img/trans.png)
[英]How to configure Kafka binded Dead Letter Queue in Spring Cloud Data Flow
[英]Spring Cloud Stream: how to republish to dead letter queue and also throw exception
我正在將使用 Spring AMQP 的項目遷移到使用 Spring Cloud Stream 和 RabbitMQ 的項目。
在我的舊項目中,當使用 @RabbitListener 處理消息時發生某些異常時,會拋出該異常。 如果綁定了死信隊列,則仍會拋出異常(如果有重試,則只拋出一次,我猜是最后一次)。 這對於記錄目的非常有幫助。
如果您定義屬性,則在 Spring Cloud 中,@StreamListener 有一個死信隊列機制:
spring.cloud.stream.bindings.input1.destination=dest1
spring.cloud.stream.rabbit.bindings.input1.consumer.auto-bind-dlq=true
spring.cloud.stream.rabbit.bindings.input1.consumer.republishToDlq=true
但是如果你有這樣的方法(只是一個例子):
@StreamListener("input1")
public void process(String message){
System.out.println("Trying...");
throw new RuntimeException();
}
日志是:
Trying...
Trying...
Trying...
(end of log, no exception thrown)
有沒有辦法拋出異常(僅在最后一次重試中)?
謝謝!
請參閱有關使用者屬性的文檔。
設置...consumer.max-attempts=1
以禁用重試。
您可以處理異常,記錄它,然后拋出 AmqpRejectAndDontRequeueException。 這會將消息發送到死信隊列
您在@StreamListener
下,您希望異常去哪里? 誰是抓住它?
你可以這樣做:
@StreamListener("input1")
public void process(String message){
try {
System.out.println("Trying...");
throw new RuntimeException();
// or the actual code that handle the message
} catch (RuntimeException re) {
// handle the exception, logging etc.
throw re
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.