A Single Channel with Multiple Consumers RabbitMQ

理解RabbitMQ中单通道多消费者机制及其实用性
本文深入探讨了RabbitMQ中通过单个通道注册多个消费者的实现方式及其潜在优势,解释了多消费者共享单个通道时的并发限制,并对比了使用相同消费者注册不同队列的独特应用场景。文章旨在揭示这种机制背后的设计考量和实际应用价值。

http://stackoverflow.com/questions/30696351/a-single-channel-with-multiple-consumers-rabbitmq

 

 

Typically the IModel.BasicConsume() registration is used to connect a single consumer through a single channel. However, the documentation is clear that multiple consumers can be registered through a single channel. For example:

Channel channel = ...; Consumer consumer1 = ...; Consumer consumer2 = ...; channel.basicQos(10); // Per consumer limit channel.basicConsume("my-queue1", false, consumer1); channel.basicConsume("my-queue2", false, consumer2);

The above snippet is taken from this link. The documentation goes on to clarify the disadvantage of doing this:

Each Channel has its own dispatch thread. For the most common use case of one Consumer per Channel, this means Consumers do not hold up other Consumers. If you have multiple Consumers per Channel be aware that a long-running Consumer may hold up dispatch of callbacks to other Consumers on that Channel.

The disadvantage is spelled out clearly. What is missing is the explanation of why positively it may be useful to do this. Does anyone know? What is the advantage?

Furthermore, there is another similar scenario to consider. Evidently it is also possible to multiply register the same consumer.

Channel channel = ...; Consumer consumer = ...; channel.basicConsume("my-queue1", false, "queue1-Tag", consumer); channel.basicConsume("my-queue2", false, "queue2-Tag", consumer);

In this latter case it is actually easier to envision the advantage; it allows multiple Rabbit queues to be multiplexed into a single event stream. But if it has that advantage, why is this approach so esoteric and completely undocumented? What makes this particularly weird is that the whole idea of a consumerTag ("queue1-Tag" and "queue2-Tag") seems to exist to support this scenario. Please note:

Is it possible for one consumer (one QueueingBasicConsumer) to register to multiple queues and have messages continually sent to it?

Certainly it is. When registering a Consumer you (either nominate, or) get a (server-generated) consumer tag, essentially an identifier for the Consumer. You only have to keep a map of tags so that the Consumer can know (when it is called) for which registration this callback is being made.

If you have a single channel connection to RabbitMQ, then the Consumers (each registration looks like a Consumer to the server) get called serially, so there is no concurrency. If you have many channels you can register for different queues on different channels and the distinct channels' callbacks can run concurrently.

The above Q&A came from this link.

If this is why consumerTag identifiers are used, why is this not stated clearly in the official documentation?

So to review the two scenarios:

  • What is the advantage of having multiple consumers registered through a single channel?
  • Why are multiple registrations of a single consumer practically discussed nowhere, even though it provides a seemingly self-evident benefit?

I'm coding in the context of .net and C#, though this question is really about RabbitMQ in general.

转载于:https://www.cnblogs.com/diegodu/p/4980941.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值