美文网首页
Springboot集成RabbitMq@RabbitListe

Springboot集成RabbitMq@RabbitListe

作者: little多米 | 来源:发表于2023-02-08 11:13 被阅读0次

    如下配置消费队列,期望会自动创建注解中的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<channel.close>(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定义


    image.png

    大意如下,如果定义了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;
        }
    
    }
    

    再次启动,成功创建。

    相关文章

      网友评论

          本文标题:Springboot集成RabbitMq@RabbitListe

          本文链接:https://www.haomeiwen.com/subject/dqdqkdtx.html