如何忽略maxAttempts并将消息发送到DLQ?

时间:2018-09-27 03:33:23

标签: rabbitmq spring-cloud-stream spring-rabbit dead-letter poison-queue

我正在使用RabbitMQ。消费者API使用的是spring-cloud-steam。 当前,由于maxAttempts为3,如果使用者无法处理该消息,则它将再次排队。这将发生3次。 如果第3次消息也失败,则将其发送到DLX-> DLQ。

如下图所示 Existing Rabbitmq exchange-queue design

现在,如果侦听器端发生某些specificException,我想跳过重试。如何跳过此重试并将消息直接发送到DLX-> DLQ以获取specificException?

下面是配置文件。

spring:
  application:
    name: consumer-api
  cloud:
    stream:
      overrideCloudConnectors: true
      bindings:
        test_channel:
          destination: destinationName
          contentType: application/json
          group: groupName
          consumer:
            maxAttempts: 3
      rabbit:
        bindings:
          test_channel:
            consumer:
              durableSubscription: true
              exchangeType: direct
              bindingRoutingKey: do.test
              autoBindDlq: true
              deadLetterExchange: destinationName.dlx
              deadLetterQueueName: destinationName.dlx.groupName.dlq
              deadLetterRoutingKey: do.test

下面是侦听器代码。

@Slf4j
@Component
public class groupNameListener {

    @StreamListener(TEST_CHANNEL)
    public void doTest(Message<DoTestMessage> doTestMessage) {
        log.info("doTest message received: {}", doTestMessage);

        try {
            if ("someCondition".equals(doTestMessage.getPayload().getMessage())) {
                throw new SpecificException();
            } else {
                log.info("do normal work");
                throw new Exception(); //if something fails
            }
        } catch (SpecificException specificException) {
            log.error("Don't requeue but send to DLQ.... how  ????");
        } catch (Exception ex) {
            log.error("Error in processing do test message");
            throw new AmqpRejectAndDontRequeueException("Reject and Don't requeue exception");
            //this will requeue the message maximum 3 times. If maxAttempts has reached then will be send to DLX->DLQ
        }
    }
}

有人可以帮我吗?如果我有任何错误,请告诉我。

1 个答案:

答案 0 :(得分:1)

即将发布的2.1版本adds the feature to specify which exceptions are, or are not, retryable

2.1.0.M3(里程碑)现在可用。

不可重试的异常将直接进入DLQ。