Robert Godfrey wrote:
The arrival of messages from a consumer are essentially events...
which presumably should be handled by some sort of listener... that
concept doesn't occur in the API... how does the python client handle
it?

Python didn't used to have a listener, but it does now.

I'm attaching a working Python consumer app that shows how to use a listener.

Jonathan


#!/usr/bin/env python
"""
 This AMQP client uses a listener to read messages; this is
 generally the most efficient approach.

 This client requires an AMQP broker running on port 5672.
"""

import qpid
from qpid.client import Client
from qpid.content import Content
from qpid.queue import Empty

import threading

""" 
  Define a listener to handle an incoming message. This listener 
  acknowledges the message, prints the message body to standard out,
  and checks to see if it is the last message; if it is, it signals 
  termination.
"""
def listener(message):
    message.ok()
    print str(message.body)
    if message.body==final:
	   done.set()

#----- Initialization --------------------------------------

#  Initialize 'final' and 'done', used in the above method.

final = "That's all, folks!"   # In a message body, signals the last message
done = threading.Event()       # Used to terminate the program

#  Create a client and log in to it.

client = Client("127.0.0.1", 5672, qpid.spec.load("../specs/amqp.0-9.xml",
                                                  "../specs/amqp-errata.0-9.xml"))
client.start({"LOGIN": "guest", "PASSWORD": "guest"})

# Channel 0 is used for all connection methods, and can not be
# used for any other methods. It is automatically opened when
# the client starts.
#
# Open Channel 1 so we can use it to manage our queue.

channel0 = client.channel(0)

channel1 = client.channel(1)
channel1.channel_open()

#----- Read from queue using a listener -----------------------

# Now let's create a local client queue and tell it to process
# incoming messages with the listener function.
# 
# Call message_consume() to tell the broker to deliver messages
# from the AMQP queue to this local client queue. The broker will
# start delivering messages as soon as message_consume() is called.

queue = client.queue("listener")
queue.listen(listener)

channel1.message_consume(queue="listener", destination="listener")

# Wait for the messages to be delivered

done.wait(10)

#----- Cleanup ------------------------------------------------

# Clean up before exiting so there are no open threads.
#
# Close Channel 1.
# Close the connection using Channel 0, which is used for all connection methods.

channel1.channel_close()
channel0.connection_close()

Reply via email to