- RabbitMQ Cookbook
- Sigismondo Boschi Gabriele Santomaggio
- 420字
- 2021-07-19 18:52:48
Working with message routing using topic exchanges
Direct and topic exchanges are conceptually very similar to each other. The main difference is that direct exchanges use exact matching only to select the destination of the messages, while topic exchanges allow using pattern matching with specific wildcards.
For example, the BBC is using topic routing with RabbitMQ to route new stories to all of the appropriate RSS feeds on their websites.
You can find the example for a topic exchange at:
Chapter01/Recipe08/Java_8/src/rmqexample/topic
Getting ready
To use this recipe we need to set up the Java development environment as indicated in the Introduction section.
How to do it…
Let's start with the producer:
- Declare a topic exchange:
channel.exchangeDeclare(exchangeName, "topic", false, false, null);
- Send some messages to the exchange, using arbitrary
routingKey
values:channel.basicPublish(exchangeName, routingKey, null, jsonBook.getBytes());
Then, the consumers:
- Declare the same exchange, identical to what was done in step 1.
- Create a temporary queue:
String myQueue = channel.queueDeclare().getQueue();
- Bind the queue to the exchange using the binding key, which in this case can contain wildcards:
channel.queueBind(myQueue,exchangeName,bindingKey);
- After having created a suitable consumer object, start consuming messages as already seen in the Consuming messages recipe.
How it works…
As in the previous recipe, messages sent to a topic exchange are tagged with a string (step 2), but it is important for a topic exchange to be composed more of dot-separated words; these are supposed to be the topics of the message. For example, in our code we have used:
technology.rabbitmq.ebook sport.golf.paper sport.tennis.ebook
To consume these messages the consumer has to bind myQueue
to the exchange (step 5) using the appropriate key.
Using the topic exchange, the subscription/binding key specified in step 5 can be a sequence of dot-separated words and/or wildcards. AMQP wildcards are just:
#
: This matches zero or more words*
: This matches exactly one word
So, for example:
#.ebook
and*.*.ebook
both match the first and the third sent messagessport.#
andsport.*.*
both match the second and the third sent messages# alone
matches any message sent
In the last case the topic exchange behaves exactly like a fanout
exchange, except for the performance, which is inevitably higher when using the former.
There's more…
Again, if some messages cannot be delivered to any one queue, they are silently dropped.
The producer can detect and behave consequently when this happens, as shown in detail in the Handling unroutable messages recipe.
- JavaScript:Functional Programming for JavaScript Developers
- 微服務設計原理與架構
- Python Network Programming Cookbook(Second Edition)
- Nexus規模化Scrum框架
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Windows內核編程
- Visual Foxpro 9.0數據庫程序設計教程
- C語言程序設計實訓教程與水平考試指導
- Kotlin進階實戰
- JavaScript前端開發基礎教程
- Java從入門到精通(視頻實戰版)
- Scratch編程從入門到精通
- 計算機系統解密:從理解計算機到編寫高效代碼
- ASP.NET Core 2 High Performance(Second Edition)
- C++教程