Guest User

Untitled

a guest
Feb 6th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. # vim: set fileencoding=utf-8 sw=4 ts=4 et :
  2.  
  3. import sys
  4.  
  5. from twisted.internet.defer import inlineCallbacks, returnValue
  6. from twisted.internet import reactor, task
  7. from twisted.internet.protocol import ClientCreator
  8.  
  9. from txamqp.protocol import AMQClient
  10. from txamqp.client import TwistedDelegate
  11. from txamqp.content import Content
  12. import txamqp.spec
  13.  
  14. RABBIT_MQ_HOST = "localhost"
  15. RABBIT_MQ_PORT = 5672
  16.  
  17. VHOST = "/"
  18. QUEUE_NAME = "connector-metro-localhost"
  19. CONSUMER_TAG = "test_consumer_tag"
  20. # Set it to 0 or None to make the concurrency problem disappear
  21. PREFETCH_COUNT = 1
  22.  
  23. credentials = {"LOGIN": "guest", "PASSWORD": "guest"}
  24.  
  25.  
  26. @inlineCallbacks
  27. def getConnection(client):
  28.     conn = yield client.connectTCP(
  29.         RABBIT_MQ_HOST, RABBIT_MQ_PORT)
  30.     yield conn.start(credentials)
  31.     returnValue(conn)
  32.  
  33.  
  34. @inlineCallbacks
  35. def getChannel(conn):
  36.     chan = yield conn.channel(1)
  37.     yield chan.channel_open()
  38.     returnValue(chan)
  39.  
  40.  
  41. @inlineCallbacks
  42. def getQueue(conn, chan):
  43.     # get the message queue on the message server
  44.     yield chan.queue_declare(queue=QUEUE_NAME, durable=True,
  45.                              exclusive=False, auto_delete=False)
  46.     if PREFETCH_COUNT is not None:
  47.         reply = yield chan.basic_qos(prefetch_count=PREFETCH_COUNT)
  48.     reply = yield chan.basic_consume(queue=QUEUE_NAME, consumer_tag=QUEUE_NAME)
  49.     # get the queue that's associated with our consumer
  50.     queue = yield conn.queue(QUEUE_NAME)
  51.     returnValue(queue)
  52.  
  53.  
  54. @inlineCallbacks
  55. def processMessage(chan, queue):
  56.     print "Queue size: %d" % len(queue.pending)
  57.     msg = yield queue.get()
  58.     print "Received: %s from channel #%s (n°%s)" % (
  59.         msg.content.body, chan.id, msg.delivery_tag)
  60.     yield chan.basic_ack(msg.delivery_tag)
  61.     print "ACKed n°%s" % msg.delivery_tag
  62.  
  63. @inlineCallbacks
  64. def sendMessage(chan):
  65.     msg = Content("blabla")
  66.     yield chan.basic_publish(exchange="perf", routing_key="perf", content=msg)
  67.     print "Published on chan #%s" % chan.id
  68.  
  69.  
  70. @inlineCallbacks
  71. def main(spec):
  72.     delegate = TwistedDelegate()
  73.     # create the Twisted consumer client
  74.     consumer = ClientCreator(
  75.         reactor, AMQClient, delegate=delegate,
  76.         vhost=VHOST, spec=spec)
  77.     # connect to the RabbitMQ server
  78.     conn = yield getConnection(consumer)
  79.     # get the channel
  80.     chan = yield getChannel(conn)
  81.     # get the message queue
  82.     queue = yield getQueue(conn, chan)
  83.     # start the workers
  84.     recv = task.LoopingCall(processMessage, chan, queue)
  85.     send = task.LoopingCall(sendMessage, chan)
  86.     recv.start(0.3)
  87.     reactor.callLater(2, send.start, 0.5)
  88.     def stop():
  89.         print "Stopping"
  90.         recv.stop()
  91.         send.stop()
  92.         reactor.stop()
  93.     reactor.callLater(15, stop)
  94.  
  95.  
  96. if __name__ == "__main__":
  97.     if len(sys.argv) != 2:
  98.         print "%s path_to_spec" % sys.argv[0]
  99.         sys.exit(1)
  100.     spec = txamqp.spec.load(sys.argv[1])
  101.     main(spec)
  102.     reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment