RabbitMQ Consumer获取消息的两种方式(poll,subscribe)解析

rabbitMQ中consumer通过建立到queue的连接,创建channel对象,通过channel通道获取message,
Consumer可以声明式的以API轮询poll的方式主动从queue的获取消息,也可以通过订阅的方式被动的从Queue中消费消息,
最近翻阅了基于Java的客户端的相关源码,简单做个分析。
编程模型伪代码如下:
ConnectionFactory factory = new ConnectionFactory();
Connection conn = factory.newConnection();
Channel channel=conn.createChannel();
创建Connection需要指定MQ的物理地址和端口,是socket tcp物理连接,而channel是一个逻辑的概念,支持在tcp连接上创建多个MQ channel
以下是基于channel上的两种消费方式。

1、Subscribe订阅方式
boolean autoAck = false;
channel.basicConsume(queueName, autoAck, "myConsumerTag",
new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body)
throws IOException
{
String routingKey = envelope.getRoutingKey();
String contentType = properties.contentType;
long deliveryTag = envelope.getDeliveryTag();
// (process the message components here ...)
channel.basicAck(deliveryTag, false);
}
});

订阅方式其实是向queue注册consumer,通过rpc向queue server发送注册consumer的消息,rabbitMQ Server在收到消息后,根据消息的内容类型判断这是一个订阅消息,
这样当MQ 中queue有消息时,会自动把消息通过该socket(长连接)通道发送出去。
参见ChannelN中的方法
public String basicConsume(String queue, boolean autoAck, String consumerTag,
boolean noLocal, boolean exclusive, Map<String, Object> arguments,
final Consumer callback)
throws IOException
{
......
rpc((Method)
new Basic.Consume.Builder()
.queue(queue)
.consumerTag(consumerTag)
.noLocal(noLocal)
.noAck(autoAck)
.exclusive(exclusive)
.arguments(arguments)
.build(),
k);

try {
return k.getReply();
} catch(ShutdownSignalException ex) {
throw wrap(ex);
}
}

Consumer接收消息的过程:
创建Connection后,会启动MainLoop后台线程,循环从socket(FrameHandler)中获取数据包(Frame),调用channel.handleFrame(Frame frame)处理消息,
public void handleFrame(Frame frame) throws IOException {
AMQCommand command = _command;
if (command.handleFrame(frame)) { // 对消息进行协议assemble
_command = new AMQCommand(); // prepare for the next one
handleCompleteInboundCommand(command);//对消息消费处理
}
}
ChannelN.handleCompleteInboundCommand
---ChannelN.processAsync
----dispatcher.handleDelivery
---QueueingConsumer.handleDelivery
---this._queue.add(new Delivery(envelope, properties, body));//消息最终放到队列中
每个Consumer都有一个BlockQueue,用于缓存从socket中获取的消息。
接下来,Consumer对象就可以调用api来从客户端缓存的_queue中依次获取消息,进行消费,参见QueueingConsumer.nextDelivery()

对于这种长连接的方式,没看到心跳功能,以防止长连接的因网络等原因连接失效


2、poll API方式
ChannelN:
GetResponse basicGet(String queue, boolean autoAck)
这种方式比较简单,直接通过RPC从MQ Server端获取队列中的消息

参考:http://blog.csdn.net/yangbutao/article/details/10395599
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ消息有以下几种方式: 1. 简单模式(Simple Mode):在简单模式下,生产者直接将消息发送到队列中,消费者从队列中接收消息。这种方式是最简单的方式,适用于简单的场景。 2. 工作队列模式(Work Queue Mode):在工作队列模式下,生产者将消息发送到一个队列中,多个消费者同时监听这个队列,每个消息只能被一个消费者接收。这种方式可以实现任务的并发处理。 3. 发布/订阅模式(Publish/Subscribe Mode):在发布/订阅模式下,生产者将消息发送到一个交换机(Exchange)中,交换机将消息广播给所有绑定到它的队列。每个消费者都会接收到交换机发送的消息。这种方式适用于需要将消息广播给多个消费者的场景。 4. 路由模式(Routing Mode):在路由模式下,生产者将消息发送到一个交换机中,并指定一个路由键(Routing Key),消费者根据路由键来选择接收哪些消息。这种方式可以实现消息的有选择性地发送给特定的消费者。 5. 主题模式(Topic Mode):在主题模式下,生产者将消息发送到一个交换机中,并指定一个主题(Topic),消费者可以使用通配符来匹配主题,选择接收哪些消息。这种方式可以实现更灵活的消息过滤和选择。 6. 高级模式(Advanced Mode):除了以上几种常见的方式RabbitMQ 还提供了一些高级的消息发送方式,如消息持久化、消息优先级、消息过期等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值