Sean O'Donnell asked how to get a broadcast exchange working with
RabbitMQ, which might be of interest to the community in general.
Here's how I would do it with py-amqplib with a running RabbitMQ. Now,
this code works on an older wersion of py-amqplib using version 0.8,
so if you're using the lastest version, you may have to spend some
time forward porting this code, but the base principles are the same.

First of all, get a channel into amqp to create your exchange

import amqplib.client_0_8 as amqp
conn = amqp.Connection(host, userid, password)
channel = connection.channel()

This is where things get a bit funky. You should only worry about your
realm if you have a boatload of exchanges and etc. Just use the
default one ('/') and everything should be hunky-dory. One of the big
problems with py-amqplib and rabbitmq in general is that it really
doesn't do defaults well. Also, you have to explicitely set write to
False and read to True, even if they're (logically) mutually
exclusive, because of the way the bit protocol in amqp works.

channel.access_request(realm, active=True, write=False, read=True)
channel.declare_exchange('name.of.exchange', "topic",
auto_delete=False, durable=True)

Now, on your one or multiple receivers, run the following:

[... create a channel like above right before declaring the exchange]
queue = channel.queue_declare(exclusive=True)[0] # exclusive means
nobody else can use this queue, which is good
channel.queue_bind(queue, 'name.of.exchange', rounting_key="#")  # The
'#' rounting key is a catch all. Read up on rounting keys if you want
something more fancy
channel.basic_consume(queue,
fancy_callback_function_that_consumes_your_message) # This starts an
event loop that will pass every incoming message to your callback
function

To send a message (or publish, in AMQP speak), run the following:
[ ... create your channel, much like above, but stop right before you
do the access request thingy]
channel.access_request(realm, active=True, write=True, read=False)
message = amqp.Message( "A string containing your message",
content_type="text/plain")
channel.basic_publish(message, exchange="name.of.exchange",
rounting_key="a.rounting.key")

And, magically, everything should just work(tm) and every client
should receive the message you just published. There is much too much
boilerplate for my liking, but, trust me, py-amqplib is actually
hiding you away from a lot of the details of the protocol (if you can
believe it). I suspect more modern versions of the library make things
easier, but the base principles are the same:

* 1 topic exchange
* bind as many queues to it, each with their own routing key
* send to the exchange with a routing key
* receive from the queues

--Rory Geoghegan

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Python Ireland" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.ie/group/pythonireland?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to