RabbitMQ - SpringAMQP及Work模型

发布于:2025-07-02 ⋅ 阅读:(23) ⋅ 点赞:(0)
一、概述

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";

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter 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 {

    @Autowired
    private 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 {

    @Autowired
    private Producer producer;

    public static void main(String[] args) {
        SpringApplication.run(RabbitMQApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        for (int i = 0; i < 10; i++) {
            producer.send("Message " + i);
        }
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到