简体   繁体   English

python stomp从activemq接收消息数

[英]python stomp receive number of messages from activemq

Is it possible to receive only a number of messages from activemq.是否可以只接收来自 activemq 的一些消息。

Let say I need to receive only 100 messages from queue, is it possible.假设我只需要从队列中接收 100 条消息,是否可能。

I am using message listener method, is there any other method to receive messages.我正在使用消息侦听器方法,是否还有其他方法来接收消息。

example code snippet:示例代码片段:

queue_messages = []

class SampleListener(object):
    def on_message(self, headers, msg):
        queue_messages.append(msg)
def read_messages():
    queue_connection = stomp.Connection([(activemq_host, int(activemq_port))])
    queue_connection.start()
    queue_connection.connect('admin', 'admin')
    queue_connection.set_listener('SampleListener', SampleListener())
    queue_connection.subscribe(destination=activemq_input_q, id=1, ack='auto')
    time.sleep(1)
    queue_connection.disconnect()

read_messages()

Why don't you share your problem rather than the solution in your mind?你为什么不分享你的问题而不是你心中的解决方案? Chances are the problem might not be a problem as you think or there can be better solutions.很有可能问题可能不是您想的那样,或者可以有更好的解决方案。

To answer your question, yes you can.要回答你的问题,是的,你可以。 For ActiveMQ case, you can add extra header like {'activemq.prefetchSize':100}, ans set ack='client', when you subscribe the queue.对于 ActiveMQ 情况,您可以在订阅队列时添加额外的标头,例如 {'activemq.prefetchSize':100}, ans set ack='client'。 But you do not acknowledge the messages at all.但是你根本不承认这些消息。 The consequence is you will not receive any more messages than 100.结果是您将不会收到超过 100 条的消息。

It is a awkward solution I must say.我必须说这是一个尴尬的解决方案。 Your code will end up with consuming the first 100 messages in the queue and that's it.您的代码最终将消耗队列中的前 100 条消息,仅此而已。 You can apparently disconnect and resubscribe the same queue to receive the next 100 messages.您显然可以断开连接并重新订阅同一队列以接收接下来的 100 条消息。

when ack='client' and i don't acknowledge the message on on_message event, when actually the acknowledgement will be sent to the server, will it send the acknowledgement on successful disconnect.当 ack='client' 并且我不确认 on_message 事件上的消息时,当实际确认将发送到服务器时,它是否会在成功断开连接时发送确认。

Also, if i abruptly kill the script, will be acknowledgement be still sent and will i miss the messages此外,如果我突然终止脚本,仍会发送确认消息,并且我会错过消息吗

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM