当前位置:   article > 正文

python操作消息队列ActiveMQ案例_python查询activemq数据并保存到本地

python查询activemq数据并保存到本地
  1. import stomp
  2. import time
  3. import json
  4. queue_name = '/queue/SampleQueue'
  5. topic_name = '/topic/SampleTopic'
  6. listener_name = 'SampleListener'
  7. # class SampleListener(object):
  8. # def on_message(self, headers, message):
  9. # print ('headers: %s' % headers)
  10. # print ('message: %s' % message)
  11. class SampleListener(stomp.ConnectionListener):
  12. def on_message(self, message):
  13. # print ('headers: %s' % headers)
  14. print ('message: %s' % message)
  15. # 推送到队列queue
  16. def send_to_queue(msg):
  17. conn = stomp.Connection10([('127.0.0.1',61613)])
  18. # conn.start()
  19. conn.connect()
  20. conn.send(queue_name, msg)
  21. conn.disconnect()
  22. #推送到主题
  23. def send_to_topic(msg):
  24. conn = stomp.Connection10([('127.0.0.1',61613)])
  25. # conn.start()
  26. conn.connect()
  27. conn.send(topic_name, msg)
  28. conn.disconnect()
  29. ##从队列接收消息
  30. def receive_from_queue():
  31. conn = stomp.Connection10([('127.0.0.1',61613)])
  32. conn.set_listener(listener_name, SampleListener())
  33. # conn.start()
  34. conn.connect()
  35. conn.subscribe(queue_name)
  36. time.sleep(1) # secs
  37. conn.disconnect()
  38. ##从主题接收消息
  39. def receive_from_topic():
  40. conn = stomp.Connection10([('127.0.0.1',61613)])
  41. conn.set_listener(listener_name, SampleListener())
  42. # conn.start()
  43. conn.connect()
  44. conn.subscribe(topic_name)
  45. while 1:
  46. data = json.dumps({'name':'zhangsan','age':12})
  47. send_to_topic(data)
  48. time.sleep(3) # secs
  49. conn.disconnect()
  50. if __name__=='__main__':
  51. # send_to_queue('len 123')
  52. # receive_from_queue()
  53. receive_from_topic()

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号