首页 文章

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

提问于
浏览
0

我正在使用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 回答

相关问题