2016-11-28 12 views
0

Pythonのstompキューからすべてのメッセージを読むにはどうすればいいですか?Pythonでstompライブラリを使用してキューからすべてのメッセージを読み取る方法は?

私はこのようなコードを書いていますが、メッセージは1つしか読み込まれず、すべてのメッセージを強制的に読み込む方法があります。

 
# coding=utf-8 
import stomp 
import logging 
from medptr.farm.farm import FarmSettings 
import platform 
import os 



if __name__ == '__main__': 
    logging.basicConfig(level=logging.DEBUG) 

    logger = logging.getLogger(__name__) 

    class ConnectionListener(stomp.ConnectionListener): 
     def __init__(self, connection): 
      self.connection = connection 
      " Current connection. " 

     def on_error(self, headers, body): 
      logger = logging.getLogger(__name__) 
      logger.error('Stomp connection error headers = %s and body = %s.' % (headers, body)) 

     def on_message(self, headers, message): 
      logger = logging.getLogger(__name__) 
      logger.debug('Stomp new message headers = %s and body = %s.' % (headers, message)) 

    farm = FarmSettings.get_by_hostname() 

    conn = stomp.Connection12(host_and_ports=farm.active_mq_settings.hosts_and_ports) 
    conn.set_listener('message', ConnectionListener(conn)) 
    conn.set_listener('print', stomp.PrintingListener()) 
    conn.set_listener('stats', stomp.StatsListener()) 
    conn.start() 
    conn.connect(username=farm.active_mq_settings.username, passcode=farm.active_mq_settings.passcode, wait=True) 
    subscribe_id = '-'.join(map(str, (platform.node(), os.getppid(), os.getpid()))) 
#   conn.set_listener('stats', stomp.StatsListener()) 
#   conn.set_listener('print', stomp.PrintingListener()) 
    conn.send('queue/test', 'hello') 
    conn.subscribe(destination='queue/test', id=subscribe_id, ack='client-individual') 
    conn.unsubscribe(id=subscribe_id) 
    conn.disconnect() 
    conn.stop() 

答えて

0

あなたはストンプライブラリからTestListnerを使用することができます。

conn = stomp.Connection([(host, 61613)]) #This is the default stomp port 
listener = TestListener() 
conn.set_listener('', listener) 
conn.start() 
conn.connect(username, password, wait=True) 
conn.subscribe(destination=queue_name, id=1, ack='auto') 
listener.message_list #This can read all the messages from the queue 
headers, message = listener.get_latest_message() #This can read the last message from the queue 
conn.unsubscribe(queue_name) 
conn.disconnect() 
関連する問題