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);
        }
    }
}

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

相关推荐
William一直在路上19 分钟前
主流分布式中间件及其选型
分布式·中间件
茫茫人海一粒沙21 分钟前
理解 Confluent Schema Registry:Kafka 生态中的结构化数据守护者
分布式·kafka
weixin_4383354024 分钟前
分布式定时任务:Elastic-Job-Lite
分布式·elasticjoblite
老友@3 小时前
服务器异常宕机或重启导致 RabbitMQ 启动失败问题分析与解决方案
服务器·rabbitmq·启动失败·宕机
hjs_deeplearning4 小时前
认知篇#10:何为分布式与多智能体?二者联系?
人工智能·分布式·深度学习·学习·agent·智能体
小毛驴8504 小时前
Windows 环境下设置 RabbitMQ 的 consumer_timeout 参数
windows·分布式·rabbitmq
wowocpp4 小时前
rabbitmq 与 Erlang 的版本对照表 win10 安装方法
java·rabbitmq·erlang
述雾学java6 小时前
Spring Cloud 服务追踪实战:使用 Zipkin 构建分布式链路追踪
分布式·spring·spring cloud·zipkin
大只鹅6 小时前
分布式部署下如何做接口防抖---使用分布式锁
redis·分布式
weixin_438335406 小时前
分布式定时任务:xxl-job
分布式