[rabbitmq-discuss] Message delivered even if routing key doen't match
Michael Bridgen
mikeb at lshift.net
Tue May 11 00:08:04 BST 2010
Alex,
> I'm starting to write a prototype with RabbitMQ but I've some problem to
> understand the concept of "routing key", below there's an excerpt of the
> producer:
>
> String exchangeName = "myExchange";
> channel.exchangeDeclare(exchangeName, "direct");
> String queueName = "myQueue";
> channel.queueDeclare(queueName);
> String routingKey = "testRoute";
> channel.queueBind(queueName, exchangeName, routingKey);
>
> the consumer's code is the the same except it defines a routingKey = "xyz"
(See the answer on routing algorithms, below, first.)
All of exchange.declare, queue.declare, and queue.bind are idempotent
given the same arguments; in this case though you're giving two
different arguments to queue.bind, so you end up with two bindings from
a single exchange to a single queue.
Hence messages to the exchange with a routing key of "xyz" OR a routing
key of "testRoute" will be delivered to the queue.
> Why does the consumer gets the messages even if the routing key that it
> defines is different from the one defined by the producer?
>
> Is there a document that describes the routing algorithm based on the
> routing key?
In the case above, what you're supplying is not a routing key but a
/binding key/.
Individual messages have routing keys; routing involves comparing the
message's /routing key/ with each binding's /binding key/ to see which
queues (via the bindings) should be delivered the message.
For a direct exchange, the algorithm is (informally) "deliver via all
bindings with a binding key equal to the message's routing key". A
fanout exchange ignores binding keys and routing keys, and delivers via
all bindings. A topic exchange does a kind of wildcard matching on the
routing key, with binding keys giving patterns.
Wikipedia is not bad on this:
http://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocol#Exchange_types_and_the_effect_of_bindings
Michael.
More information about the rabbitmq-discuss
mailing list