简体   繁体   English

使用python STOMP从ActiveMQ队列接收单个消息

[英]Receive single message from ActiveMQ queue using python STOMP

  • Hi, I am using stomp.py module to send and receive messages from 嗨,我正在使用stomp.py模块发送和接收来自
    ActiveMQ using python. 使用python的ActiveMQ。
  • While receiving messages listener will read multiple messages within the specified sleep time. 接收消息时,侦听器将在指定的睡眠时间内读取多条消息。
  • But I need to read only a single message. 但是我只需要阅读一条消息。 It's possible in java. 在Java中是可能的。 How can I read a single message from ActiveMQ using STOMP? 如何使用STOMP从ActiveMQ中读取一条消息?

Here is the listener script which I am testing, 这是我正在测试的监听器脚本,

import stomp
import time

class SampleListener(object):
  def on_message(self, headers, msg):
    print(msg)

conn = stomp.Connection([('localhost',61613)]) 
conn.set_listener('SampleListener', SampleListener()) 
conn.start() 
conn.connect() 
conn.subscribe(destination='queue_name', id=1, ack='auto')
time.sleep(10) # secs 
conn.disconnect()

If you only want to read one message at a time with STOMP the only way to do that is to use an ack mode such as client or client-individual so that the client is sent new messages only when it has sent an explicit ACK for the one's that it has. 如果您只想一次使用STOMP读取一条消息,那么唯一的方法就是使用ack模式,例如client或client-individual,这样,只有当客户端发送了明确的ACK消息时,客户端才会发送新消息。一个。 This also would require setting the prefetch value to one so that the broker doesn't send a batch of messages to the client. 这也需要将预取值设置为1,以使代理不会向客户端发送一批消息。

The STOMP Acknowledgement modes are defined in the spec here . 该STOMP确认模式在规范中定义在这里 The documentation for the broker's STOMP support is here . 经纪人的STOMP支持的文档在此处 The client sets the prefetch using the header 'activemq.prefetchSize' on the SUBSCRIBE as listed in the ActiveMQ documentation. 客户端使用ActiveMQ文档中列出的SUBSCRIBE上的标头'activemq.prefetchSize'设置预取。

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

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