Default Exchange in RabbitMQ is not a special type of Exchange in RabbitMQ like Direct Exchange or Topic Exchange. It is a special Direct Exchange with an empty name. In the Elements of AMQP article, you have sent a message directly to a Queue using the empty Exchange name. This is where Default Exchange comes to play.

The below image is from the RabbitMQ admin UI, go to the Exchanges tab and click on Exchange: (AMQP default).

Default Exchange in RabbitMQ

Remeber – The Default Exchange is implicitly bound to every Queue with a routing key equals to the Queue name.

Publish a message to Default Exchange

In the previous article – Headers Exchange we created three Queues, we will make use of one of them in this tutorial (SportsQ). If you have landed directly on this page, just take a look at the previous tutorials explaining the Queue creation.

As you can see in the below code, the exchange name is "" (empty), routingKey is same as QueueName (SportsQ).

Java
package com.amqp.basic.queue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class MessagePublisher {
  public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    Connection connection = factory.newConnection("amqp://guest:guest@localhost:5672/");
    Channel channel = connection.createChannel();
    for (int i = 0; i < 4; i++) {
      String message = "Default Exchange Example" + i;
  
      //publish - (exchange, routingKey, properties, message)
      channel.basicPublish("", "SportsQ", null, message.getBytes());
    }
    channel.close();
    connection.close();
  }
}

The Message Subscription is the same as other examples, just assign a consumer to the Queue.

Java
package com.amqp.basic.queue;

import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class MessageSubscriber {
  public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    Connection connection = factory.newConnection("amqp://guest:guest@localhost:5672/");
    Channel channel = connection.createChannel();
  
    DeliverCallback deliverCallback = (s, delivery) -> {
      System.out.println(new String(delivery.getBody(), "UTF-8"));
    };
 
    CancelCallback cancelCallback = s -> {
      System.out.println(s);
    };
    channel.basicConsume("SportsQ", true, deliverCallback, cancelCallback);
  }
}

Output:

Default Exchange Example - Msg0
Default Exchange Example - Msg1
Default Exchange Example - Msg2
Default Exchange Example - Msg3

Conclusion:

You have learned about the default exchange, it is not any special type of exchange rather a Direct Exchange with an empty name. The example here is quite simplified just for the learning purpose. Let me know your thoughts on this article in the comments below.

By |Last Updated: April 3rd, 2024|Categories: RabbitMQ|

Table of Contents