[当启用发布者确认时,设置了队列长度限制,并且将溢出设置为拒绝-发布,为什么导致我收到的确认回调为空?

问题描述 投票:1回答:1

我正在学习队列长度限制(https://www.rabbitmq.com/maxlength.html),正如它说的那样,将队列设置为'x-max-length:10''x-overflow:reject-publish',此外,我启用发布者确认,因此,当队列中的邮件数达到10,将通过basic.nack消息通知发布者拒绝,并且它是:我的确认回调收到了错误的ack,但原因为null,我想知道它是否不应该返回某些内容,以便我可以区分情况。部分代码如下:

  @Bean
  public AmqpTemplate amqpTemplate(@Autowired CachingConnectionFactory amqpConnectionFactory) {
    amqpConnectionFactory.setPublisherReturns(true);
    amqpConnectionFactory.setPublisherConfirms(true);
    RabbitTemplate rabbitTemplate = new RabbitTemplate(amqpConnectionFactory);
    rabbitTemplate.setMessageConverter(jsonMessageConverter());
    rabbitTemplate.setConfirmCallback(confirmCallback);
    rabbitTemplate.setReturnCallback(returnCallback);
    return rabbitTemplate;
  }

  static RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
      System.out.println(ack);  // when number of messages reach 10, print false
      System.out.println(cause); // when number of messages reach 10, print null
    }
  };

 @Bean
  public Queue queue() {
    return QueueBuilder.durable(DURABLE_QUEUE).withArgument("x-max-length", 10).withArgument("x-overflow", "reject-publish").build();
  }

 @Scheduled(fixedDelay = 1000L)
  public void produce() {
    Message msg = new Message(UUID.randomUUID().toString(), "sth");
    amqpTemplate.convertAndSend("sth", "sth", msg );
  }

java spring-boot spring-amqp spring-rabbitmq
1个回答
0
投票

[不幸的是,AMQP协议和Java客户端没有提供有关发布失败原因的信息。仅确认/否以及确认是否针对多条消息:

/**
 * Implement this interface in order to be notified of Confirm events.
 * Acks represent messages handled successfully; Nacks represent
 * messages lost by the broker.  Note, the lost messages could still
 * have been delivered to consumers, but the broker cannot guarantee
 * this.
 * For a lambda-oriented syntax, use {@link ConfirmCallback}.
 */
public interface ConfirmListener {
    void handleAck(long deliveryTag, boolean multiple)
        throws IOException;

    void handleNack(long deliveryTag, boolean multiple)
        throws IOException;
}

我们添加了cause,因为在某些情况下,框架会合成一个小节(例如,在等待确认的同时关闭通道时,我们将Channel closed by application添加为cause

该框架无法推测我们从经纪人那里得到零花钱的原因。

© www.soinside.com 2019 - 2024. All rights reserved.