Guest User

Untitled

a guest
Jan 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import sys
  2. import time
  3.  
  4. # Detect if we're running in a git repo
  5. from os.path import exists, normpath
  6. if exists(normpath('../pika')):
  7. sys.path.insert(0, '..')
  8.  
  9. from pika.adapters import SelectConnection
  10. from pika.connection import ConnectionParameters
  11. from pika import BasicProperties, spec
  12. import pika.log
  13.  
  14. # We use these to hold our connection & channel
  15. connection = None
  16. channel = None
  17.  
  18.  
  19. def on_connected(connection):
  20. print "demo_send: Connected to RabbitMQ"
  21. connection.channel(on_channel_open)
  22.  
  23.  
  24. def send_close_ok(code, text):
  25. print "demo_send: Sending a Close-Ok message"
  26. channel.transport.rpc(spec.Channel.CloseOk())
  27.  
  28. def on_channel_open(channel_):
  29. print "demo_send: Received our Channel"
  30. global channel
  31. channel = channel_
  32. channel.add_on_close_callback(send_close_ok)
  33. channel.basic_publish(exchange='false', body='a', routing_key='b')
  34. print "demo_send: Waiting 1 second before reopening the channel"
  35. connection.add_timeout(1, reopen_channel)
  36.  
  37. def reopen_channel():
  38. print "demo_send: Reopening the Channel"
  39. connection.channel(on_channel_open_2)
  40.  
  41. def on_channel_open_2(channel_):
  42. global channel
  43. channel = channel_
  44. channel.add_on_close_callback(send_close_ok)
  45. print "demo_send: Reopened the Channel"
  46. channel.queue_declare(queue="test", durable=True,
  47. exclusive=False, auto_delete=False,
  48. callback=on_queue_declared)
  49.  
  50.  
  51. def on_queue_declared(frame):
  52. print "demo_send: Queue Declared"
  53. for x in xrange(0, 3):
  54. message = "Hello World #%i: %.8f" % (x, time.time())
  55.  
  56. # Create properties with when we sent the message, the app_id
  57. # user we connected with, a content type and non persisted messages
  58. properties = BasicProperties(timestamp=time.time(),
  59. app_id=__file__,
  60. user_id='guest',
  61. content_type="text/plain",
  62. delivery_mode=1)
  63.  
  64. # Send the message
  65. channel.basic_publish(exchange='',
  66. routing_key="test",
  67. body=message,
  68. properties=properties)
  69.  
  70. print "demo_send: Sent %s" % message
  71.  
  72. # Close our connection
  73. print "demo_send: Closing"
  74. connection.close()
  75.  
  76.  
  77. if __name__ == '__main__':
  78. pika.log.setup(level=pika.log.INFO)
  79.  
  80. # Connect to RabbitMQ
  81. host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1'
  82. connection = SelectConnection(ConnectionParameters(host, 5673),
  83. on_connected)
  84. # Loop until CTRL-C
  85. try:
  86. # Start our blocking loop
  87. connection.ioloop.start()
  88.  
  89. except KeyboardInterrupt:
  90.  
  91. # Close the connection
  92. connection.close()
  93.  
  94. # Loop until the connection is closed
  95. connection.ioloop.start()
Add Comment
Please, Sign In to add comment