当前位置: 首页 > 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.lqws.cn/news/593515.html

相关文章:

  • 信息化项目验收,软件工程评审和检查表单
  • Qt中使用QSettings数据或结构体到INI文件
  • 边缘人工智能与医疗AI融合发展路径:技术融合与应用前景(下)
  • 区块链存证:数字时代的法律盾牌还是技术乌托邦?
  • 数据结构day5——队列和树
  • 县级智慧水务一体化方案及落地案例PPT(39页)
  • 8.Docker镜像讲解
  • 高强螺栓的计算与选用
  • 深入金融与多模态场景实战:金融文档分块技术与案例汇总
  • Qt时间显示按钮功能详解
  • 【docker】unknown shorthand flag: ‘f‘ in -f See ‘docker --help‘.
  • 实变与泛函题解-心得笔记【16】
  • Electron 应用中的内容安全策略 (CSP) 全面指南
  • MySQL索引深度解析:B+树、B树、哈希索引怎么选?
  • 机器学习在智能金融风险评估中的应用:信用评分与欺诈检测
  • day48
  • C++ 网络编程(13) asio多线程模型IOServicePool
  • CAU数据挖掘实验 表分析数据插件
  • 零信任安全管理系统介绍
  • 安防监控视频汇聚平台EasyCVR v3.7.2版云端录像无法在web端播放的原因排查和解决方法
  • 笔记本电脑怎样投屏到客厅的大电视?怎样避免将电脑全部画面都投出去?
  • Rust 是什么
  • [C#] WPF - 自定义样式(Slider篇)
  • WPF学习(三)
  • 08跨域
  • vue-i18n+vscode+vue 多语言使用
  • Mac 部署Latex OCR并优化体验(打包成App并支持全局快捷键)
  • WebSocket技术全面解析:从历史到实践
  • (Python)Python基础语法介绍(二)(Python基础教学)
  • 老年护理实训室建设方案:打造沉浸式护理实训环境