【发布时间】:2019-11-28 21:40:56
【问题描述】:
如果我在 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]
为加里编辑
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;
}
}
【问题讨论】:
标签: spring-boot spring-amqp spring-rabbit