config:rabbitmq auto create queue

This commit is contained in:
zzs 2024-11-11 16:15:52 +08:00
parent 5c44ac05ec
commit c5ab64c137
4 changed files with 24 additions and 3 deletions

View File

@ -31,7 +31,6 @@
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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 配置类