ActiveMQ - Exclusive Consumer 独占消费

来自官网:http://activemq.apache.org/exclusive-consumer.html

=============================================================================

Exclusive Consumer

We have a new feature in 4.x called Exclusive Consumer or Exclusive Queues which avoids the end user having to pin anything. The broker will pick a single MessageConsumer to get all the messages for a queue to ensure ordering. If that consumer fails, the broker will auto failover and choose another consumer.

So the effect is a heterogeneous J2EE cluster where each JVM has the same setup and configuration; the broker is choosing one consumer to be the master and send all the messages to it in order until it dies; then you get immediate failover to another consumer.

For those who've struggled with pinning JMS consumers in J2EE clusters you'll immediately realize how useful this is to making clustered, high available distributed services.

Example

An Exclusive Consumer is created using Destination Options as follows:

queue = new ActiveMQQueue("TEST.QUEUE?consumer.exclusive=true");
consumer = session.createConsumer(queue);
=============================================================================

我们经常希望维持队列中的消息,按一定次序转发给消息者。然而当有多个JMS Session和消息消费者实例的从同一个队列中获取消息的时候,就不能保证消息顺序处理。因为消息被多个不同线程并发处理着。

在ActiveMQ4.x中可以采用Exclusive Consumer或者Exclusive Queues,避免这种情况,Broker会从消息队列中,一次发送消息给一个消息消费者来保证顺序。如下配置。

queue = new ActiveMQQueue("TEST.QUEUE?consumer.exclusive=true");
consumer = session.createConsumer(queue);
这样可以保证

A.当在接收信息的时候有一个或者多个备份接收消息者和一个独占消息者的同时接收时候,无论两者创建先后,在接收的时候,均为独占消息者接收。

B.当在接收信息的时候,有多个独占消费者的时候,只有一个独占消费者可以接收到消息。

C.当有多个备份消息者和多个独占消费者的时候,当所有的独占消费者均close的时候,只有一个备份消费者接到到消息。
备注:备份消费者为不带任何参数的消费者。

the broker is choosing one consumer to be the master and send all the messages to it in order until it dies; then you get immediate failover to another consumer.

总结起来就是,代理者会在消费者中选择一个作为主消费者并且将所有消息都按顺序发送给它,如果该主消费者挂掉,那么将立即启用故障切换到下一个消费者。

当然,独占消费模式虽然好,但还有更好用的Message Groups, 在官网的 http://activemq.apache.org/message-groups.html 有介绍。

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页