Spring Cloud Stream RabbitMQ 在死信队列上留下消息

Spring Cloud Stream RabbitMQ leave message on dead letter queue

我正在使用 Spring 云流来使用来自 rabbitmq 的消息。在多次重试后,我试图让一条失败的消息留在死信队列中。我在使用 amqp 之前以编程方式完成了此操作,但使用 spring 云流似乎有点困难。

@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");
  }
}

我的配置

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

非常感谢任何帮助

谢谢

您不能修改被拒绝的消息(例如添加 TTL header)。

我相信您将不得不在重试次数耗尽时手动发布到 parking-lot queue。