ActiveMQ(5.10.0) - Wildcards and composite destinations
In this section we’ll look at two useful features of ActiveMQ: subscribing to multiple destinations using wildcards, and publishing to multiple destinations using composite destinations. ActiveMQ uses a special notation to denote a wildcard subscription; we’ll describe that in the next section.
Consume from multiple destinations using wildcards
ActiveMQ supports the concept of destination hierarchies — where the name of a destination can be used to organize messages into hierarchies, an element in the name is delimited by a dot (. ). Destination hierarchies apply to both topics and queues.
For example, if you had an application that subscribed to the latest results for sports on a Saturday afternoon, you could use the following naming convention for your topics:
<Sport>.<League>.<Team>
For example, to subscribe to the latest result for a team called Leeds in an English football game, you’d subscribe to the topic: football.division1.leeds. Now Leeds plays both football and rugby, and for convenience, you’d want to see all results for Leeds for both football and rugby for the same MessageConsumer. This is where wildcards are useful.
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 subscribe to the topic named *.*.Leeds, as shown:
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 subscribe to football.division1.*, and if you wanted to find out the latest scores for all rugby games, you could subscribe to rugby.>.
Wildcards and destination hierarchies are useful for adding flexibility to your applications, allowing for a message consumer to subscribe to more than one destination at a time. The ActiveMQ broker will scan any destination name for a match using wildcards, so generally the shorter the destination name, the better the performance.
But wildcards only work for consumers. If you publish a message to a topic named rugby.>, the message will only be sent to the topic named rugby.>, and not all topics that start with the name “rugby.” There is a way for a message producer to send a message to multiple destinations: by using composite destinations, which we look at next.
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 a retail store might want to send a message to request more inventory. So a message is sent to a queue destination at the retail store’s warehouse. But it may also want to broadcast that order to an in-store activity monitoring system. Usually you’d have to do this by sending the message twice and use two message producers—one for the queue and one for the topic. ActiveMQ supports a feature called composite destinations that allows you to send the same message to multiple destinations at once.
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 would actually be sent to the two queues from the same send operation, one queue named store.order.backoffice and one queue named store.order.warehouse.
Composite destinations can support a mixture of queues and topics at the same time. By default, you have to prepend the destination name with either queue:// or topic://. So if you want to send an order message to both the order queue and also a topic, you’d set up your message producer as follows:
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 destination = session.createQueue("example.HelloQueue, example.HelloQueue2, topic://example.HelloTopic");
MessageProducer producer = session.createProducer(destination);
Message textMessage = session.createTextMessage();
producer.send(textMessage);
Wildcards and composite destinations are powerful tools for building less-complicated and flexible applications with ActiveMQ.
ActiveMQ(5.10.0) - Wildcards and composite destinations的更多相关文章
- ActiveMQ 5.10.0 安装与配置
先在官网下载activeMQ,我这里是5.10.0. 然后在解压在一个文件夹下即可. 我这里是:D:\apache-activemq-5.10.0-bin 然后进入bin目录:D:\apache-ac ...
- ActiveMQ(5.10.0) - Configuring the JAAS Authentication Plug-in
JAAS provides pluggable authentication, which means ActiveMQ will use the same authentication API re ...
- ActiveMQ(5.10.0) - Destination-level authorization
To build upon authentication, consider a use case requiring more fine-grained control over clients t ...
- ActiveMQ(5.10.0) - Spring Support
Maven Dependency: <dependencies> <dependency> <groupId>org.apache.activemq</gro ...
- ActiveMQ(5.10.0) - 删除闲置的队列或主题
方法一 通过 ActiveMQ Web 控制台删除. 方法二 通过 Java 代码删除. ActiveMQConnection.destroyDestination(ActiveMQDestinati ...
- ActiveMQ(5.10.0) - Connection Configuration URI
An Apache ActiveMQ connection can be configured by explicitly setting properties on the ActiveMQConn ...
- ActiveMQ(5.10.0) - Building a custom security plug-in
If none of any built-in security mechanisms works for you, you can always build your own. Though the ...
- ActiveMQ(5.10.0) - Message Redelivery and DLQ Handling
When messages expire on the ActiveMQ broker (they exceed their time-to-live, if set) or can’t be red ...
- ActiveMQ(5.10.0) - hello world
Sending a JMS message public class MyMessageProducer { ... // 创建连接工厂实例 ConnectionFactory connFactory ...
随机推荐
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- matlab和FPGA中无符号数和有符号数的转化(转)
在FPGA 设计过程中经常会遇到关于数表示之间的转化问题,最常见的是无符号数和有符号数之间的转化问题.(1)在FPGA设计过程中,能够很直接的看出数字的位宽,但经常以无符号数的形式输出,在后继的处理中 ...
- ASP.NET- 使用NPOI导入导出标准Excel
尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...
- 【多校练习4签到题】HDU 4642—— Fliping game
来源:点击打开链接 看上去很难,比赛的时候光看hehe了,也没有想. 但是仔细想想,是可以想出来的.一个棋盘上每个格子摆放一个硬币,硬币有正面1和反面0之分.现在两个人可以按照规则翻硬币,选择(x,y ...
- cocos2d-x CCTableView
转自:http://www.cnblogs.com/dcxing/archive/2013/01/16/2862068.html CCTableView在游戏中一般用在背包这样场景或层中,当然也不止这 ...
- win7硬盘安装Ubuntu12.04 64位时显示Error 15: File not found.
安装Ubuntu12.04 -64位时,用EasyBCD建好引导文件重启电脑后出现如下错误: Error 15: File not found 原因一个是安装文件所在盘符不对,另一个是文件名.Ubun ...
- Hyper-V网络配置
Hyper-V虚拟交换机类型应用: 外部虚拟网络: 可以实现虚拟机之间.虚拟机和物理机.虚拟机和外部网络的通信. 生产环境不勾选“允许管理操作系统共享此网络适配器”,勾选之后会为主机创建虚拟网卡,会实 ...
- JAVA实现HTTPserver端
用java socket实现了一个简单的httpserver, 能够处理GET, POST,以及带一个附件的multipart类型的POST.尽管中途遇到了非常多问题, 只是通过在论坛和几个高手交流了 ...
- BZOJ 2705: [SDOI2012]Longge的问题 GCD
2705: [SDOI2012]Longge的问题 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...
- mysql的二级索引
mysql中每个表都有一个聚簇索引(clustered index ),除此之外的表上的每个非聚簇索引都是二级索引,又叫辅助索引(secondary indexes). 以InnoDB来说,每个Inn ...