简体   繁体   English

使用Python3和Stomp,如何禁用activemq的预取?

[英]With Python3 & Stomp, how to disable prefetch of activemq?

I'm designing a distributed project, on win10. 我正在Win10上设计一个分布式项目。 However all messages goes to one consumer with below code. 但是,所有消息都会通过以下代码发送给一个使用者。 It caches all before handling them. 它会先缓存所有内容,然后再处理它们。 What is the correct way to to that? 正确的方法是什么?

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

Some years ago, I used ActiveMQ and stompy (I know it's a different module or probably an older version of the module) and I had the same prefetching problem and I worked around it by setting ack='client' in the subscribe call: stomp.subscribe(SETTINGS.QUEUE, ack='client') 几年前,我使用了ActiveMQ和stompy(我知道这是一个不同的模块,或者可能是该模块的旧版本),并且我遇到了相同的预取问题,并且通过在订阅调用中设置ack ='client'来解决此问题: stomp.subscribe(SETTINGS.QUEUE, ack='client')

Looking at the docs , you should be able to do the same with the module you are using. 查看文档 ,您应该能够对所使用的模块执行相同的操作。

Here's part of my old code: 这是我的旧代码的一部分:

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

I forgot what went in settings . 我忘记了settings

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

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