当前位置: 首页 > ops >正文

RabbitMQ简单消息监听

如何监听RabbitMQ队列

简单代码实现RabbitMQ消息监听

需要的依赖

        <!--rabbitmq--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId><version>x.x.x</version></dependency>

消息监听示例

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Slf4j
@Configuration
public class RabbitMQConfig {private String addresses = "localhost:5672";private String username = "xxx";private String password = "xxx";@Beanpublic ConnectionFactory connectionFactory() {CachingConnectionFactory connectionFactory = new CachingConnectionFactory();connectionFactory.setAddresses(this.addresses);connectionFactory.setUsername(this.username);connectionFactory.setPassword(this.password);return connectionFactory;}@Beanpublic SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.setQueueNames("queue1", "queue2");//要监听的队列名称// 设置消息监听器container.setMessageListener((message) -> {log.info("Received message: " + new String(message.getBody()));});return container;}}
http://www.xdnf.cn/news/14741.html

相关文章:

  • MongoDB 安装使用教程
  • 我认知的AI宇宙系列第三期
  • 视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
  • 车载Tier1 supplier梳理
  • Instrct-GPT 强化学习奖励模型 Reward modeling 的训练过程原理实例化详解
  • C语言main函数的原理:程序入口的奥秘
  • 多路转接select
  • Linux云计算基础篇(2)
  • SpringCloud系列(42)--搭建SpringCloud Config分布式配置总控中心(服务端)
  • Deepoc 大模型在无人机行业应用效果的方法
  • java JNDI高版本绕过 工具介绍 自动化bypass
  • 信息安全工程师考试架构相关说明
  • Nordic空中升级OTA[NRF52832蓝牙OTA]
  • 力扣 hot100 Day30
  • Hadoop WordCount 程序实现与执行指南
  • Python 数据分析与机器学习入门 (三):Pandas 数据导入与核心操作
  • 提示技术系列——链式提示
  • 现代 JavaScript (ES6+) 入门到实战(四):数组的革命 map/filter/reduce - 告别 for 循环
  • stm32 USART串口协议与外设(程序)——江协教程踩坑经验分享
  • 第二届 Parloo杯 应急响应学习——畸形的爱
  • 理解 Confluent Schema Registry:Kafka 生态中的结构化数据守护者
  • Qt事件系统
  • 机器学习在智能电网中的应用:负荷预测与能源管理
  • MySQL锁机制全解析
  • 06_注意力机制
  • Modbus 报文结构与 CRC 校验实战指南(一)
  • leetcode437-路径总和III
  • TVFEMD-CPO-TCN-BiLSTM多输入单输出模型
  • ASP.Net依赖注入!使用Microsoft.Extensions.DependencyInjection配置依赖注入
  • 【ad-hoc】# P12414 「YLLOI-R1-T3」一路向北|普及+