Im using Spring cloud streams to consume message from rabbitmq. Im trying to get a failed message to stay on a dead letter queue after a number of retries. I have programmatically done this before using amqp but it seems to be a bit more difficult to get right with spring cloud streams.
@StreamListener(target = Sink.INPUT)
public void messageListener(final String in, @Header(name = "x-death", required = false) Map<?, ?> death) {
if (!validString(in)) {
// We don’t need this message anymore not even on the dlq
throw new ImmediateAcknowledgeAmqpException(“String not good”);
}
// If message has been retried more than 3 time we want to put message on dlq
if (death != null && death.get("count").equals(3L)) {
// I know this is incorrect as it will ack the message, but at this point I need the message to be left on the dlq
throw new ImmediateAcknowledgeAmqpException("Failed after 4 attempts");
}
try {
// this trows an exception
processService.process(in);
}
catch (Exception ex) {
// here we retry the message
throw new AmqpRejectAndDontRequeueException("retry message");
}
}
My Configuration
spring.cloud.stream.bindings.input.destination=adestination
spring.cloud.stream.bindings.input.group=aqueue
spring.cloud.stream.rabbit.bindings.input.consumer.bindingRoutingKey=akey
#dlx/dlq setup
spring.cloud.stream.rabbit.bindings.input.consumer.deadLetterQueueName=adeadletterqueue
spring.cloud.stream.rabbit.bindings.input.consumer.dlqDeadLetterExchange=
spring.cloud.stream.rabbit.bindings.input.consumer.autoBindDlq=true
spring.cloud.stream.rabbit.bindings.input.consumer.requeueRejected=true
spring.cloud.stream.rabbit.bindings.input.consumer.dlqTtl=5000
# disable binder retries
spring.cloud.stream.bindings.input.consumer.max-attempts=1
Any help greatly appreciated
Thanks
You can't modify a rejected message (e.g. adding a TTL header).
I believe you will have to manually publish to a parking-lot queue when the retries are exhausted.