博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ActiveMQ(5.10.0) - Wildcards and composite destinations
阅读量:4315 次
发布时间:2019-06-06

本文共 4927 字,大约阅读时间需要 16 分钟。

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.

 

转载于:https://www.cnblogs.com/huey/p/5131412.html

你可能感兴趣的文章
http和webservice
查看>>
hdu1879------------prim算法模板
查看>>
jdbc之二:DAO模式
查看>>
MySQL性能优化方法一:缓存参数优化
查看>>
Angular2 - 概述
查看>>
正则表达式tab表示\t
查看>>
NodeJS+Express+MongoDB 简单实现数据录入及回显展示【Study笔记】
查看>>
Highcharts使用指南
查看>>
网络基础(子网划分)
查看>>
Google C++ Style
查看>>
微软阵营企稳的利好消息:.NET开源、Visual Studio免费
查看>>
MyBatis总结八:缓存介绍(一级缓存,二级缓存)
查看>>
div+css教程网站建设门户网站和电子商务网站CSS样式表
查看>>
[LeetCode][JavaScript]Candy
查看>>
Mybatis分页插件
查看>>
sk_buff Structure
查看>>
oracle的级联更新、删除
查看>>
多浏览器开发需要注意的问题之一
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>