Spring AMQP-发布者确认发布到不存在的队列时确认返回

时间:2019-11-28 21:40:56

标签: spring-boot spring-amqp spring-rabbitmq spring-rabbit

如果我使用发布者确认并在RabbitMQ上返回回调,则永远不会收到返回的消息。 从Spring AMQP文档:

- Publish to an exchange but there is no matching destination queue.
- Publish to a non-existent exchange.
The first case is covered by publisher returns, as described in Publisher Confirms and Returns.

因此,我认为如果发布存在交换但队列不存在,我将得到返回消息。但是返回回调从未调用过。 我需要设置其他内容吗?
我正在使用RabbitMQ 3.8.0和Spring Boot 2.2.1

application.yml

spring:
  rabbitmq:
    publisher-confirms: true
    publisher-returns: true
    template:
      mandatory: true

制作人

@Service
public class PublisherConfirmProducer {

    private static final Logger log = LoggerFactory.getLogger(PublisherConfirmProducer.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    private void postConstruct() {
        this.rabbitTemplate.setConfirmCallback((correlation, ack, reason) -> {
            if (correlation != null) {
                log.info("Received " + (ack ? " ack " : " nack ") + "for correlation: " + correlation);
            }
        });

        this.rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            log.info("Returned: " + message + "\nreplyCode: " + replyCode + "\nreplyText: " + replyText
                    + "\nexchange/rk: " + exchange + "/" + routingKey);
        });
    }

    // Careful : will be silently dropped, since the exchange is exists, but no
    // route to queue, but ack-ed. How to know that I publish to non-existing queue?
    public void sendMessage_ValidExchange_InvalidQueue(DummyMessage message) {
        CorrelationData correlationData = new CorrelationData("Correlation for message " + message.getContent());
        this.rabbitTemplate.convertAndSend("x.test", "not-valid-routing-key", message, correlationData);
    }

}

主应用

@SpringBootApplication
public class RabbitmqProducerTwoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(RabbitmqProducerTwoApplication.class, args);
    }

    @Autowired
    private PublisherConfirmProducer producer;

    @Override
    public void run(String... args) throws Exception {
        var dummyMessage_2 = new DummyMessage("Message 2", 2);
        producer.sendMessage_ValidExchange_InvalidQueue(dummyMessage_2);
    }

}

记录结果

2019-11-29 04:45:23.796  INFO 8352 --- [           main] c.c.r.RabbitmqProducerTwoApplication     : Starting RabbitmqProducerTwoApplication on timpamungkas with PID 8352 (D:\workspace\eclipse\my-courses\rabbitmq-1.2\rabbitmq-producer-two\bin\main started by USER in D:\workspace\eclipse\my-courses\rabbitmq-1.2\rabbitmq-producer-two)
2019-11-29 04:45:23.800  INFO 8352 --- [           main] c.c.r.RabbitmqProducerTwoApplication     : No active profile set, falling back to default profiles: default
2019-11-29 04:45:24.952  INFO 8352 --- [           main] c.c.r.RabbitmqProducerTwoApplication     : Started RabbitmqProducerTwoApplication in 1.696 seconds (JVM running for 3.539)
2019-11-29 04:45:24.990  INFO 8352 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
2019-11-29 04:45:25.024  INFO 8352 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#599f571f:0/SimpleConnection@86733 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 50688]
2019-11-29 04:45:25.058  INFO 8352 --- [nectionFactory1] c.c.r.producer.PublisherConfirmProducer  : Received  ack for correlation: CorrelationData [id=Correlation for message Message 2]

为加里(Gary)编辑

RabbitMqConfig.java

@Configuration
public class RabbitmqConfig {

    @Bean
    public Jackson2JsonMessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, Jackson2JsonMessageConverter converter) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(converter);
        return template;
    }

}

3 个答案:

答案 0 :(得分:0)

返回的消息后,您将收到肯定的确认。

你的东西对我来说看起来是正确的。它与this sample非常相似,因此应以相同的方式工作。

x.test是什么类型的交换?绑定了哪些队列,以及哪些路由键?

如果在查看该样本后仍无法使它正常工作,请将项目张贴到某个地方,我会看一下。

答案 1 :(得分:0)

实现看起来是正确的,只需实现自己的私有回调类并扩展 ConfirmCallback 以注册回调,在初始化 RabbitTemplate 时设置 ConfirmCallback。

 private static class PublisherCallback implements RabbitTemplate.ConfirmCallback {
    
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("Confirm message returned " + correlationData.toString() + " Ack " + ack + " cause " + cause);
    }

}

rabbitTemplate.setConfirmCallback(new PublisherCallback());

答案 2 :(得分:0)

我使用 Springboot 2.5.2 和 RabbitMQ 3.8.18,当我使用无效的路由键将我的队列绑定到主题交换时,我的 ReturnsCallback 被调用

@Bean(name = "binding")

Binding binding(@Qualifier("queue") Queue queue, @Qualifier("exchange") TopicExchange exchange) {

return BindingBuilder.bind(queue).to(exchange).with("aaa"+ Constants.VALID_ROUTING_KEY);
}

//configuration
rabbitTemplate.setConfirmCallback(confirmCallbackService);
rabbitTemplate.setReturnsCallback(returnCallbackService);
rabbitTemplate.setMandatory(true); //seems this config is mandatory


//logs
2021-07-08 15:08:05,078 ERROR [connectionFactory1] com..publisher.config.ReturnCallbackService: returnedMessage =>

Rabbitmq-Batch-Rabbitmq-Publish-Subscribe