0

I have a Java consumer application that connects to a RabbitMQ (3.2.4) non-deletable fanout exchange called "my_exhange_foo":

Connection connection = connectionFactory.newConnection(consumerPool);
Channel channel = connection.createChannel();
channel.exchangeDeclare("my_exhange_foo", "fanout"); // is this necessary?

String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, "my_exhange_foo", "");

QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);

The client consumer application receives the messages regardless of whether the exchange is declared or not.

I followed the example ReceiveLogsDirect.java in this tutorial https://www.rabbitmq.com/tutorials/tutorial-four-java.html

and read the api but cannot figure out what the purpose of declaring the exchange is on the consumer side. I would appreciate if someone can shed some light on it.

Javide
  • 2,477
  • 5
  • 45
  • 61

1 Answers1

2

what is the purpose of declaring the exchange on the consumer side?

It lets one start the consumer process before the producer process has been started. Without it, if the consumer was started first then it would error. Having the flexibility to start the consumer first is useful when working with a production system, it reduces possible problems caused by the inherent timing of restarting systems.

Chris K
  • 11,622
  • 1
  • 36
  • 49