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的更多相关文章

  1. ActiveMQ 5.10.0 安装与配置

    先在官网下载activeMQ,我这里是5.10.0. 然后在解压在一个文件夹下即可. 我这里是:D:\apache-activemq-5.10.0-bin 然后进入bin目录:D:\apache-ac ...

  2. ActiveMQ(5.10.0) - Configuring the JAAS Authentication Plug-in

    JAAS provides pluggable authentication, which means ActiveMQ will use the same authentication API re ...

  3. ActiveMQ(5.10.0) - Destination-level authorization

    To build upon authentication, consider a use case requiring more fine-grained control over clients t ...

  4. ActiveMQ(5.10.0) - Spring Support

    Maven Dependency: <dependencies> <dependency> <groupId>org.apache.activemq</gro ...

  5. ActiveMQ(5.10.0) - 删除闲置的队列或主题

    方法一 通过 ActiveMQ Web 控制台删除. 方法二 通过 Java 代码删除. ActiveMQConnection.destroyDestination(ActiveMQDestinati ...

  6. ActiveMQ(5.10.0) - Connection Configuration URI

    An Apache ActiveMQ connection can be configured by explicitly setting properties on the ActiveMQConn ...

  7. 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 ...

  8. 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 ...

  9. ActiveMQ(5.10.0) - hello world

    Sending a JMS message public class MyMessageProducer { ... // 创建连接工厂实例 ConnectionFactory connFactory ...

随机推荐

  1. UVaLive 6628 Grachten (水题,数学)

    题意:给定一个平面图形并且且给了几条连,求一条. 析:简单么,三角形相似,很简单就AC. 代码如下: #pragma comment(linker, "/STACK:1024000000,1 ...

  2. SQL大数据查询分页存储过程

    最后一页分页一卡死,整个网站的性能都会非常明显的下降,不知道为啥,微软有这个BUG一直没处理好.希望SQL2012里不要有这个问题就好了. 参考代码如下: -- =================== ...

  3. 调用DEDE日期时间格式整理大全

    dedecms 日期时间格式大全,大家可以根据需要选择.DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strf ...

  4. angurlajs 如何绑定Jquery

    //绑定Jqueryfunction bindJQuery() { // bind to jQuery if present; jQuery = window.jQuery;//获取windows 的 ...

  5. android ipc通信机制之之三,进程通讯方式。

    IPC通讯方式的优缺点: IPC通讯方式的对比 名称 优点 缺点 适用场景 Bundle 简单易用 只能传输Bundle支持的数据类型 四大组件的进程通信 文件共享 简单易用 不适合高并发场景,并无法 ...

  6. 在 SUSE 11 sp2 上安装 freeradius

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  7. 继承虚函数浅谈 c++ 类,继承类,有虚函数的类,虚拟继承的类的内存布局,使用vs2010打印布局结果。

    本文笔者在青岛逛街的时候突然想到的...最近就有想写几篇关于继承虚函数的笔记,所以回家到之后就奋笔疾书的写出来发布了 应用sizeof函数求类巨细这个问题在很多面试,口试题中很轻易考,而涉及到类的时候 ...

  8. C++技术问题总结-第12篇 设计模式原则

    设计模式六大原则,參见http://www.uml.org.cn/sjms/201211023.asp. 1. 单一职责原则 定义:不要存在多于一个导致类变更的原因.通俗的说,即一个类仅仅负责一项职责 ...

  9. Javascript-XMLHttpRequest对象简介

        XMLHttpRequest是Ajax的核心,通过调用XMLHttpRequest对象的属性和方法可以实现在客户端和浏览器之间进行数据的异步传输,从而实现页面的无刷新效果.   XMLHttp ...

  10. 一种快速求fibonacci第n个数的算法

    利用动态规则的思路,摒弃传统的递归做法,可以得到一种快速的求fibonacci第n个数的算法: ''' 求第n(从1开始)位fibonacci数 fibonacci数列前两位为0, 1. 后面每一位数 ...