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

11.1 通配符和组合消息目的地

11.1 Wildcards and composite destinations

In this section we’ll look at two useful features of ActiveMQ: subscribing to multipledestinations using wildcards, and publishing to multiple destinations using compositedestinations. ActiveMQ uses a special notation to denote a wildcard subscription; we’lldescribe that in the next section.

11.1 通配符和组合消息目的地

本节中,我们将看到ActiveMQ的两个有用的特性:通过使用通配符订阅多个消息目的地消息以及通过使用组合消息目的地发布消息到多个目的地.ActiveMQ使用特殊标记来表示通配符,下一小节中我们将看到这中通配符.

11.1.1 Consume from multiple destinations using wildcards

ActiveMQ supports the concept of destination hierarchies—where the name of a destinationcan be used to organize messages into hierarchies, an element in the name isdelimited by a dot (.). Destination hierarchies apply to both topics and queues.For example, if you had an application that subscribed to the latest results forsports on a Saturday afternoon, you could use the following naming convention foryour topics:

11.1.1 使用通配符同时处理多个消息目的地的消息

ActiveMQ支持消息目的地层次结构的概念,即,可以通过消息目的地的名称将消息组织成一种等级结构,消息目的地的名称通过点(.)来分割.消息目的地等级可以用于消息主题和消息队列.比如,你有一个应用程序用于订阅周六下午的体育比赛的最新结果,你可以使用用下面的名称示例来命名的消息主题:<Sport>.<League>.<Team> –

For example, to subscribe to the latest result for a team called Leeds in an English footballgame, you’d subscribe to the topic: football.division1.leeds. Now Leeds plays bothfootball and rugby, and for convenience, you’d want to see all results for Leeds forboth football and rugby for the same MessageConsumer. This is where wildcards areuseful.

例如,为了订阅Leeds队足球比赛的最新结果,你可以订阅下面的主题: football.division1.leeds. 现在,假如Leeds队还进行橄榄球比赛,这样为了方便,你打算使用同一个MessageConsumer来处理足球和橄榄球比赛结果,此时通配符就会发挥用处.

Three special characters are reserved for destination names:
. A dot, used to separate elements in the destination name
* Used to match one element
> Matches one or all trailing elements
以下三个字符是消息目的地名称中的保留字符:
. 一个点,用于给目的地名称分段(划分等级)
* 星号用于匹配一个分段
> 大于号匹配一个分段或所有后续分段

So to subscribe to the latest scores that all Leeds teams are playing in, you can subscribeto the topic named *.*.Leeds, as shown:

因此,为了订阅Leeds队的所有最新比分消息,你可以订阅主题名为: *.*.Leeds,代码如下所示:

String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic allLeeds = session.createTopic("*.*.Leeds");
MessageConsumer consumer = session.createConsumer(allLeeds);
Message result = consumer.receive();

If you wanted to find out the results of all the football games in Division 1, you’d subscribeto football.division1.*, and if you wanted to find out the latest scores for allrugby games, you could subscribe to rugby.>.

如果你想了解所有Division 1的比赛结果,你可以订阅football.division1.*主题;弱国你打算了解所有橄榄球比赛的比分你额可以订阅rugby.>主题.

Wildcards and destination hierarchies are useful for adding flexibility to yourapplications, allowing for a message consumer to subscribe to more than one destinationat a time. The ActiveMQ broker will scan any destination name for a match usingwildcards, so generally the shorter the destination name, the better the performance.

通配符和消息目的地按等级分段额可以增加应用程序的灵活性,使得消息消费者可以同时订阅多消息目的地.ActiveMQ会扫描所有匹配通配符的消息目的地,因此消息目的地的名称越短,程序的性能

But wildcards only work for consumers. If you publish a message to a topic namedrugby.>, the message will only be sent to the topic named rugby.>, and not all topicsthat start with the name “rugby.” There is a way for a message producer to send a messageto multiple destinations: by using composite destinations, which we look at next.

但是,通配符只能在消息消费者中使用.如果你发布一个消息到名称为rugby.>的主题,那么该消息只会被发送到名称确实为rugby.>主题(译注:并不会将rugby.>中的>当成通配符看),而不是发送到名称以rugby开头的消息主题.但是确实有一种方法使得消息生产者可以发送消息到多个消息目的地:即,使用组合消息目的地.下面我们将做阐述.

11.1.2 Sending a message to multiple destinations 

It can be useful to send the same message to different destinations at once. For example,when you need real-time analytics about your enterprise: an application used by aretail store might want to send a message to request more inventory. So a message issent to a queue destination at the retail store’s warehouse. But it may also want tobroadcast that order to an in-store activity monitoring system. Usually you’d have todo this by sending the message twice and use two message producers—one for thequeue and one for the topic. ActiveMQ supports a feature called composite destinationsthat allows you to send the same message to multiple destinations at once.

11.1.2 发送消息到多个消息目的地 

发送同样的消息到不同的消息目的地可能是有用的.当你需要实时的分析你的企业时,比如,你的零售商店需要使用一个应用程序发送消息以请求获取更多的商品存货清单.这时,消息会被发送到零售商店仓库的消息队列中.同时,你必须要广播订单到零售点内活动的监控系统中.通常,你必须使用两个消息生产者发送两次消息–一次发送到队列,另一次发送到消息主题.ActiveMQ支持一种成为组合消息目的地的特性,允许你同时发送相同的消息到多个消息目的地.

A composite destination uses a comma-separated name as the destination name.For example, if you created a queue with the name store.order.backoffice,store.order.warehouse, then the messages sent to that composite destination wouldactually be sent to the two queues from the same send operation, one queue namedstore.order.backoffice and one queue named store.order.warehouse.

组合消息目的地,使用一个以分好分割的消息目的地名称.例如,如果你创建一个名称为store.order.backoffice,store.order.warehouse的消息队列,那么发送至这个消息队列的消息将会非发送到两个队列:一个是tore.order.backoffice队列还有一个是store.order.warehouse队列.

Composite destinations can support a mixture of queues and topics at the sametime. By default, you have to prepend the destination name with either queue:// ortopic://. So for the store application scenario where you want to send an order messageto both the order queue and also a topic, you’d set up your message producer asfollows:

组合消息目的地同时支持队列和消息主题混合使用.默认情况下,你需要使用queue://或topic://前缀以便将消息目的地设置为队列和主题.所以,对于上面的零售店例子来说,如果你打算同时发送一个消息到订单队列和一个主题,你需要按照下面的方式设置你的消息生产者:

String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue ordersDestination = session.createQueue("store.orders, topic://store.orders");
MessageProducer producer = session.createProducer(ordersDestination);
Message order = session.createObjectMessage();
producer.send(order);

 (译注:”store.orders, topic://store.orders”这种方式只要制定topic://或者queue://即可,因为后面的名字和前面的一样,指定了一个,另一个也定下来了)

Wildcards and composite destinations are powerful tools for building less-complicatedand flexible applications with ActiveMQ.

通配符和组合消息目的地是构建低复杂度,高灵活性应用程序的有力工具.

Next we’ll look at the management advisory messages that the ActiveMQ brokerproduces, and how you can subscribe to them to gain useful information on changesto your ActiveMQ system.

接下来,我们将看到ActiveMQ代理生成的advisory管理消息,以及通过订阅这些advisory管理消息以便在你的ActiveMQ系统发生变化时获取有用的相关信息.

赞 赏

   微信赞赏  支付宝赞赏


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

该日志由 边城网事 于2013年12月16日发表在 ActiveMQ in Action 读书笔记 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 11.1 通配符和组合消息目的地 | 边城网事

11.1 通配符和组合消息目的地 暂无评论

发表评论

快捷键:Ctrl+Enter