From c5ab64c137d9b27ce1b5a7b67237562a0fdcf79f Mon Sep 17 00:00:00 2001
From: zzs <hi@lnbiuc.copm>
Date: Mon, 11 Nov 2024 16:15:52 +0800
Subject: [PATCH] config:rabbitmq auto create queue

---
 .../wmyun-spring-boot-starter-mq/pom.xml        |  1 -
 .../config/WmyunRabbitMQAutoConfiguration.java  | 17 +++++++++++++++++
 .../system/mq/producer/sms/SmsProducer.java     |  7 ++++++-
 .../src/main/resources/application-local.yaml   |  2 +-
 4 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/wmyun-framework/wmyun-spring-boot-starter-mq/pom.xml b/wmyun-framework/wmyun-spring-boot-starter-mq/pom.xml
index b8b5bfb..1917307 100644
--- a/wmyun-framework/wmyun-spring-boot-starter-mq/pom.xml
+++ b/wmyun-framework/wmyun-spring-boot-starter-mq/pom.xml
@@ -31,7 +31,6 @@
         <dependency>
             <groupId>org.springframework.amqp</groupId>
             <artifactId>spring-rabbit</artifactId>
-            <optional>true</optional>
         </dependency>
         <dependency>
             <groupId>org.apache.rocketmq</groupId>
diff --git a/wmyun-framework/wmyun-spring-boot-starter-mq/src/main/java/com/wmyun/framework/mq/rabbitmq/config/WmyunRabbitMQAutoConfiguration.java b/wmyun-framework/wmyun-spring-boot-starter-mq/src/main/java/com/wmyun/framework/mq/rabbitmq/config/WmyunRabbitMQAutoConfiguration.java
index 437d373..afea289 100644
--- a/wmyun-framework/wmyun-spring-boot-starter-mq/src/main/java/com/wmyun/framework/mq/rabbitmq/config/WmyunRabbitMQAutoConfiguration.java
+++ b/wmyun-framework/wmyun-spring-boot-starter-mq/src/main/java/com/wmyun/framework/mq/rabbitmq/config/WmyunRabbitMQAutoConfiguration.java
@@ -1,6 +1,9 @@
 package com.wmyun.framework.mq.rabbitmq.config;
 
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.core.Queue;
+import org.springframework.amqp.rabbit.core.RabbitAdmin;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
 import org.springframework.amqp.support.converter.MessageConverter;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -25,4 +28,18 @@ public class WmyunRabbitMQAutoConfiguration {
         return new Jackson2JsonMessageConverter();
     }
 
+    // 创建 RabbitAdmin
+    @Bean
+    public RabbitAdmin rabbitAdmin(RabbitTemplate rabbitTemplate) {
+        RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate);
+        rabbitAdmin.afterPropertiesSet(); // 初始化
+        return rabbitAdmin;
+    }
+
+
+    // 定义队列
+    @Bean
+    public Queue MESSAGE_QUEUE() {
+        return new Queue("SEND_MESSAGE_QUEUE", true);
+    }
 }
diff --git a/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/mq/producer/sms/SmsProducer.java b/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/mq/producer/sms/SmsProducer.java
index 17d4770..881a224 100644
--- a/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/mq/producer/sms/SmsProducer.java
+++ b/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/mq/producer/sms/SmsProducer.java
@@ -3,6 +3,7 @@ package com.wmyun.module.system.mq.producer.sms;
 import com.wmyun.framework.common.core.KeyValue;
 import com.wmyun.module.system.mq.message.sms.SmsSendMessage;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Component;
 
@@ -22,6 +23,9 @@ public class SmsProducer {
     @Resource
     private ApplicationContext applicationContext;
 
+    @Resource
+    private RabbitTemplate rabbitTemplate;
+
     /**
      * 发送 {@link SmsSendMessage} 消息
      *
@@ -35,7 +39,8 @@ public class SmsProducer {
                                    Long channelId, String apiTemplateId, List<KeyValue<String, Object>> templateParams) {
         SmsSendMessage message = new SmsSendMessage().setLogId(logId).setMobile(mobile);
         message.setChannelId(channelId).setApiTemplateId(apiTemplateId).setTemplateParams(templateParams);
-        applicationContext.publishEvent(message);
+//        applicationContext.publishEvent(message);
+        rabbitTemplate.convertAndSend(SmsSendMessage.QUEUE, message);
     }
 
 }
diff --git a/wmyun-module-system/wmyun-module-system-biz/src/main/resources/application-local.yaml b/wmyun-module-system/wmyun-module-system-biz/src/main/resources/application-local.yaml
index 7af72a2..93ad389 100644
--- a/wmyun-module-system/wmyun-module-system-biz/src/main/resources/application-local.yaml
+++ b/wmyun-module-system/wmyun-module-system-biz/src/main/resources/application-local.yaml
@@ -92,7 +92,7 @@ spring:
   # RabbitMQ 配置项,对应 RabbitProperties 配置类
   rabbitmq:
     host: 127.0.0.1 # RabbitMQ 服务的地址
-    port: 15672 # RabbitMQ 服务的端口
+    port: 5672 # RabbitMQ 服务的端口
     username: admin # RabbitMQ 服务的账号
     password: admin # RabbitMQ 服务的密码
   # Kafka 配置项,对应 KafkaProperties 配置类