[rabbitmq-discuss] Basics: Queues, Routing Keys

Steve Powell steve at rabbitmq.com
Fri Mar 30 11:44:23 BST 2012


Hi Rich,

The bit you're missing is the proper role of the exchange and the
routing-key and the fact that queues are *bound* to the exchange with a
binding key. Try reading
http://www.rabbitmq.com/tutorials/amqp-concepts.html, and come back here
(the reading list <rabbitmq-discuss at lists.rabbitmq.com>) if you still
don't get it.

Steve Powell
steve at rabbitmq.com
[wrk: +44-2380-111-528] [mob: +44-7815-838-558]

On 30 Mar 2012, at 00:57, aloharich wrote:

> 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()
> 
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss



More information about the rabbitmq-discuss mailing list