python stomp接收来自activemq的消息数

时间:2022-06-16 00:17:03

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

1 个解决方案

#1


0  

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. But you do not acknowledge the messages at all. The consequence is you will not receive any more messages than 100.

要回答你的问题,是的,你可以。对于ActiveMQ情况,您可以在订阅队列时添加额外的标头,例如{'activemq.prefetchSize':100},并设置ack ='client'。但是你根本不承认这些消息。结果是你不会收到超过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. You can apparently disconnect and resubscribe the same queue to receive the next 100 messages.

我必须说这是一个尴尬的解决方案。您的代码最终将消耗队列中的前100条消息,就是这样。您显然可以断开连接并重新订阅同一个队列以接收接下来的100条消息。

#1


0  

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. But you do not acknowledge the messages at all. The consequence is you will not receive any more messages than 100.

要回答你的问题,是的,你可以。对于ActiveMQ情况,您可以在订阅队列时添加额外的标头,例如{'activemq.prefetchSize':100},并设置ack ='client'。但是你根本不承认这些消息。结果是你不会收到超过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. You can apparently disconnect and resubscribe the same queue to receive the next 100 messages.

我必须说这是一个尴尬的解决方案。您的代码最终将消耗队列中的前100条消息,就是这样。您显然可以断开连接并重新订阅同一个队列以接收接下来的100条消息。