You will learn to collect rejected messages in RabbitMQ. There can be several reasons to do that, for example, to track down the spammers in an Open CRM system. We will learn this with an example of a Topic Exchange, please check the previous article on Topic Exchange if you are new to RabbitMQ.

What is Alternate Exchange?

  • There will be situations when a message sent to an Exchange can not be forwarded to any of the bound Queues or bound-exchanges. These messages are discarded and get lost in rabbitMQ.
  • An alternate exchange can be defined to collect these discarded messages before they are lost.
  • Any unrouted message will be finally sent to the defined alternative-exchange.
  • Any existing exchange can be set as an alternative exchange, but a Fanout exchange is typically used to do this job as they don’t perform any filtration.

Alternate Exchange recommendations

  • Any of the 4 types of Exchanges (Direct, Fanout, Topic, and Headers) can be assigned as an alternate exchange for another exchange of any type.
  • It is recommended to use a Fanout Exchange as an alternate exchange as it forwards the message unconditionally.
  • Let’s say you want to set an alternate exchange for the exchange ex-abc. You just need to set the arguments alternate-exchange = "alt-xyz" for ex-abc. So the alt-xyz becomes an alternate queue for ex-abc.

Message routing in an Alternate Exchange

  • A producer sends a message with a routing key RK to a Topic Exchange ( It can be any type of Exchange in a real project scenario.
  • The Fanout Exchange ( is set as the alternative exchange for the topic exchange (
  • If the message routing key RK matches with any of the routing patterns using which the Queues are bound to the Topic Exchange, then the message is routed to one or more matching Queues.
  • If the message routing key does not match with any pattern, it will not be routed to any Queues. So, whenever a message can not be routed to any of its bound Queues, it is rejected.
  • The rejected messages are by default deleted by RabbitMQ. But in our case, we have the alternate exchange which will receive this rejected message and forward it to the altQueue.
  • Finally, the subscribers linked the respective queues receive the message and process it.

Implementation of an alternate exchange

We will now implement an example to demonstrate the working of the exchange as an alternate exchange. The code here is similar to the previous article, so let’s do it step by step.

Create an instance of the Connection class as a singleton object. It is safe to share the same single instance of the Connection object and that is the recommended approach.

package com.amqp.exchanges.all;

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

import java.util.concurrent.TimeoutException;

public class ConnectionManager {

  private static Connection connection = null;

   * Create RabbitMQ Connection
   * @return Connection
  public static Connection getConnection() {
    if (connection == null) {
      try {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connection = connectionFactory.newConnection("amqp://guest:guest@localhost:5672/");
      } catch (IOException | TimeoutException e) {
    return connection;

Declare the two exchange, Fanout Exchange with the name and  a topic exchange with the name We will make use of the Channel.exchangeDeclare method as shown below. Create a class as AlternateExchange and add the below code as a method in it. As you can see, the property alternate-exchange" = " sets the alternate exchange.

  public static void declareExchange() throws IOException, TimeoutException {
    Channel channel = ConnectionManager.getConnection().createChannel();
    //Declare the fanout exchange
    channel.exchangeDeclare("", BuiltinExchangeType.FANOUT, true);

    //Declare the topic exchange and set the alternate-exchange = ""
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("alternate-exchange", "");
    channel.exchangeDeclare("", BuiltinExchangeType.TOPIC, true, false, arguments);

Declare the Queues used in this example. We will create 3 Queues (HealthQ, SportsQ, EducationQ) for the Topic exchange and 1 Queue for the Fanout exchange. These Queues will be linked later to their respective Exchanges.

  public static void declareQueues() throws IOException, TimeoutException {
    //Create a channel - do not share the Channel instance
    Channel channel = ConnectionManager.getConnection().createChannel();

    //Create the Queues for "".
    channel.queueDeclare("HealthQ", true, false, false, null);
    channel.queueDeclare("SportsQ", true, false, false, null);
    channel.queueDeclare("EducationQ", true, false, false, null);

    //Create the Queues for ""
    channel.queueDeclare("altQueue", true, false, false, null);

    //Close the channel

Now link the Queues to respective Exchanges. We will use the declareBindings method. Look at the routing patterns Topic-Queues use.

  public static void declareBindings() throws IOException, TimeoutException {
    Channel channel = ConnectionManager.getConnection().createChannel();
    //Create bindings for - (queue, exchange, routingKey)
    channel.queueBind("HealthQ", "", "health.*");
    channel.queueBind("SportsQ", "", "#.sports.*");
    channel.queueBind("EducationQ", "", "");

    //Binding for fanoutExchange - and altQueue
    channel.queueBind("altQueue", "", "");


Assign subscribers to these Queues to demo the working of alternate exchange.

  public static void subscribeMessage() throws IOException, TimeoutException {
    Channel channel = ConnectionManager.getConnection().createChannel();

    DeliverCallback deliverCallback = (consumerTag, message) -> {
      System.out.println("\n" + consumerTag);
      System.out.println(new String(message.getBody()));

    CancelCallback cancelCallback = consumerTag -> {

    channel.basicConsume("HealthQ", true, deliverCallback, cancelCallback);
    channel.basicConsume("SportsQ", true, deliverCallback, cancelCallback);
    channel.basicConsume("EducationQ", true, deliverCallback, cancelCallback);

    //Consume the rejected message
    channel.basicConsume("altQueue", true, (consumerTag, message) -> {
      System.out.println("\n" + consumerTag);
      System.out.println("rejected message: " + new String(message.getBody()));
    }, consumerTag -> {

Now, let’s publish two messages to the topic exchange. As you can guess, the message with the routing key education will reach EducationQ and the second message does not reach any Queue. Hence it will be collected by the alternate-exchange

  public static void publishMessage() throws IOException, TimeoutException {
    Channel channel = ConnectionManager.getConnection().createChannel();
    String message = "Learn something new everyday";
    channel.basicPublish("", "education", null, message.getBytes());

    //No Queue for this routingKey.
    message = "Stay fit in Mind and Body";
    channel.basicPublish("", "", null, message.getBytes());


It is time to put all together inside a public static void main to test its working.

  public static void main(String[] args) throws IOException, TimeoutException {

    //Threads created to publish-subscribe asynchronously
    Thread subscribe = new Thread() {
      public void run() {
        try {
        } catch (IOException | TimeoutException e) {

    Thread publish = new Thread() {
      public void run() {
        try {
        } catch (IOException | TimeoutException e) {



Learn something new everyday

rejected message: Stay fit in Mind and Body


You have seen the working of an Alternate exchange. Let me know if you have any feedback in the comments below.