Springboot集成RabbitMq@RabbitListener不自动生成队列

如下配置消费队列,期望会自动创建注解中的queue和exchange

@Component
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = MqDefConstant.QUEUE_DEAL_ORDER_REFUND_1, durable = "true", autoDelete = "false"),
        exchange = @Exchange(value = MqDefConstant.EXCHANGE_ORDER_REFUND, type = ExchangeTypes.FANOUT)), containerFactory = "rabbitListenerContainerFactory")
public class OrderRefundConsumer {

    private static final Logger logger = LoggerFactory.getLogger(OrderRefundConsumer.class);

    @RabbitHandler
    public void consumer(OrderRefundMsg msg) {
        //...
    }
}

但是启动报错,日志如下

com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method(reply-code=404, reply-text=NOT_FOUND - no queue 'RETAILSTORE.TOPIC.ALIPAYNOTIFYTOPIC' in vhost '/', class-id=50, method-id=10)
org.springframework.amqp.rabbit.listener.QueuesNotAvailableException: Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.

查看RabbitListener定义
Springboot集成RabbitMq@RabbitListener不自动生成队列_第1张图片
大意如下,如果定义了RabbitAdmin的Bean,就会自动创建队列
修改rabbit配置,增加RabbitAdmin定义,如下

@Configuration
@EnableRabbit
public class RabbitConfig {
    
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        // 服务启动时候开启自动启动
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

}

再次启动,成功创建。

你可能感兴趣的:(组件,java-rabbitmq,rabbitmq,spring,boot)