如何使用 Spring Cloud Stream Rabbit 管理手动确认?

How to manage manual ack with Spring Cloud Stream Rabbit?

我尝试了几种使用 RabbitMQ 管理手动 ack 的方法,尤其是基于 this previous post,但不幸的是,其中 none 有效。

在我的配置文件中,我已将 acknowledge-mode 设置为 MANUAL,如下所示:

spring:
  cloud:
    stream:
      bindings:
        magento-consumer:
          binder: rabbit
          destination: toto
          durableSubscription: false
          consumer:
            acknowledge-mode: MANUAL

然后,我有一个 receive 方法,它由 @StreamListener 注释,它有 4 个参数:

    @StreamListener(SinkInterface.NAME)
    public void receive(
            Message<Event> m,
            @Header(name = Queue.TO_MAGENTO , required = false) Boolean header,
            @Header(AmqpHeaders.CHANNEL) Channel channel,
            @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag
    ) {

        //Do something

    }

问题是 channel 参数始终为空,因此我无法检索 amqp_channel header.

org.springframework.messaging.MessageHandlingException: Missing header 'amqp_channel' for method parameter type [interface com.rabbitmq.client.Channel]

如何检索此频道参数? 我不明白为什么 amqp_channel 没有设置,以及如何注入它。

我是不是漏掉了什么?

我看到 属性 名称需要更正。设置 acknowledgeMode 的正确 属性 名称是:spring.cloud.stream.rabbit.bindings.<channelName>.consumer. acknowledge-mode。您可以查看 here 了解更多信息。