Browse Source

设置mq自动加载队列

Bob 2 years ago
parent
commit
b8b9b6e32c
1 changed files with 38 additions and 10 deletions
  1. 38 10
      background-user/src/main/java/cn/kdan/compdf/config/RabbitConfig.java

+ 38 - 10
background-user/src/main/java/cn/kdan/compdf/config/RabbitConfig.java

@@ -5,6 +5,9 @@ import org.springframework.amqp.core.Binding;
 import org.springframework.amqp.core.BindingBuilder;
 import org.springframework.amqp.core.Queue;
 import org.springframework.amqp.core.TopicExchange;
+import org.springframework.amqp.rabbit.connection.ConnectionFactory;
+import org.springframework.amqp.rabbit.core.RabbitAdmin;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
@@ -15,6 +18,20 @@ import org.springframework.context.annotation.Configuration;
 @Configuration
 public class RabbitConfig {
 
+    /**
+     * 设置启动自动创建队列
+     * @param connectionFactory
+     * @return
+     */
+    @Bean
+    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
+        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
+        rabbitAdmin.setAutoStartup(true);
+        return rabbitAdmin;
+    }
+    @Autowired
+    private RabbitAdmin rabbitAdmin;
+
     /**
      *  topics 交换机
      */
@@ -24,12 +41,14 @@ public class RabbitConfig {
         // 参数2:  消息是否持久
         // 参数3:  是否自动删除
         // 参数4:  其他配置
-        return new TopicExchange(RabbitMQEnum.BACKGROUND_EXCHANGE,true,false,null);
+        TopicExchange topicExchange = new TopicExchange(RabbitMQEnum.BACKGROUND_EXCHANGE, true, false, null);
+        rabbitAdmin.declareExchange(topicExchange);
+        return topicExchange;
     }
 
 
     /**
-     * API新队列
+     * API新队列
      */
     @Bean
     public Queue getAPIAddQueue(){
@@ -38,36 +57,45 @@ public class RabbitConfig {
         // 参数3: 这个队列是否让消费者多占
         // 参数4: 这个队列是否自动删除
         // 参数5:  这个队列的其他参数
-        return new Queue(RabbitMQEnum.BACKGROUND_API_ADD_QUEUE,true,false,false,null);
+        Queue queue = new Queue(RabbitMQEnum.BACKGROUND_API_ADD_QUEUE, true, false, false, null);
+        rabbitAdmin.declareQueue(queue);
+        return queue;
     }
     /**
      * API删除队列
      */
     @Bean
     public Queue getAPIDelQueue(){
-
-        return new Queue(RabbitMQEnum.BACKGROUND_API_DEL_QUEUE,true,false,false,null);
+        Queue queue = new Queue(RabbitMQEnum.BACKGROUND_API_DEL_QUEUE, true, false, false, null);
+        rabbitAdmin.declareQueue(queue);
+        return queue;
     }
     /**
-     * webhooks新队列
+     * webhooks新队列
      */
     @Bean
     public Queue getWebhooksAddQueue(){
-        return new Queue(RabbitMQEnum.BACKGROUND_WEBHOOKS_ADD_QUEUE,true,false,false,null);
+        Queue queue = new Queue(RabbitMQEnum.BACKGROUND_WEBHOOKS_ADD_QUEUE, true, false, false, null);
+        rabbitAdmin.declareQueue(queue);
+        return queue;
     }
     /**
      * webhooks删除队列
      */
     @Bean
     public Queue getWebhooksDelQueue(){
-        return new Queue(RabbitMQEnum.BACKGROUND_WEBHOOKS_DEL_QUEUE,true,false,false,null);
+        Queue queue = new Queue(RabbitMQEnum.BACKGROUND_WEBHOOKS_DEL_QUEUE, true, false, false, null);
+        rabbitAdmin.declareQueue(queue);
+        return queue;
     }
     /**
-     * webhooks删除队列
+     * webhooks编辑队列
      */
     @Bean
     public Queue getWebhooksUpdQueue(){
-        return new Queue(RabbitMQEnum.BACKGROUND_WEBHOOKS_UPD_QUEUE,true,false,false,null);
+        Queue queue = new Queue(RabbitMQEnum.BACKGROUND_WEBHOOKS_UPD_QUEUE, true, false, false, null);
+        rabbitAdmin.declareQueue(queue);
+        return queue;
     }