[rabbitmq-discuss] Basics: Queues, Routing Keys

aloharich rich.aloha at gmail.com
Fri Mar 30 00:57:01 BST 2012


I'm trying to wrap my head around exchanges,queues, routing keys. My 
assumption which code is proving incorrect is: I can create a single 
exchange, with multiple queues. A consumer can point to a particular queue 
and get messages just for that queue. Now here is where my brain loses the 
plot: I had assumed I could use routing keys to have particular consumers 
just get messages on a queue that match a particular "routing_key'.

So assuming I have a queue FOO and a queue BAR, I'd expect the below 
senders messages to queue BAR to NOT show up on queue FOO. However, when 
running the below, the queue FOO receiver receives messages if the routing 
key matches 

% python send.py a.b.c
% python recy.py a.b.c
(receives a message on QUEUE FOO since it appears routing key is matching)

Are routing keys global? Ie, if you have a key a.b.c any body on any queue 
will match that?

What I'm trying to achieve is the ability to send certain messages that 
only a group of consumers on a particular "queue" will process.

Thanks,

Rich


#!/usr/bin/env python
import pika
import sys

ex = "dev.test"
q = "BAR"

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='127.0.0.1'))
channel = connection.channel()
channel.exchange_declare(exchange=ex,
                         type='direct', durable=True)
result = channel.queue_declare(queue=q,auto_delete=False, durable=True)
print "queue %s" % result.method.queue
channel.queue_bind(exchange=ex,
                   queue=q)
routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
message = "hello"
for i in range(1,2):
    channel.basic_publish(exchange=ex,
                          routing_key=routing_key,
                          body=message,
                          properties=pika.BasicProperties(
            delivery_mode = 2, 
            ))
    print " [x] Sent %r" % (message,)


recv:

#!/usr/bin/env python
import pika
import sys

ex = "dev.test"
q = "FOO"

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='127.0.0.1'))
channel = connection.channel()
channel.exchange_declare(exchange=ex,
                         type='direct', durable=True)

result = channel.queue_declare(queue=q, durable=True)
queue_name = result.method.queue
binding_keys = sys.argv[1:]
if not binding_keys:
    print >> sys.stderr, "Usage: %s [binding_key]..." % (sys.argv[0],)
    sys.exit(1)

for binding_key in binding_keys:
    channel.queue_bind(exchange=ex,
                       queue=queue_name,
                       routing_key=binding_key)

print ' [*] Waiting for logs. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print "Routing key: %r" % (method.routing_key)
    print "Body %r" % (body,)

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120329/6b18078f/attachment.htm>


More information about the rabbitmq-discuss mailing list