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

RabbitMQ - SpringAMQP及Work模型

一、概述

RabbitMQ是一个流行的开源消息代理,支持多种消息传递协议。它通常用于实现异步通信、解耦系统组件和分布式任务处理。Spring AMQP是Spring框架下的一个子项目,提供了对RabbitMQ的便捷访问和操作。本文将详细介绍RabbitMQ的工作模型(Work Queue Model)以及如何通过Spring AMQP实现该模型。

二、RabbitMQ工作模型

工作模型(Work Queue Model)是一种常见的消息传递模式,适用于将任务分发给多个工作者(worker)进行并行处理。这种模型提高了任务处理的效率和系统的吞吐量。

1. 模型概述
  • 生产者(Producer) :发送消息到队列。
  • 队列(Queue) :存储消息,等待被消费者处理。
  • 消费者(Consumer) :从队列中接收和处理消息。
2. 模型特性
  • 消息轮询:消息在多个消费者之间进行轮询分发,每个消息只被一个消费者处理。
  • 消息确认:消费者处理完成后,发送确认消息,确保消息不会丢失。
  • 预取计数:通过设置预取计数(prefetch count),可以限制消费者一次从队列中获取的消息数量,防止消息处理不均衡。
三、Spring AMQP实现

使用Spring AMQP可以方便地与RabbitMQ进行交互。以下示例展示了如何通过Spring AMQP实现工作模型。

1. 配置

首先,在Spring Boot应用中添加RabbitMQ的依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
​
2. 定义配置类

在配置类中定义队列、交换机和绑定关系:

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitMQConfig {static final String queueName = "workQueue";@BeanQueue queue() {return new Queue(queueName, false);}@BeanRabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {return new RabbitTemplate(connectionFactory);}@BeanSimpleMessageListenerContainer container(ConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.setQueueNames(queueName);container.setMessageListener(listenerAdapter);return container;}@BeanMessageListenerAdapter listenerAdapter(Receiver receiver) {return new MessageListenerAdapter(receiver, "receiveMessage");}
}
​
3. 定义生产者

生产者用于发送消息到队列:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class Producer {@Autowiredprivate RabbitTemplate rabbitTemplate;public void send(String message) {rabbitTemplate.convertAndSend(RabbitMQConfig.queueName, message);System.out.println("Sent: " + message);}
}
​
4. 定义消费者

消费者用于接收和处理消息:

import org.springframework.stereotype.Component;@Component
public class Receiver {public void receiveMessage(String message) {System.out.println("Received: " + message);}
}
​

5. 测试

在Spring Boot应用的入口类中测试消息的发送和接收:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RabbitMQApplication implements CommandLineRunner {@Autowiredprivate Producer producer;public static void main(String[] args) {SpringApplication.run(RabbitMQApplication.class, args);}@Overridepublic void run(String... args) throws Exception {for (int i = 0; i < 10; i++) {producer.send("Message " + i);}}
}
http://www.lryc.cn/news/578509.html

相关文章:

  • k8s将service的IP对应的不同端口分配到不同的pod上
  • Vue 3 中的 `h` 函数详解
  • CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
  • 【Python】字典get方法介绍
  • 面试拷打-20250701
  • 计网学习笔记第1章 计算机网络体系结构(灰灰题库)
  • 论文阅读笔记 NoPoSplat
  • 笔记/计算机网络
  • 动手学深度学习13.5. 多尺度目标检测-笔记练习(PyTorch)
  • 推客系统小程序终极指南:从0到1构建自动裂变增长引擎,实现业绩10倍增长!
  • (JAVA)自建应用调用企业微信API接口,实现消息推送
  • uniapp+vue写小程序页面,实现一张图片默认放大后,可以在容器内上下左右拖动查看
  • android13 如何定时输出app的帧率FPS
  • 应急响应类题练习——玄机第五章 Windows 实战-evtx 文件分析
  • mac重复文件清理,摄影师同款清理方案
  • COCO、VOC 和 YOLO三种主流目标检测数据格式的详细对比与示例说明
  • Java Selenium反爬虫技术方案
  • 笔记本电脑怎样投屏到客厅的大电视?怎样避免将电脑全部画面都投出去?
  • 基于c#语言的股票模拟交易软件的开发与实现
  • Vue3 使用 i18n 实现国际化完整指南
  • AiPy实战(7):一键生成天气组件,解放UI设计的双手
  • 应用场景全解析:飞算 JavaAI 的实战舞台
  • 业界优秀的零信任安全管理系统产品介绍
  • 启用不安全的HTTP方法
  • 内部类与Lambda的衍生关系(了解学习内部类,Lambda一篇即可)
  • [6-02-01].第48节:场景整合 -搭建父项目
  • DIC技术在金属材料裂纹尖端张开位移(COD)分析中的创新应用
  • Badoo×亚矩云手机:社交约会革命的“云端心跳加速剂“
  • 企业上网行为管理:零信任安全产品的对比分析
  • 3D 商品展示与 AR 试戴能为珠宝行业带来一些便利?