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 有介绍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值