【发布时间】:2013-07-13 09:29:19
【问题描述】:
我想知道是否可以设置队列中的最大消息数?
假设我希望队列 Foo 中的消息不超过 100 条,有可能吗?
【问题讨论】:
标签: python rabbitmq queue pika
我想知道是否可以设置队列中的最大消息数?
假设我希望队列 Foo 中的消息不超过 100 条,有可能吗?
【问题讨论】:
标签: python rabbitmq queue pika
是的,有可能。
队列的最大长度可以限制为一组 通过提供 x-max-length 队列声明参数与 一个非负整数值。
AFAIK,鼠兔的 channel.queue_declare 有 queue_declare 有 arguments 参数,这绝对是你想要的。
【讨论】:
"Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached."。如果队列已满,您知道如何抛出异常吗?因为它会默默地覆盖消息,例如,如果限制设置为 10,并且我发布了 15 条消息 [0 - 14],我只会收到从 5 到 14 的消息。如果没有任何警告,那 5 条消息会丢失
像这样去做,然后快乐!
import pika
QUEUE_SIZE = 5
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(
queue='ids_queue',
arguments={'x-max-length': QUEUE_SIZE}
)
在参数中,您还需要跟踪队列的队列溢出行为。
【讨论】: