How to get Spring RabbitMQ to create a new Queue?

前端 未结 4 644
长发绾君心
长发绾君心 2020-12-06 09:46

In my (limited) experience with rabbit-mq, if you create a new listener for a queue that doesn\'t exist yet, the queue is automatically created. I\'m trying to use the Sprin

4条回答
  •  星月不相逢
    2020-12-06 10:20

    As of Spring Boot 2.1.6 and Spring AMQP 2.1.7 you can create queues during startup if they don't exists with this:

    @Component
    public class QueueConfig {
    
        private AmqpAdmin amqpAdmin;
    
        public QueueConfig(AmqpAdmin amqpAdmin) {
            this.amqpAdmin = amqpAdmin;
        }
    
        @PostConstruct
        public void createQueues() {
            amqpAdmin.declareQueue(new Queue("queue_one", true));
            amqpAdmin.declareQueue(new Queue("queue_two", true));
        }
    }
    

提交回复
热议问题