当前位置: 首页 > ActiveMQ in Action 读书笔记 > 正文

11.3 使用虚拟化增强JMS消息主题

11.3 Supercharge JMS topics by going virtual

If you want to broadcast a message to multiple consumers, then you use a JMS topic. Ifyou want a pool of consumers to receive messages from a destination, then you use aJMS queue. But there’s no satisfactory way to send a message to a topic and then havemultiple consumers receiving messages on that topic the way you can with queues.

11.3 使用虚拟化增强JMS消息主题

如果你想广播消息到多个消息消费者,你会使用JMS消息主题.如果你打算让多个消息消费者从一个消息目的地接收消息,你会使用JMS消息队列.但是没有令人满意的方式用来发送消息到一个主题并且想使用消息队列那样让多个消息消费者从这个主题接收消息.

The JMS spec requires that a durable subscriber to a topic use a unique JMS client IDand subscriber name. Also, only one thread (a single consumer) can be active at anytime with that unique JMS client ID and subscriber name. This means that if that subscriberdies for some reason, there will be no failover to another consumer and there’sno ability to load balance messages across competing consumers. But using JMS queuesemantics allows the ability to fail over consumers, to load balance messages amongcompeting consumers, and to use ActiveMQ message groups (see chapter 12), whichallows sticky load balancing of messages to maintain message order. Furthermore, JMSqueue depths can be monitored via JMX (see chapter 14). Using virtual topics worksaround these disadvantages while still retaining the benefits of JMS topics.

JMS规范要求持久化的主题订阅者使用唯一的JMS client ID和name属性.同样,同一时间只运行激活一个使用该唯一的JMS client ID 和订阅者name的线程.(单一的消息消费者).这就是说,如果这个订阅者因为某种原因失效后没有任何可以用来作为失效转移的消息消费者了,并且负载均衡消息也不能在处于竞争状态的消息消费者中传递.使用JMS消息队列允许消息消费者进行失效转移,允许负载均衡消息在处于竞争状态的消息消费者之间传递,并且允许使用消息群组(见12章)以便用粘性负载均衡消息来维持消息额次序.此外,JMS队列深度可以通过JMX进行监控(参见第14章).使用虚拟消息主题可以保留使用JMS主题的好处同时绕过使用主题的缺点.

Virtual topics allow a publisher to send messages to a normal JMS topic while consumersreceive messages from a normal JMS queue. So consumers subscribe to aqueue to receive messages that were published to a topic. Figure 11.1 shows a diagramof how virtual topics are structured in ActiveMQ.

虚拟主题允许消息发布者发送消息到一个正常的JMS主题,而消息消费者从一个正常的的JMS队列接收消息.因此,订阅队列的消息消费者接收到的消息实际上是发布到一个主题的.图11.1展示了ActiveMQ中虚拟主题的结构.

Some naming conventions are required to allow virtual topics to operate correctly.First, to identify that a topic is to be treated as a virtual topic, the topic name shouldalways follow the pattern of VirtualTopic.<topic name>. So if you want to create a virtualtopic for a topic whose name is orders, you need to create a destination with the nameVirtualTopic.orders. This means that a publisher sends messages to a topic namedVirtualTopic.orders. In order to consume from the queue that’s backed by the virtualtopic, consumers must subscribe to a queue whose name follows the patternConsumer.<consumer name>.VirtualTopic.<virtual topic name>.

为保证虚拟主题工作正常,需要使用一些命名规范.首先,为了标识一个主题作为虚拟主题,主题的名称需要符合下面的格式:VirtualTopic.<topic name>.因此,如果你打算创建一个名称为 orders的虚拟主题,你需要创建一个名称为VirtualTopic.orders的消息目的地.这就是说,消息发布者发送消息到名称为VirtualTopic.orders的主题.为了使用虚拟主题维护的消息队列,消息消费者必须订阅一个队列,该队列的名称需要符合下面的模式:Consumer.<consumer name>.VirtualTopic.<virtual topic name>

Suppose you want consumers to compete for messages on a queue, but you wantthat queue to be backed by a topic. You’d create a two queue receivers, each consumingfrom a queue named Consumer.Foo.VirtualTopic.orders. An example of this is shown next.
Listing 11.2 Using virtual topics

设想一下你打算让一些消息消费者争抢来自队列的消息,而这个消息队列是由消息主题维护的.你需要创建两个队列消息接受者,每个接受者都使用名称为Consumer.Foo.VirtualTopic.orders的队列中的消息.下面是示例代码:
代码清单11.2 使用虚拟主题

...
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);
Connection consumerConnection = connectionFactory.createConnection();
consumerConnection.start();
 
String queueName = "Consumer.Foo.VirtualTopic.orders";
 
// Create the first consumer for Consumer.Foo.VirtualTopic.orders
Session sessionA = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Queue fooQueueA = sessionA.createQueue(queueName);
MessageConsumer consumerA = sessionA.createConsumer(fooQueueA);
consumerA.setMessageListener(getMessageListener());
 
// Create the second consumer for Consumer.Foo.VirtualTopic.orders
Session sessionB = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Queue fooQueueB = sessionB.createQueue(queueName);
MessageConsumer consumerB = sessionB.createConsumer(fooQueueB);
consumerB.setMessageListener(getMessageListener());
 
// Create the sender
String topicName = "VirtualTopic.orders";
Connection senderConnection = connectionFactory.createConnection();
senderConnection.start();
Session senderSession = senderConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic ordersDestination = senderSession.createTopic(topicName);
MessageProducer producer = senderSession.createProducer(ordersDestination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
 
// Send 2000 messages
for (int i = 0; i < 2000; ++i) 
{
  TextMessage message = createMessage(i);
  producer.send(message);
}
...

In listing 11.2, note that two consumers are subscribed to the same queue whose namefollows the virtual topic naming pattern for the queue side. Also note that the produceris sending to a topic whose name follows the virtual topic naming pattern forthe topic side. When the 2,000 messages are sent to the topic, each consumer willreceive 1,000 messages from the queue.

在代码清单11.2中,注意到两个消息消费者订阅了同一个消息队列,该队列的命名遵循虚拟主题的中队列一侧的命名模式.同时,注意到消息生产者发送消息到消息主题,该主题的命名遵循虚拟主题中主题一侧的命名模式.当发送完2000个消息到主题后,每个消息消费者将从队列接收到1000个消息.

Virtual topics are a convenient mechanism to combine the load balancing andfailover aspects of queues, with the durability of topics. Not only does the consumernot need to worry about creating a unique JMS client ID and subscriber name, but theconsumers are competing for messages in a load balanced manner using JMS queuesemantics. If one of the consumers dies, the other consumer will continue to receiveall the messages on the queue.

虚拟主题是一种方便的机制,使用了联合消息队列的负载均衡和失效转移特性的持久化主题.不但消息消费者不选哟担心创建唯一的JMS clientID和订阅者名称属性,而且所有消息消费者可以用JMS队列语法,并以一种负载均衡的方式竞争接收消息.如果有一个消息消费者失效了,其他的消费者镜持续接收队列中所有的消息.

In the next section we’ll look at using ActiveMQ to combine the longevity of durablesubscribers, with the performance of normal topic subscribers.

下一节,我们将看到使用ActiveMQ联合持久化订阅者的长生命周期和常规主题订阅者的性能.

赞 赏

   微信赞赏  支付宝赞赏


本文固定链接: https://www.jack-yin.com/coding/translation/activemq-in-action/1699.html | 边城网事

该日志由 边城网事 于2013年12月16日发表在 ActiveMQ in Action 读书笔记 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 11.3 使用虚拟化增强JMS消息主题 | 边城网事

11.3 使用虚拟化增强JMS消息主题:目前有1 条留言

  1. 0楼
    Bell:

    为博主真心点个赞

    2015-05-26 11:25 [回复]

发表评论

快捷键:Ctrl+Enter