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

时间:2019-09-21 20:12:01

标签: spring-cloud-stream spring-rabbitmq

我正在使用Spring云流来消耗来自Rabbitmq的消息。我尝试多次重试后,试图获取失败的消息以保留在死信队列中。在使用amqp之前,我已经以编程方式进行了此操作,但要正确使用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");
  }
}

我的配置

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

任何帮助表示赞赏

谢谢

1 个答案:

答案 0 :(得分:1)

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

我相信重试用尽后,您将必须手动将其发布到停车场队列中。