正如我所说,我正在学习队列长度限制(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 );
}
答案 0 :(得分: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
添加为{{1 }}。
该框架无法推测出经纪人给我们带来麻烦的原因。