【问题标题】:ActiveMQ: Store message to string in pythonActiveMQ:将消息存储到python中的字符串
【发布时间】:2017-08-03 20:11:06
【问题描述】:

我想将从 ActiveMQ 中的队列中获得的消息存储到字符串中,但我不知道该怎么做。我的代码如下:

import stomp
import time

class SampleListener(object):
  def on_message(self, headers, msg):
    print(msg)

conn = stomp.Connection10()

conn.set_listener('SampleListener', SampleListener())

conn.start()

conn.connect()

conn.subscribe('test2')



time.sleep(1) # secs

conn.disconnect()

【问题讨论】:

  • 把它存储到一个字符串在哪里?在磁盘上?
  • @roganjosh 在代码上,所以我以后可以使用它。

标签: python activemq


【解决方案1】:

在您的课堂上,您只是打印收到的消息,您需要:

  • 将消息存储在列表中
  • 实例化“SampleListener”类并返回列表

请参阅下面的代码添加内容。

import stomp
import time

class SampleListener(object):

  #define your empty list used to store the messages
  def __init__(self):
    self.message_list = []

  def on_message(self, headers, msg):

    # appends new msg to message_list
    self.message_list.append((headers, msg))

    # comment this out if you dont want to print all msgs to console
    print(msg) 

conn = stomp.Connection10()

# instantiate the class
listener = SampleListener()

conn.set_listener('SampleListener', listener)

conn.start()

conn.connect()

conn.subscribe('test2')



time.sleep(1) # secs

conn.disconnect()

# messages received during the 1 second period stored in a list called "myMessages"
myMessages = listener.message_list

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 2014-09-24
    • 2015-11-07
    • 1970-01-01
    • 2015-01-14
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多