RabbitMQ 匿名队列详解

在小组代码 Review 时,讨论到了 RabbitMQ 的匿名队列,于是去网上查了下关于匿名队列的内容,并记录下来。

匿名队列是一种特殊的临时队列,在消息传递过程中有着独特的用途,匿名队列也被称为临时队列,它没有固定的名称,其名称由RabbitMQ 服务器自动生成,一般是类似 amq.gen-xxxxxxxxxxxx 的随机字符串。一旦消费者与队列的连接断开,该队列会自动被删除。当只需要临时接收少量消息时,使用匿名队列可以避免手动管理队列的生命周期。

于是,我们就可以了解到匿名队列的使用场景,当接收少量消息的时候,或者定时接收消息,不需要持续性消费的场景下。

还有在微服务架构中,一个服务向另一个服务发送请求,使用匿名队列接收响应消息。例如,服务A向服务B发送一个查询请求,服务A创建一个匿名队列并将队列名称包含在请求消息中发送给服务B,服务B处理请求后将响应消息发送到该匿名队列,服务A从该匿名队列接收响应。

使用匿名队列的好处有:

  • 队列名称由 RabbitMQ 自动生成,不会出现命名冲突。
  • 仅在消费者连接到队列时存在,连接关闭后队列自动删除,无需手动清理资源。
  • 通常是独占队列,即只能由创建它的消费者使用,其他消费者无法访问。

1、在Java中使用 Spring AMQP 创建和使用匿名队列的示例。

java 复制代码
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitMQConfig {

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        return connectionFactory;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

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

在上述RabbitMQConfig配置类中,创建了与 RabbitMQ 的连接工厂、管理工具和消息模板。

java 复制代码
import org.springframework.amqp.core.AnonymousQueue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RabbitMQService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String exchange, String routingKey, String message) {
        // 创建匿名队列
        AnonymousQueue anonymousQueue = new AnonymousQueue();
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        // 监听匿名队列接收消息
        receiveMessage(anonymousQueue.getName());
    }

    @RabbitListener(queues = "#{@anonymousQueue.name}")
    public void receiveMessage(String queueName) {
        Object message = rabbitTemplate.receiveAndConvert(queueName);
        if (message != null) {
            System.out.println("Received message: " + message);
        }
    }
}

在上述 RabbitMQService 的 sendMessage 方法中,使用 AnonymousQueue 类创建了一个匿名队列。通过 rabbitTemplate 发送消息,并使用@RabbitListener 注解监听匿名队列接收消息。

相关推荐
老陈说编程3 小时前
1. 鸿蒙 (HarmonyOS) 2012 至 2026 年的发展历程
分布式·华为·个人开发·harmonyos·鸿蒙·鸿蒙系统·程序员创富
xxwl58520 小时前
RabbitMQ 学习笔记
笔记·学习·rabbitmq
ly76892 天前
Redis 分布式锁的边界条件:Redlock 争议、锁续期与客户端崩溃后的互斥失效
数据库·redis·分布式·分布式锁·watchdog·redlock
灯澜忆梦2 天前
【minio】#5 | MinIO 分布式部署 + HTTPS 部署
分布式·网络协议·https·对象存储·minio
谢亮_vipxieliang3 天前
Spring Cloud 服务治理入门:注册发现、配置中心、网关限流、熔断降级与分布式一致性方案
分布式·spring·spring cloud
樱花落木兰3 天前
分布式登录实战:Session 会话共享改造,Redis 存储用户登录状态
java·javascript·数据库·redis·分布式·缓存
ShineWinsu3 天前
对于Redis:Steam、Geospatial、Hyperloglog、Bitmap、Bitfield类型的解析
数据库·c++·redis·分布式·缓存·面试·zset
Flynt4 天前
Redis Cluster主节点挂了,为什么"高可用"还全员掉线?我把三次kill的记录翻出来了
数据库·redis·分布式
JosieBook4 天前
【数据库】MySQL 实战精通系列 · 第10篇:分库分表与分布式事务实战
数据库·分布式·mysql