Book Image

RabbitMQ Cookbook

Book Image

RabbitMQ Cookbook

Overview of this book

RabbitMQ is an open source message broker software (sometimes called message-oriented middleware) that implements the Advanced Message Queuing Protocol (AMQP). The RabbitMQ server is written in the Erlang programming language and is built on the Open Telecom Platform framework for clustering and failover. Messaging enables software applications to connect and scale. Applications can connect to each other as components of a larger application or to user devices and data. RabbitMQ Cookbook touches on all the aspects of RabbitMQ messaging. You will learn how to use this enabling technology for the solution of highly scalable problems dictated by the dynamic requirements of Web and mobile architectures, based for example on cloud computing platforms. This is a practical guide with several examples that will help you to understand the usefulness and the power of RabbitMQ. This book helps you learn the basic functionalities of RabbitMQ with simple examples which describe the use of RabbitMQ client APIs and how a RabbitMQ server works. You will find examples of RabbitMQ deployed in real-life use-cases, where its functionalities will be exploited combined with other technologies. This book helps you understand the advanced features of RabbitMQ that are useful for even the most demanding programmer. Over the course of the book, you will learn about the usage of basic AMQP functionalities and use RabbitMQ to let decoupled applications exchange messages as per enterprise integration applications. The same building blocks are used to implement the architecture of highly scalable applications like today's social networks, and they are presented in the book with some examples. You will also learn how to extend RabbitMQ functionalities by implementing Erlang plugins. This book combines information with detailed examples coupled with screenshots and diagrams to help you create a messaging application with ease.
Table of Contents (19 chapters)
RabbitMQ Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Declare a topic exchange:

    channel.exchangeDeclare(exchangeName, "topic", false, false, null);
  2. Send some messages to the exchange, using arbitrary routingKey values:

    channel.basicPublish(exchangeName, routingKey, null, jsonBook.getBytes());

Then, the consumers:

  1. Declare the same exchange, identical to what was done in step 1.

  2. Create a temporary queue:

    String myQueue = channel.queueDeclare().getQueue();
  3. Bind the queue to the exchange using the binding key, which in this case can contain wildcards:

    channel.queueBind(myQueue,exchangeName,bindingKey);
  4. 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.

Tip

Using the messaging jargon, the consumer has to subscribe to the topics it's interested in.

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 messages

  • sport.# and sport.*.* 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.