【问题标题】:With Python3 & Stomp, how to disable prefetch of activemq?使用 Python3 和 Stomp,如何禁用 activemq 的预取?
【发布时间】:2019-12-27 08:01:09
【问题描述】:

我正在设计一个分布式项目,在 win10 上。但是,所有消息都通过以下代码发送给一位消费者。它在处理它们之前缓存所有内容。正确的方法是什么?

# -*-coding:utf-8-*-
import stomp
import time

#http://localhost:8161 

queue_name = '/queue/SampleQueue'
topic_name = '/topic/SampleTopic'
listener_name = 'SampleListener'

class SampleListener(object):
    def on_message(self, headers, message):
        print ('headers: %s' % headers)
        print ('message: %s' % message)
        time.sleep(1) # blocking consumer here

def send_to_queue(msg):
    conn = stomp.Connection10([('127.0.0.1',61613)])
    conn.start()
    conn.connect()
    for i in range(50):
        conn.send(queue_name, msg)
    conn.disconnect()

def receive_from_queue():
    conn = stomp.Connection10([('127.0.0.1',61613)])
    conn.set_listener(listener_name, SampleListener())
    conn.start()
    conn.connect()
    #conn.subscribe(queue_name, {'ack': 'client-individual', 'activemq.prefetchSize': 0})
    conn.subscribe(queue_name, {'activemq.prefetchSize':1, })

    time.sleep(600) # secs
    conn.disconnect()


if __name__=='__main__':
    if True:
        send_to_queue('sample text 123')
        receive_from_queue()

【问题讨论】:

    标签: python activemq stomp prefetch


    【解决方案1】:

    几年前,我使用了 ActiveMQ 和 stompy(我知道它是一个不同的模块,或者可能是该模块的旧版本)并且我遇到了相同的预取问题,我通过在订阅中设置 ack='client' 来解决它致电:stomp.subscribe(SETTINGS.QUEUE, ack='client')

    看看docs,你应该可以对你正在使用的模块做同样的事情。

    这是我的旧代码的一部分:

    from stompy.simple import Client
    
    destination = 'yourqueuehere'
    settings = {} # some dictionary with the host, port, etc.; I forget exactly
    
    stomp = Client(**settings)
    stomp.connect()
    stomp.subscribe(destination, ack='client')
    try:
        while True:
            message = stomp.get()
            stomp.ack(message) # I have to call ack myself since I have ack='client' above
            # do something with message
    
    finally:    
        stomp.unsubscribe(destination)
        stomp.disconnect()
    

    我忘记了 settings 中的内容。

    【讨论】:

    • 谢谢,但还是不行。我这样做了:更新订阅如下:receive_from_queue(): conn.subscribe(queue_name, id=taskname, ack='client') ,更新 on_message 如下:def on_message(self, headers, message): self.conn。 ack(headers['message-id'], self.taskname) ........
    • 使用 ack=client 时,所有消息都处于待处理状态,conn.ack 根本不起作用。通过删除队列,我可以看到程序继续接收消息。疯了!
    • 我注意到您将dict 传递给subscribe 方法:conn.subscribe(queue_name, {'activemq.prefetchSize':1, })。这是在哪里记录的?我看到的文档中的订阅方法有这个签名:subscribe(destination, id, ack='auto', headers=None, **keyword_headers)
    • 是的,一个拼写错误。应该是 headers={'activemq.prefetchSize':1, }
    猜你喜欢
    • 2012-03-20
    • 2014-04-19
    • 2019-10-28
    • 1970-01-01
    • 2014-09-01
    • 2019-02-08
    • 2016-01-27
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多