制作一个rabbitmq-sdk以及rabbitmq消费者实现定时上下线功能

news/2024/11/17 3:40:34/
目录结构

在这里插入图片描述

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.aasee</groupId><artifactId>sms-modules</artifactId><version>3.6.3</version></parent><artifactId>sms-rabbitmq-starter</artifactId><name>sms-rabbitmq-starter</name><description>rabbitmq-sdk</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- SpringBoot Boot rabbitmq --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- skywalking --><dependency><groupId>org.apache.skywalking</groupId><artifactId>apm-toolkit-logback-1.x</artifactId><version>9.3.0</version></dependency><dependency><groupId>org.apache.skywalking</groupId><artifactId>apm-toolkit-trace</artifactId><version>9.3.0</version></dependency></dependencies>
</project>
rabbitmqconfigureCallback_62">com.aasee.rabbitmq.configure.Callback
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
@Slf4j
public class Callback implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {/**** @param correlationData correlation data for the callback. 相关配置信息* @param ack true for ack, false for nack exchange交换机 是否成功收到了消息。true代表成功,false代表失败* @param cause An optional cause, for nack, when available, otherwise null.  失败原因*/@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String cause) {log.info("confirm方法被执行了!!!!! correlationData:{}",correlationData);if (ack){// 接受成功log.info("接受消息成功! correlationDataId: {} ,cause: {} " ,correlationData.getId(), cause);}else {// 接受失败log.error("接受消息失败! correlationDataId: {} ,cause: {} " ,correlationData.getId(), cause);}}/*** 回退模式:当消息发送给Exchange后, Exchange路由到Queue失败时 才会执行 ReturnCallBack* 步骤:*  1. 开启回退模式: publisher-returns: true #是否开启发送端消息抵达队列的确认*  2. 设置ReturnCallBack*  3. 设置Exchange处理消息的模式:*      1. 如果消息没有路由到Queue,则丢弃信息(默认)*      2. 如果消息没有路由到Queue,返回给消息发送方ReturnCallBack  设置mandatory为true**//**** @param returned the returned message and metadata.*/@Overridepublic void returnedMessage(ReturnedMessage returned) {log.info("return机制方法被执行了。。。。。");log.info("消息(message):" + returned.getMessage());log.info("退回原因代码(replyCode):" + returned.getReplyCode());log.info("退回原因(replyText):" + returned.getReplyText());log.info("交换机(exchange):" + returned.getExchange());log.info("路由Key(routingKey):" + returned.getRoutingKey());// TODO 处理未到Queue的数据(或者使用备份交换机)}
}
rabbitmqconfigureCustomMessageInterceptor_119">com.aasee.rabbitmq.configure.CustomMessageInterceptor
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.stereotype.Component;/*** 自定义消息拦截器** @author Aasee* @date 2024-03-11*/
@Component
public class CustomMessageInterceptor implements MessagePostProcessor {@Overridepublic Message postProcessMessage(Message message) {// 获取原始消息属性MessageProperties properties = message.getMessageProperties();// 设置新的消息头部信息(如果有需求)
//        properties.setHeader("loginUser", SecurityUtils.getLoginUser());return new Message(message.getBody(), properties);}
}
rabbitmqconfigureRabbitMQConfig_148">com.aasee.rabbitmq.configure.RabbitMQConfig
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** RabbitMQ配置** @author Aasee* @date 2024-02-24*/
@Configuration
public class RabbitMQConfig {/*** rabbitmq Redis 防止重复消费键*/public final static String RABBITMQ_REDIS_KEY="RabbitMq_Send_Sms:";public final static String RABBITMQ_REISSUE_KEY="RabbitMq_Reissue_Sms:";public final static String RABBITMQ_CALLBACK_KEY="RabbitMq_Callback_Sms:";//--------------交换机名称-----------------------------------public static final String EXCHANGE_NAME = "aasee_topic_exchange";//---------------队列名称------------------------------------public static final String QUEUE_NAME = "aasee_queue";public static final String SMS_QUEUE_NAME = "cloud_sms_queue";public static final String REISSUE_QUEUE_NAME = "sms_reissue_queue";public static final String CALLBACK_QUEUE_NAME = "callback_sms_queue";//--------------路由键名称-----------------------------------public static final String ROUTING_KEY = "aasee.#";public static final String SMS_ROUTING_KEY = "sms.#";public static final String REISSUE_ROUTING_KEY = "reissue.#";public static final String CALLBACK_ROUTING_KEY = "callback.#";//------------------------交换机--------------------------// 交换机@Bean(value = "aaseeExchange")public Exchange bootExchange(){return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();}//------------------------队列--------------------------// 队列@Bean("aaseeQueue")public Queue bootQueue(){return QueueBuilder.durable(QUEUE_NAME).build();}// SMS 队列@Bean("cloudSmsQueue")public Queue smsQueue(){return QueueBuilder.durable(SMS_QUEUE_NAME).build();}// SMS 补发 队列@Bean("smsReissueQueue")public Queue smsReissueQueue(){return QueueBuilder.durable(REISSUE_QUEUE_NAME).build();}// 短信回调 队列@Bean("callbackSmsQueue")public Queue callbackSmsQueue(){return QueueBuilder.durable(CALLBACK_QUEUE_NAME).build();}//-------------------路由绑定-------------------------// 队列和交换机绑定关系 Binding/*1. 知道哪个队列2. 知道哪个交换机3. routing key*/// 路由绑定@Beanpublic Binding bingQueueExchange(@Qualifier("aaseeQueue") Queue queue, @Qualifier("aaseeExchange") Exchange exchange){return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY).noargs();}// SMS 路由绑定@Beanpublic Binding bingSmsQueueExchange(@Qualifier("cloudSmsQueue") Queue queue, @Qualifier("aaseeExchange") Exchange exchange){return BindingBuilder.bind(queue).to(exchange).with(SMS_ROUTING_KEY).noargs();}// 补发 路由绑定@Beanpublic Binding bingSmsReissueQueueExchange(@Qualifier("smsReissueQueue") Queue queue, @Qualifier("aaseeExchange") Exchange exchange){return BindingBuilder.bind(queue).to(exchange).with(REISSUE_ROUTING_KEY).noargs();}// 短信回调 路由绑定@Beanpublic Binding bingCallbackSmsQueueExchange(@Qualifier("callbackSmsQueue") Queue queue, @Qualifier("aaseeExchange") Exchange exchange){return BindingBuilder.bind(queue).to(exchange).with(CALLBACK_ROUTING_KEY).noargs();}// Mq模板类@Bean//设置rabbitTemplate的scope为:prototype
//    @Scope("prototype")public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {RabbitTemplate template = new RabbitTemplate(connectionFactory);template.setBeforePublishPostProcessors(new CustomMessageInterceptor());//成功回调template.setConfirmCallback(new Callback());// 开启mandatory模式(开启失败回调)template.setMandatory(true);//失败回调template.setReturnsCallback(new Callback());return template;}}
rabbitmqserviceRabbitMqService_265">com.aasee.rabbitmq.service.RabbitMqService
import com.alibaba.fastjson2.JSON;
import com.aasee.rabbitmq.configure.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.UUID;/*** Rabbit MQ 工具类** @author Aasee* @date 2024-03-11*/
//@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
@Slf4j
public class RabbitMqService {@Autowiredpublic RabbitTemplate rabbitTemplate;/*** 发送 MQ 消息** @param exchange             交换机* @param routingKey           路由键* @param value                放入mq中的消息体(需要对象)* @param messagePostProcessor 消息后处理器(自定义处理)* @param correlationData      相关数据(用于传递唯一标识,跟踪绑定数据信息)*/public <T> void sendMqMessage(String exchange, String routingKey,T value, MessagePostProcessor messagePostProcessor,CorrelationData correlationData) {// 推送到Mq中rabbitTemplate.convertAndSend(exchange, routingKey, JSON.toJSONString(value), messagePostProcessor,correlationData);}/*** 发送 MQ 消息** @param exchange             交换机* @param routingKey           路由键* @param value                放入mq中的消息体(需要对象)* @param messagePostProcessor 消息后处理器(自定义处理)*/public <T> void sendMqMessage(String exchange, String routingKey,T value, MessagePostProcessor messagePostProcessor) {// 唯一标识用于判断消息身份和内容String messageId = UUID.randomUUID().toString();CorrelationData correlationData = new CorrelationData(messageId);// 推送到Mq中String jsonString = JSON.toJSONString(value);log.info("发送 MQ 消息! exchange: {} , routingKey: {} , messageId: {} , jsonString: {}",exchange,routingKey,messageId,jsonString);rabbitTemplate.convertAndSend(exchange, routingKey, jsonString, messagePostProcessor,correlationData);}/*** 发送短信信息** @param value                短信信息* @param messagePostProcessor 消息后处理器(自定义处理)*/public <T> void sendSmsMessage(T value, MessagePostProcessor messagePostProcessor) {// 推送到Mq中rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.SMS_ROUTING_KEY, JSON.toJSONString(value), messagePostProcessor);}
}
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.aasee.rabbitmq.configure.RabbitMQConfig, \com.aasee.rabbitmq.service.RabbitMqService, \com.aasee.rabbitmq.configure.CustomMessageInterceptor

以上就是sdk的所有配置内容,使用bean的自动装配原理让这个sdk被引入时可以自动被spring托管配置。

接下来就是展示如何使用sdk的,同时展示nacos配置或者说application.yml配置

引入SDK
<dependency><groupId>com.aasee</groupId><artifactId>sms-rabbitmq-starter</artifactId><version>3.6.3</version>
</dependency>

可以把这个sdk放到阿里云制品仓库,或者自建私服,又或是直接托管到maven中央仓库,这样你的小伙伴们就能直接引入你的sdk

application.yml
# rabbitmq 配置rabbitmq:host: xx.xx.xxx.xxxvirtual-host: /cloudsmsTestusername: rootpassword: xxxxxxport: 5672publisher-confirm-type: correlatedpublisher-returns: true #是否开启发送端消息抵达队列的确认template:mandatory: true # 只要消息没有正确抵达队列,以异步方式优先执行我们自己设置的回调,设置交换机处理失败消息的模式listener:simple:acknowledge-mode: manualprefetch: 1 #更改为每次读取1条消息,在消费者未回执确认之前,不在进行下一条消息的投送
定时上下线

在开发中我发现了一个有趣的需求,定时上线消费,定时下线停止消费,生产者可以持续往队列里发送消息,但是消费者则可以在指定时间,或者通过手动的方式上下线,以下是具体实现方法

com.aasee.smsconsumer.scheduler.ConsumerScheduler
import com.aasee.common.core.constant.ChannelConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component
@Slf4j
public class ConsumerScheduler implements ApplicationListener<ContextRefreshedEvent> {@Autowiredprivate ApplicationContext applicationContext;@Autowiredprivate RabbitListenerEndpointRegistry registry;// 每天早上8点启动消费者// todo 创建接口用于手动启动@Scheduled(cron = "0 0 8 * * ?")public void startListening() {
//        RabbitListenerEndpointRegistry registry = applicationContext.getBean(RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME, RabbitListenerEndpointRegistry.class);startConsumer();}public void startConsumer() {if (!registry.getListenerContainer(ChannelConstant.SMSCONSUMER).isRunning()) {registry.getListenerContainer(ChannelConstant.SMSCONSUMER).start();log.info("smsConsumer 开启监听");}if (!registry.getListenerContainer(ChannelConstant.REISSUECONSUMER).isRunning()) {registry.getListenerContainer(ChannelConstant.REISSUECONSUMER).start();log.info("reissueConsumer 开启监听");}}// 每天晚上7点停止消费者@Scheduled(cron = "0 0 19 * * ?")public void stopListening() {
//        RabbitListenerEndpointRegistry registry = applicationContext.getBean(RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME, RabbitListenerEndpointRegistry.class);if (registry.getListenerContainer(ChannelConstant.SMSCONSUMER).isRunning()) {registry.getListenerContainer(ChannelConstant.SMSCONSUMER).stop();log.info("smsConsumer 停止监听");}if (registry.getListenerContainer(ChannelConstant.REISSUECONSUMER).isRunning()) {registry.getListenerContainer(ChannelConstant.REISSUECONSUMER).stop();log.info("reissueConsumer 停止监听");}}// 这个是为了每次启动或者重启服务后马上开始消费,可以取消@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {log.info("服务启动开启消费!");startConsumer();}
}
注意

如何你们存在这种定时上下线的需求,需要在**@RabbitListener**注解上加多一个参数 autoStartup = “false” ,这样消费者就不会自动消费消息了

@RabbitListener(id = ChannelConstant.REISSUECONSUMER,queues = RabbitMQConfig.REISSUE_QUEUE_NAME,autoStartup = "false")

http://www.ppmy.cn/news/1530847.html

相关文章

JavaScript 学习

一、输出 为方便调试可以输出内容&#xff0c;但是用户是看不到的。要在开发者模式中看。 console . log ( "Hello" )&#xff1b; 二、外部文件引用 可以直接在html中写JS <head> <meta charset"utf-8"> <script> console.log("he…

利用 GlobalPointer 进行中文命名实体识别

利用 GlobalPointer 进行中文命名实体识别 在自然语言处理领域&#xff0c;命名实体识别&#xff08;NER&#xff09;是一个重要任务&#xff0c;它旨在识别文本中的特定信息单元&#xff0c;如人名、地名和组织名等。本文将详细分析使用 GlobalPointer 进行中文命名实体识别的…

甘肃非遗文化网站:Spring Boot开发实战

3 系统分析 当用户确定开发一款程序时&#xff0c;是需要遵循下面的顺序进行工作&#xff0c;概括为&#xff1a;系统分析–>系统设计–>系统开发–>系统测试&#xff0c;无论这个过程是否有变更或者迭代&#xff0c;都是按照这样的顺序开展工作的。系统分析就是分析系…

Linux-L14-Linux中把用户加入到管理者root中

目的 将用户添加到root用户组&#xff08;通常是管理员组&#xff09; 步骤 这可以通过usermod命令来完成 1打开终端 如果你需要为新用户添加到root组&#xff0c;首先使用以下命令创建一个新用户&#xff08;如果用户已经存在&#xff0c;可以跳过这一步&#xff09;&…

如何安装和注册 GitLab Runner

如何安装和注册 GitLab Runner GitLab Runner 是一个用于运行 GitLab CI/CD (Continuous Integration/Continuous Deployment) 作业。它是一个与 GitLab 配合使用的应用程序&#xff0c;可以在本地或云中运行。Runner 可以执行不同类型的作业&#xff0c;例如编译代码、运行测…

计算机毕业设计 物业智慧系统的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

签署《AI安全国际对话威尼斯共识》 智源持续推动人工智能安全发展

近日&#xff0c;由AI安全国际论坛&#xff08;Safe AI Forum&#xff09;和博古睿研究院&#xff08;Berggruen Institute) 共同举办的第三届国际AI安全对话&#xff08;International Dialogues on AI Safety&#xff09;在威尼斯举办。图灵奖得主Yoshua Bengio、姚期智教授&…

Qt C++设计模式->装饰器模式

装饰器模式&#xff08;Decorator Pattern&#xff09;是一种结构型设计模式&#xff0c;允许你在不改变对象接口的情况下&#xff0c;动态地为对象添加额外的功能。它通过将对象包装在装饰器类中来实现这一点&#xff0c;从而提供了比继承更灵活的扩展方式。 装饰器模式的应用…