client.py creates a private queue for the server's responses and binds to it using a unique routing key. To guarantee uniqueness, it uses the session name for both the name of the queue and the routing key::
reply_to = "reply_to:" + session.name session.queue_declare(queue=reply_to, exclusive=True) session.exchange_bind(exchange="amq.direct", queue=reply_to, binding_key=reply_to)
It also creates a local queue, from which it reads the server's responses. It subscribes this queue to its private server-side queue and calls start() to start receiving messages:
local_queue_name = "local_queue" queue = session.incoming(local_queue_name) session.message_subscribe(queue=reply_to, destination=local_queue_name) queue.start()
Next, it sends some lines of poetry to the server, one line at a time, using the routing key for its private queue in the reply-to property:
lines = ["Twas brilling, and the slithy toves",
"Did gyre and gimble in the wabe.",
"All mimsy were the borogroves,",
"And the mome raths outgrabe."]
# We will use the same reply_to and routing key
# for each message
message_properties = session.message_properties()
message_properties.reply_to = session.reply_to("amq.direct", reply_to)
delivery_properties = session.delivery_properties(routing_key="request")
for line in lines:
print "Request: " + line
session.message_transfer(destination="amq.direct", message=Message(message_properties, delivery_properties, line))
Finally, we call the dump_queue() function to see the responses we have received from the server:
] dump_queue(reply_to)
Here is the definition of the dump_queue() function:
def dump_queue(queue_name):
print "Messages on queue: " + queue_name
message = 0
while True:
try:
message = queue.get(timeout=10)
content = message.body
session.message_accept(RangedSet(message.id))
print "Response: " + content
except Empty:
print "No more messages!"
break
except:
print "Unexpected exception!"
break