【RabbitMQ之简单队列】

本文介绍了RabbitMQ中的简单队列模型,强调了Queue作为存储消息的核心角色。生产者将消息发送到Queue,而消费者可以从Queue接收并处理消息。在多消费者订阅同一Queue的情况下,消息会被平均分配。此外,文章还概述了生产者和消费者的编码实践,包括创建连接、channel、queue以及消息的生产和消费步骤。
摘要由CSDN通过智能技术生成

 

简单队列模型

Queue

Queue(队列)是RabbitMQ的内部对象,用于存储消息,用下图表示。

RabbitMQ中的消息都只能存储在Queue中,生产者(下图中的P)生产消息并最终投递到Queue中,消费者(下图中的C)可以从Queue中获取消息并消费。

 

多个消费者可以订阅同一个Queue,这时Queue中的消息会被平均分摊给多个消费者进行处理,而不是每个消费者都收到所有的消息并处理。

编码实践

一、生产者和消费者模型

1、创建连接(生产端)

 Connection connection = null;
 ConnectionFactory connectionFactory = new ConnectionFactory();
 connectionFactory.setHost("youHost");
 connectionFactory.setUsername("userName");
 connectionFactory.setPassword("password");
 connection = connectionFactory.newConnection();

 

2、创建channel(生产端)

Channel channel = connection.createChannel();

3、创建queue(生产端)

channel.queueDeclare("队列名称",false,false,false,null);
 
源码:
queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments)
/**
 * Construct a new queue, given a name, durability flag, and auto-delete flag, and arguments.
 * @param name the name of the queue - must not be null; set to "" to have the broker generate the name.
 * @param durable true if we are declaring a durable queue (the queue will survive a server restart)
 * @param exclusive true if we are declaring an exclusive queue (the queue will only be used by the declarer's
 * connection)
 *
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值