Declaring, Binding To, and Publishing To a Named Exchange

71 views
Skip to first unread message

Mason Kramer

unread,
Aug 24, 2015, 2:02:54 PM8/24/15
to rabbitmq-users
Hello, this is a cross-post from Stack Overflow with a bit more elaboration. There seems to be very little activity on the rabbitmq-c tag there, so I decided to move to the list.

I'm getting confused by basics here. I'm basically trying to declare,publish to, bind to, and receive from to a named exchange of name "hello" and type "amq.fanout". 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.

The essentially confusing thing is that when I look at usages of amq_basic_publish, I don't see a place to publish to an exchange by name. The parameters are the exchange type, and the queue_name. This is confusing me to because in my mental model, when publishing in amq, you publish to a named exchange and know nothing about queues. So why is publish receiving a queue_name?

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?

Best regards,

Mason Kramer

Derick Bailey

unread,
Aug 24, 2015, 2:13:37 PM8/24/15
to rabbitm...@googlegroups.com
Hi Mason,

looking at the docs, here http://alanxz.github.io/rabbitmq-c/docs/0.5.0/amqp_8h.html#a9cada07768aa46297acdd701f96d0d7f I see the 3rd parameter is the exchange to use. 

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: http://derickbailey.com/2014/11/14/understanding-the-relationship-between-rabbitmq-exchanges-queues-and-bindings/

hope that helps!

  - Derick


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages