Why declare Exchange in RabbitMQ?
Asked Answered
P

1

5

I am working on a project with RabbitMQ. My code is below.

Producer:

public static void Main()
{
    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare("hello", false, false, false, null);

            string message = "Hello World!";
            var body = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish("", "hello", null, body);
            Console.WriteLine(" [x] Sent {0}", message);
        }
    }
}

Consumer with Exchange declared:

public static void Main()
{
    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare("hello", "direct",false, false, false, null);
            channel.QueueDeclare("hello", false, false, false, null);

            var consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume("hello", true, consumer);

            Console.WriteLine(" [*] Waiting for messages." +
                                     "To exit press CTRL+C");
            while (true)
            {
                var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
            }
        }
    }

Consumer without Exchange declared:

public static void Main()
{
    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare("hello", false, false, false, null);

            var consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume("hello", true, consumer);

            Console.WriteLine(" [*] Waiting for messages." +
                                     "To exit press CTRL+C");
            while (true)
            {
                var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
            }
        }
    }

Both consumer code works well, so what's the main use of declaring exchange? I am confused. Can anyone clarify?

Pectinate answered 14/9, 2014 at 9:52 Comment(0)
D
16

Publishing to queues lets you only implement basic publish-subscribe scenarios, where the producer and consumer use the exact queue. In case of multiple consumers a single queue of messages is distributed between multiple consumers.

Publishing to exchanges lets you create complicated scenarios, because of routing between exchanges and queues.

For example, a fanout exchange routes messages to all bound queues. This way, you can have one producer and multiple consumers and each message is copied to all bound queues independently and received independently.

Another example of exchange, a topic exchange routes messages to bound queues based on routing key in a message and a pattern on a queue. This introduces an interesting possibility of tagging messages and delivering them conditionally.

For a complete reference of exchange types and their profiles refer to the documentation:

https://www.rabbitmq.com/tutorials/amqp-concepts.html

https://www.rabbitmq.com/getstarted.html

Discriminate answered 14/9, 2014 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.