Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using default exchange in rabbitmq-c

I am trying to connect to rabbitmq-c in centos 5.6 and test its function in c client following the steps of the website: http://www.rabbitmq.com/tutorials/tutorial-one-java.html. However, it fails when I use the default exchange.

For example, I want to send a message, "Hello world", to a queue named "myqueue" via the default exchange whose name is "(AMQP default)".

In java, here is the code:

channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

But in c, when I run rmq_new_task.c (almost the same as amqp_sendstring.c) as the examples on https://github.com/liuhaobupt/rabbitmq_work_queues_demo-with-rabbit-c-client-lib.

queuename="myqueue";
......
die_on_error(amqp_basic_publish(conn, amqp_cstring_bytes(exchange),
    amqp_cstring_bytes(routingkey), &props, amqp_cstring_bytes("Hello world")),
    "Publishing");

In the java client, we just set the parameter "exchange" to "" to tell the server that we'd send the message to a specified queue named the same as routingkey via the default exchange.

So what value should I give the second parameter "exchange" in c client (using the default exchange)? I tried to set it to "" or "amq.direct". It didnot show any error while running and seemed working well.

However, when I checked in the rabbitmq-management(http://localhost:55672/#/queues), the queue named "myqueue" did not exist!

Would someone please point me to the right direction? I'd really appreciate!

like image 571
Hugo Avatar asked Nov 30 '12 07:11

Hugo


1 Answers

Take a look at http://www.rabbitmq.com/tutorials/amqp-concepts.html and specifically look for the section entitled Default Exchange.

The usage of the default exchange is very simple.

In java you would do:

channel.basicPublish("", "hello", null, message.getBytes());

By specifying "" in says to use the default exchange. (There should be no need to use amq.direct)

As per the article above it states:

The default exchange is a direct exchange with no name (empty string) pre-declared by the broker. It has one special property that makes it very useful for simple applications: every queue that is created is automatically bound to it with a routing key which is the same as the queue name.

So that means publishing to the default exchange will only work if you have already created the queue that you want to publish to.

So you will need to create your queue before you can publish to the default exchange. Once you've done that you will start seeing your messages.

like image 118
kzhen Avatar answered Oct 16 '22 13:10

kzhen