最新の安定バージョンについては、spring-cloud-stream 4.2.1 を使用してください。 |
すべてまとめる
次の構成では、ワイルドカードルーティングキー #
を使用してトピック交換にバインドされたキュー myDestination.consumerGroup
を使用して交換 myDestination
を作成します。
---
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=
---
この構成では、myDestination.consumerGroup
のルーティングキーを使用して直接交換(DLX
)にバインドされた DLQ が作成されます。メッセージが拒否されると、メッセージは 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
ヘッダーの count プロパティが Long
であることに注意してください。