简体   繁体   English

如何使用Python中的stomp库从队列中读取所有消息?

[英]How to read all message from queue using stomp library in Python?

How can I read all messages from stomp queue in Python? 如何从Python的stomp队列中读取所有消息?

I wrote such code but it reads only one message and exists - how to force read all messages. 我写了这样的代码,但它只读取一条消息并存在-如何强制读取所有消息。

# 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()

You can use TestListner from Stomp Library: 您可以使用Stomp库中的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()   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python STOMP从ActiveMQ队列接收单个消息 - Receive single message from ActiveMQ queue using python STOMP 如何使用 python 和 STOMP 从 Activemq 队列读取消息 - How to read messages from Activemq queues using python and STOMP 如何使用python boto库将SQS消息从一个队列移到另一个队列? - How to move SQS message from one queue to another using python boto library? 从python STOMP创建临时队列 - Temporary queue creation from python STOMP 为什么这个 Python 代码在 stomp 库中失败 - 缺少消息参数? - why is this Python code failing in the stomp library - missing message argument? 使用任何库从 python 中的验证码中读取所有字符 - Read all characters from the captcha in python using any library 如何使用python stomp.py客户端在activemq中计划延迟消息 - How to schedule delay message in activemq using python stomp.py client 如何使用Python中的boto库获取Amazon SQS队列中的所有消息? - How to get all messages in Amazon SQS queue using boto library in Python? SQS:如何使用Python的boto库读取SQS消息的发送时间 - SQS: How can I read the sent time of an SQS message using Python's boto library 如何使用python从Azure函数将消息发送到Azure中的队列? - How to do I send a message to a queue in Azure from an Azure function using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM