AMQP: Exchange, Queue, and Routing Key, and Binding Key: Declaring and referencing a named exchange

1.5k views Asked by At

I am trying to create an exchange of type amq.fanout using rabbitmq-c, an amqp client for RabbitMQ . However, I'm getting tripped up on what exactly I have to declare for it. According to this, I have to declare the exchange name and type, and optionally durability and lifetime semantics for the exchange.

When I declare an exchange, it looks like I can supply the 'exchange', which I presume is the name of the exchange, and well as the exchange type, e.g. amq.fanout. From examples/amqp_exchange_declare.c

amqp_exchange_declare(conn,
    1,
    amqp_cstring_bytes(exchange),
    amqp_cstring_bytes(exchangetype),
    0, 0, 0, 0, 
    amqp_empty_table);

But then if I look at an example of publishing, there is a place to supply the exchange type, in the second parameter which is named exchange. For example, examples/amqp_producer.c

amqp_basic_publish(conn,
                                1,
                                amqp_cstring_bytes("amq.direct"),
                                amqp_cstring_bytes(queue_name),
                                0,
                                0,
                                NULL,
                                message_bytes)

But there's no place to supply the name of the exchange. So how can I publish to a named exchange?

And why the heck am I supplying a queue_name to publish? I should only be publishing to exchanges - queues are supposed to be hidden from the publisher. Is this all just poorly named variables in the examples or am I missing something fundamental?

1

There are 1 answers

1
Derick Bailey On BEST ANSWER

looking at the docs I see the 3rd parameter is the exchange to use.

int amqp_basic_publish   (   amqp_connection_state_t     state,
    amqp_channel_t  channel,
    amqp_bytes_t    exchange,
    amqp_bytes_t    routing_key,
    amqp_boolean_t  mandatory,
    amqp_boolean_t  immediate,
    struct amqp_basic_properties_t_ const *     properties,
    amqp_bytes_t    body 
)       

what's probably confusing is the use of the "amq.direct" exchange in many of the examples... this is not the exchange type. rather, this is a built-in default exchange that is named "amq.direct". it happens to be a "direct" exchange type, but "amq.direct" is the name of the exchange, not the type.

The naming is a bit confusing, at first... it took me nearly a year to figure this out, honestly. :)

It's common for simple examples to do a "publish to queue" where you push the message through the amq.direct exchange with the queue name as the routing key. this will publish the message directly to that queue. most of the time, this is only useful for demos or in an RPC (request/response) scenario where you need to publish a response to a specific reply-to queue.

i wrote a blog post on the relationships between exchanges, queues and bindings a while back. it may help clear up some of the confusion.

hope that helps!