RabbitTemplate的配置和使用就不介绍了,就说一下遇到的问题
问题:RabbitTemplate发送消息时候,如果队列不存在,它收不到消息,也不报错,对于新手或者调试的时候会有干扰
解决办法:简单粗暴,发送消息前先查一下要发送的队列存不存在,不存在就创建再发送
......connectionFactory配置省略......
@Autowired
private RabbitTemplate rabbitTemplate;
RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate);
String queueName= "qa.message.testQueue";
//如果queueName队列不存在,创建队列
if (Objects.isNull(rabbitAdmin.getQueueProperties(queueName))) {
org.springframework.amqp.core.Queue queue = new Queue(queueName);
rabbitAdmin.declareQueue(queue);
}
rabbitTemplate.convertAndSend(routingKey, userEntities);