Understanding RabbitMQ Exchange Types (with Spring Boot)

Axellageraldinc Adryamarthanino
4 min readJun 20, 2019

--

Before we start, I expect you’ve understood what RabbitMQ is, what it does, and what exchange, queue, binding, and routing key are. Well, let’s begin.

There are two apps built for this article, they are called rabbitmq-producer and rabbitmq-consumer. Both apps are written in Java Spring Boot.

The Application Scheme

From the scheme above, you can see that :

  1. Direct Exchange is bound to Queue A with direct1 routing key.
  2. Direct Exchange is bound to Queue B with direct2 routing key.
  3. Topic Exchange is bound to Queue C with rabbitmq.# routing key.
  4. Topic Exchange is bound to Queue D with rabbitmq.spring.# routing key.
  5. Fanout Exchange is bound to Queue E without routing key (because fanout exchange doesn’t need one).
  6. Fanout Exchange is bound to Queue F without routing key (because fanout exchange doesn’t need one).

Inside application.properties

application.properties

Direct Exchange

Direct Exchange is an exchange which forwards messages to queues based on the messages’ routing key. Take a look at the rabbitmq-producer’s code below.

Both codes are all about binding the direct-exchange with queue A with direct1 routing key and queue B with direct2 routing key.

So, when the producer send a message to direct-exchange with direct1 routing key, it’ll be sent to queue A.

Sending a message to direct-exchange with direct1 routing key via API endpoint with Postman

See the log from rabbitmq-consumer below.

Message received in Queue A : message to direct-exchange with direct1 routing key

When the producer send a message to direct-exchange with direct2 routing key, it’ll be sent to queue B.

Sending a message to direct-exchange with direct1 routing key via API endpoint with Postman

See the log from rabbitmq-consumer below.

Message received in Queue B : message to direct-exchange with direct2 routing key

Topic Exchange

Topic exchange is the same as direct exchange, but the routing key here is also called routing pattern because the routing key is not fixed, instead it uses wildcards. Take a look at the rabbitmq-producer’s code below.

The first code is the binding between topic-exchange with queue C with rabbitmq.# routing pattern. For example routing key rabbitmq.learning, rabbitmq.spring.learning, and rabbitmq.learning.exchange matches that pattern.

The second code is the binding between topic-exchange with queue D with rabbitmq.spring.# routing pattern. For example routing key rabbitmq.spring.learning and rabbitmq.spring.learning.exchange matches that pattern.

Suppose we want to send a message with rabbitmq.learning routing key.

The message will be sent to queue C.

Message received in Queue C : message to topic-exchange with rabbitmq.learning routing key

If we want to send a message with rabbitmq.spring.learning routing key.

The message will be sent to queue C and queue D.

Message received in Queue C : message to topic-exchange with rabbitmq.spring.learning routing key
Message received in Queue D : message to topic-exchange with rabbitmq.spring.learning routing key

Fanout Exchange

The main difference between fanout exchange and the other exchanges is that fanout exchange doesn’t need routing key to forward messages to queues. Fanout exchange will forward messages to every queues bound to it. Take a look at the code below.

The first code is the binding between fanout-exchange with queue E. The second code is the binding between fanout-exchange with queue F. As you can see, there is no routing key specified in the binding process.

When we send message to fanout-exchange, the message will be forwarded to queue E and queue F.

Here is the log in rabbitmq-consumer.

Message received in Queue E : message to fanout-exchange
Message received in Queue F : message to fanout-exchange

That’s it!

That’s all about exchange types in RabbitMQ. For complete source code can be found here :

🎶🎶🎶 If you’re happy and you know it clap your hands 👏👏👏

🎶🎶🎶 Kalau kau suka hati tepuk tangan👏👏👏

--

--

Responses (2)