当前位置: 首页 > 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.dtcms.com/a/263656.html

相关文章:

  • C++仿函数与谓词深度解析:函数对象的艺术
  • android apk签名
  • 文件系统之配置网络参数
  • SiFli 52 UART的RX唤醒MCU怎么做
  • 飞算 JavaAI:我的编程强力助推引擎
  • Vue Vue-route (3)
  • Web性能测试常用指标(转自百度AI)
  • PHP爬虫实战指南:获取淘宝商品详情
  • 飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
  • 图神经网络(篇一)-GraphSage
  • CyclicBarrier(同步屏障)是什么?它的原理和用法是什么?
  • 新手向:从零开始Node.js超详细安装、配置与使用指南
  • Embeddings模型
  • 微服务介绍
  • Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
  • 【RTSP从零实践】4、使用RTP协议封装并传输AAC
  • 学习threejs,使用自定义GLSL 着色器,生成艺术作品
  • 电机参数测量
  • 自由学习记录(66)
  • JT808教程:消息的结构
  • react中在Antd3.x版本中 Select框在单选时 选中框的高度调整
  • Qt 实现Opencv功能模块切换界面功能
  • 【算法】动态规划:python实现 1
  • TensorFlow内核剖析:分布式TensorFlow架构解析与实战指南
  • mini-electron使用方法
  • 内部类与Lambda的衍生关系(了解学习内部类,Lambda一篇即可)
  • C# WPF + Helix Toolkit 实战:用两种方式打造“六面异色立方体”
  • QNN SDK学习笔记
  • 二十八、【环境管理篇】灵活应对:多测试环境配置与切换
  • python开发|yaml用法知识介绍