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

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.lqws.cn/news/595837.html

相关文章:

  • 基于开源AI智能名片链动2+1模式S2B2C商城小程序的抖音渠道力拓展与多渠道利润增长研究
  • [特殊字符] 分享裂变新姿势:用 UniApp + Vue3 玩转小程序页面分享跳转!
  • 飞算 JavaAI:我的编程强力助推引擎
  • vue-34(单元测试 Vue 组件的介绍)
  • Langgraph 学习教程
  • AR衍射光波导设计遇瓶颈,OAS 光学软件来破局
  • 基于 51 单片机做的二十四点小游戏
  • Gartner《Choosing Event Brokers to Support Event-DrivenArchitecture》心得
  • AG32调试bug集合
  • ubuntu编译 aosp for x86, windows avd启动 aosp-qemu镜像
  • PADS使用记录2
  • android14 默认关闭或开启数据流量
  • Ubuntu机器开启root用户远程登录
  • FastGPT与MCP:解锁AI新时代的技术密码
  • Spring Boot - 参数校验:分组校验、自定义注解、嵌套对象全解析
  • 【CVPR2024】计算机视觉|EGTR : Transformer中挖掘关系图,场景图生成SOTA!
  • 141.在 Vue 3 中使用 OpenLayers Link 交互:把地图中心点 / 缩放级别 / 旋转角度实时写进 URL,并同步解析显示
  • 利用springEvent,进行服务内部领域事件处理
  • 使用 icinga2 写入 TDengine
  • 用lines_gauss的width属性提取缺陷
  • C++ 11 中 condition_variable 的探索与实践
  • python 调用C/C++动态库
  • Apache HTTP Server部署全攻略
  • 用于快速训练收敛的 Conditional DETR
  • 基于.Net的Web API 控制器及方法相关注解属性
  • 数字ic后端设计从入门到精通10(含fusion compiler, tcl教学)静态时序分析
  • 3D 商品展示与 AR 试戴能为珠宝行业带来一些便利?
  • 什么是测试用例?它的核心要素有哪些?
  • docker desktop部署本地gitlab服务
  • 【仿muduo库实现并发服务器】Connection模块