diff options
| author | Gordon Sim <gsim@apache.org> | 2008-05-12 17:23:21 +0000 |
|---|---|---|
| committer | Gordon Sim <gsim@apache.org> | 2008-05-12 17:23:21 +0000 |
| commit | 3a923f1e6a96e856911d3bbf49dc7af42e16c98b (patch) | |
| tree | fbf3732cbddb43f09713652f8c1052f48582e7ed /python/examples/fanout | |
| parent | 0655ff5aceb9d53eb256a05d7beb55b1c803c8de (diff) | |
| download | qpid-python-3a923f1e6a96e856911d3bbf49dc7af42e16c98b.tar.gz | |
QPID-1044: Part of patch from Jonathan Robie + changes to verify scripts to keep automated testing working.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@655568 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/examples/fanout')
| -rwxr-xr-x | python/examples/fanout/fanout_consumer.py | 115 | ||||
| -rwxr-xr-x | python/examples/fanout/fanout_producer.py | 33 | ||||
| -rwxr-xr-x | python/examples/fanout/listener.py | 83 | ||||
| -rw-r--r-- | python/examples/fanout/verify.in | 48 |
4 files changed, 150 insertions, 129 deletions
diff --git a/python/examples/fanout/fanout_consumer.py b/python/examples/fanout/fanout_consumer.py index b82d8045ff..21fc5e8f16 100755 --- a/python/examples/fanout/fanout_consumer.py +++ b/python/examples/fanout/fanout_consumer.py @@ -13,61 +13,27 @@ from qpid.connection import Connection from qpid.datatypes import Message, RangedSet, uuid4 from qpid.queue import Empty -#----- Functions ------------------------------------------- - -def dump_queue(session, queue_name): - - print "Messages queue: " + queue_name - - consumer_tag = queue_name # Use the queue name as the consumer tag - need a unique tag - queue = session.incoming(queue_name) - - # Call message_subscribe() to tell the broker to deliver messages - # from the AMQP queue to a local client queue. The broker will - # start delivering messages as soon as message_subscribe() is called. - - session.message_subscribe(queue=queue_name, destination=consumer_tag) - session.message_flow(consumer_tag, 0, 0xFFFFFFFF) - session.message_flow(consumer_tag, 1, 0xFFFFFFFF) - - print "Subscribed to queue " + queue_name - sys.stdout.flush() - - 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 - - - # Messages are not removed from the queue until they - # are acknowledged. Using cumulative=True, all messages - # in the session up to and including the one identified - # by the delivery tag are acknowledged. This is more efficient, - # because there are fewer network round-trips. - - #if message != 0: - # message.complete(cumulative=True) - - #----- Initialization -------------------------------------- + # Set parameters for login -host=len(sys.argv) > 1 and sys.argv[1] or "127.0.0.1" -port=len(sys.argv) > 2 and int(sys.argv[2]) or 5672 +host="127.0.0.1" +port=5672 user="guest" password="guest" -amqp_spec="" +amqp_spec="/usr/share/amqp/amqp.0-10.xml" + +# If an alternate host or port has been specified, use that instead +# (this is used in our unit tests) +# +# If AMQP_SPEC is defined, use it to locate the spec file instead of +# looking for it in the default location. + +if len(sys.argv) > 1 : + host=sys.argv[1] +if len(sys.argv) > 2 : + port=int(sys.argv[2]) try: amqp_spec = os.environ["AMQP_SPEC"] @@ -75,25 +41,48 @@ except KeyError: amqp_spec="/usr/share/amqp/amqp.0-10.xml" # Create a connection. -conn = Connection (connect (host,port), qpid.spec.load(amqp_spec)) -conn.start() +socket = connect(host, port) +connection = Connection (sock=socket, spec=qpid.spec.load(amqp_spec)) +connection.start() +session = connection.session(str(uuid4())) + + +#----- Main Body ------------------------------------------- + +# Create a server-side queue and route messages to it. +# The server-side queue must have a unique name. Use the +# session id for that. +server_queue_name = session.name +session.queue_declare(queue=server_queue_name) +session.exchange_bind(queue=server_queue_name, exchange="amq.fanout") + +# Create a local queue to receive messages from the server-side +# queue. +local_queue_name = "local_queue" +local_queue = session.incoming(local_queue_name) + +# Call message_consume() to tell the server to deliver messages +# from the AMQP queue to this local client queue. -session_id = str(uuid4()) -session = conn.session(session_id) +session.message_subscribe(queue=server_queue_name, destination=local_queue_name) +session.message_flow(local_queue_name, session.credit_unit.message, 0xFFFFFFFF) +session.message_flow(local_queue_name, session.credit_unit.byte, 0xFFFFFFFF) -#----- Main Body -- ---------------------------------------- +print "Subscribed to queue " + server_queue_name +sys.stdout.flush() -# Make a unique queue name for my queue from the session ID. -my_queue = session_id -session.queue_declare(queue=my_queue) +# Initialize 'final' and 'content', variables used to identify the last message. +final = "That's all, folks!" # In a message body, signals the last message +content = "" # Content of the last message read -# Bind my queue to the fanout exchange. No routing key is required -# the fanout exchange copies messages unconditionally to every -# bound queue -session.exchange_bind(queue=my_queue, exchange="amq.fanout") +# Read the messages - acknowledge each one +message = None +while content != final: + message = local_queue.get(timeout=10) + content = message.body + session.message_accept(RangedSet(message.id)) + print content -# Dump the messages on the queue. -dump_queue(session, my_queue) #----- Cleanup ------------------------------------------------ diff --git a/python/examples/fanout/fanout_producer.py b/python/examples/fanout/fanout_producer.py index 1b5ea6995e..43d6a94c3d 100755 --- a/python/examples/fanout/fanout_producer.py +++ b/python/examples/fanout/fanout_producer.py @@ -8,21 +8,31 @@ import qpid import sys import os -from random import randint from qpid.util import connect from qpid.connection import Connection -from qpid.datatypes import Message +from qpid.datatypes import Message, uuid4 from qpid.queue import Empty #----- Initialization ----------------------------------- # Set parameters for login -host=len(sys.argv) > 1 and sys.argv[1] or "127.0.0.1" -port=len(sys.argv) > 2 and int(sys.argv[2]) or 5672 +host="127.0.0.1" +port=5672 user="guest" password="guest" -amqp_spec="" +amqp_spec="/usr/share/amqp/amqp.0-10.xml" + +# If an alternate host or port has been specified, use that instead +# (this is used in our unit tests) +# +# If AMQP_SPEC is defined, use it to locate the spec file instead of +# looking for it in the default location. + +if len(sys.argv) > 1 : + host=sys.argv[1] +if len(sys.argv) > 2 : + port=int(sys.argv[2]) try: amqp_spec = os.environ["AMQP_SPEC"] @@ -30,19 +40,22 @@ except KeyError: amqp_spec="/usr/share/amqp/amqp.0-10.xml" # Create a connection. -conn = Connection (connect (host,port), qpid.spec.load(amqp_spec)) -conn.start() +socket = connect(host, port) +connection = Connection (sock=socket, spec=qpid.spec.load(amqp_spec)) +connection.start() +session = connection.session(str(uuid4())) -session = conn.session(str(randint(1,64*1024))) #----- Publish some messages ------------------------------ # Create some messages and put them on the broker. +delivery_properties = session.delivery_properties(routing_key="routing_key") + for i in range(10): - session.message_transfer("amq.fanout", None, None ,Message("message " + str(i))) + session.message_transfer(destination="amq.fanout", message=Message(delivery_properties,"message " + str(i))) -session.message_transfer("amq.fanout", None, None, Message("That's all, folks!")) +session.message_transfer(destination="amq.fanout", message=Message(delivery_properties, "That's all, folks!")) #----- Cleanup -------------------------------------------- diff --git a/python/examples/fanout/listener.py b/python/examples/fanout/listener.py index 8997c3698f..50cd06d2a5 100755 --- a/python/examples/fanout/listener.py +++ b/python/examples/fanout/listener.py @@ -8,11 +8,15 @@ import qpid import sys -from qpid.client import Client -from qpid.content import Content -from qpid.queue import Empty -from time import sleep +import os +from qpid.util import connect +from qpid.connection import Connection +from qpid.datatypes import Message, RangedSet, uuid4 +from qpid.queue import Empty + +# +from time import sleep #----- Message Receive Handler ----------------------------- class Receiver: @@ -23,57 +27,76 @@ class Receiver: return self.finalReceived def Handler (self, message): - content = message.content.body + content = message.body + session.message_accept(RangedSet(message.id)) print content if content == "That's all, folks!": self.finalReceived = True - # Messages are not removed from the queue until they are - # acknowledged. Using cumulative=True, all messages from the session - # up to and including the one identified by the delivery tag are - # acknowledged. This is more efficient, because there are fewer - # network round-trips. - message.complete(cumulative=True) - #----- Initialization -------------------------------------- # Set parameters for login -host=len(sys.argv) > 1 and sys.argv[1] or "127.0.0.1" -port=len(sys.argv) > 2 and int(sys.argv[2]) or 5672 -amqp_spec="/usr/share/amqp/amqp.0-10-preview.xml" +host="127.0.0.1" +port=5672 user="guest" password="guest" +amqp_spec="/usr/share/amqp/amqp.0-10.xml" -# Create a client and log in to it. +# If an alternate host or port has been specified, use that instead +# (this is used in our unit tests) +# +# If AMQP_SPEC is defined, use it to locate the spec file instead of +# looking for it in the default location. -client = Client(host, port, qpid.spec.load(amqp_spec)) -client.start({"LOGIN": user, "PASSWORD": password}) +if len(sys.argv) > 1 : + host=sys.argv[1] +if len(sys.argv) > 2 : + port=int(sys.argv[2]) -session = client.session() -session.session_open() +try: + amqp_spec = os.environ["AMQP_SPEC"] +except KeyError: + amqp_spec="/usr/share/amqp/amqp.0-10.xml" + +# Create a connection. +socket = connect(host, port) +connection = Connection (sock=socket, spec=qpid.spec.load(amqp_spec)) +connection.start() +session = connection.session(str(uuid4())) #----- Read from queue -------------------------------------------- -# Now let's create a local client queue and tell it to read -# incoming messages. +# Create a server-side queue and route messages to it. +# The server-side queue must have a unique name. Use the +# session id for that. + +server_queue_name = session.name +session.queue_declare(queue=server_queue_name) +session.exchange_bind(queue=server_queue_name, exchange="amq.fanout") + +# Create a local queue to receive messages from the server-side +# queue. +local_queue_name = "local_queue" +local_queue = session.incoming(local_queue_name) + -# The consumer tag identifies the client-side queue. +# The local queue name identifies the client-side queue. -consumer_tag = "consumer1" -queue = client.queue(consumer_tag) +local_queue_name = "local_queue" +local_queue = session.incoming(local_queue_name) # Call message_subscribe() 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_subscribe() is called. -session.message_subscribe(queue="message_queue", destination=consumer_tag) -session.message_flow(consumer_tag, 0, 0xFFFFFFFF) # Kill these? -session.message_flow(consumer_tag, 1, 0xFFFFFFFF) # Kill these? +session.message_subscribe(queue=server_queue_name, destination=local_queue_name) +session.message_flow(local_queue_name, session.credit_unit.message, 0xFFFFFFFF) +session.message_flow(local_queue_name, session.credit_unit.byte, 0xFFFFFFFF) receiver = Receiver () -queue.listen (receiver.Handler) +local_queue.listen (receiver.Handler) while not receiver.isFinal (): sleep (1) @@ -84,4 +107,4 @@ while not receiver.isFinal (): # Clean up before exiting so there are no open threads. # -session.session_close() +session.close() diff --git a/python/examples/fanout/verify.in b/python/examples/fanout/verify.in index 30dfeb9e69..d4b8670de9 100644 --- a/python/examples/fanout/verify.in +++ b/python/examples/fanout/verify.in @@ -1,31 +1,27 @@ ==== fanout_producer.py.out ==== fanout_consumer.py.out | remove_uuid -Messages queue: Subscribed to queue -Response: message 0 -Response: message 1 -Response: message 2 -Response: message 3 -Response: message 4 -Response: message 5 -Response: message 6 -Response: message 7 -Response: message 8 -Response: message 9 -Response: That's all, folks! -No more messages! +message 0 +message 1 +message 2 +message 3 +message 4 +message 5 +message 6 +message 7 +message 8 +message 9 +That's all, folks! ==== fanout_consumer.pyX.out | remove_uuid -Messages queue: Subscribed to queue -Response: message 0 -Response: message 1 -Response: message 2 -Response: message 3 -Response: message 4 -Response: message 5 -Response: message 6 -Response: message 7 -Response: message 8 -Response: message 9 -Response: That's all, folks! -No more messages! +message 0 +message 1 +message 2 +message 3 +message 4 +message 5 +message 6 +message 7 +message 8 +message 9 +That's all, folks! |
