AMQP/RabbitMQ - Process messages sequentially

怎甘沉沦 提交于 2019-12-05 19:24:41

Usually the point of a MQ system is to distribute workload. Of course, there are some situations where processing of message N depends on result of processing the message N-1, or even the N-1 message itself.

If A and B can't process messages at the same time, then why not just have A or just B? As I see it, you are not saving anything with having 2 consumers in a way that one can work only when the other one is not...

In your case, it would be best to have one consumer but to actually do the parallelisation (not a word really) on the processing part.

Just to add that RMQ is distributing messages evenly to all consumers (in round-robin fashion) regardless on any criteria. Of course this is when prefetch is set to 1, which by default it is. More info on that here, look for "fair dispatch".

One approach to handling failover in a case where you want redundant consumers but need to process messages in a specific order is to use the exclusive consumer option when setting up the bind to the queue, and to have two consumers who keep trying to bind even when they can't get the exclusive lock.

The process is something like this:

  1. Consumer A starts first and binds to the queue as an exclusive consumer. Consumer A begins processing messages from the queue.
  2. Consumer B starts next and attempts to bind to the queue as an exclusive consumer, but is rejected because the queue already has an exclusive consumer.
  3. On a recurring basis, consumer B attempts to get an exclusive bind on the queue but is rejected.
  4. Process hosting consumer A crashes.
  5. Consumer B attempts to bind to the queue as an exclusive consumer, and succeeds this time. Consumer B starts processing messages from the queue.
  6. Consumer A is brought back online, and attempts an exclusive bind, but is rejected now.
  7. Consumer B continues to process messages in FIFO order.

While this approach doesn't provide load sharing, it does provide redundancy.

Even though this is already answer. May be this can help others. ActiveMQ has a feature known as Single Active Consumer, which matches your case.

We can have N consumers attached to a Queue but only 1 (one) of them will be actively consuming messages from the Queue. Fail-over happens only when active consumer fails.

Kindly take a look at the link https://www.rabbitmq.com/consumers.html#single-active-consumer

Thank you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!