我正在使用RabbitMqTemplate的convertAndSend方法发布消息。如果我没有运行rabbitMq服务器,那么我希望该调用引发一个异常,但不会。
我已经像这样设置了连接工厂
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
connectionFactory.setUsername(rabbitProperties.getUsername());
connectionFactory.setPassword(rabbitProperties.getPassword());
connectionFactory.setPort(rabbitProperties.getPort());
connectionFactory.setPublisherConfirms(true);
connectionFactory.setPublisherReturns(true);
return connectionFactory;
}
@Bean
public RabbitTemplate rabbitTemplate() {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
return rabbitTemplate;
}
我这样发送消息
this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
LOGGER.debug("RabbitMq could not send the message");
});
this.rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
LOGGER.debug("RabbitMq return callback : " + replyCode);
});
this.rabbitTemplate.convertAndSend(exchangeName, routing, deliverable, new CorrelationData(correlationId));
如果convertAndSend没有成功将消息发布到Queue,我想采取一些措施。我该怎么办?
谢谢, Sudha