5

I'm wondering is it possible to set the max number of messages in the queue?

Let's say I want to have no more than 100 msgs in queue Foo, is it possible to do?

AlGiorgio
  • 497
  • 5
  • 25
Vor
  • 33,215
  • 43
  • 135
  • 193

2 Answers2

8

Yes, it is possible.

From official documentation

The maximum length of a queue can be limited to a set number of messages by supplying the x-max-length queue declaration argument with a non-negative integer value.

AFAIK, pika's channel.queue_declare has queue_declare has arguments argument which is definitely what you want.

pinepain
  • 12,453
  • 3
  • 60
  • 65
  • 1
    Thank you, I tried it, it works, but I'm not satisfied with one thing: `"Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached."`. Do you know how to throw an exception if queue is full? Because it's silently overrides messages, for example if limit set to 10, and I publish 15 messages [0 - 14], I only get msgs from 5 to 14. With out any warnings, that 5 messages where lost – Vor Jul 15 '13 at 15:03
  • 1
    There are no chances to get messages number in queue (unless you are using admin plugin) nor throw an exception when queue is full. It looks like that you don't need RabbitMQ for this task. – pinepain Jul 15 '13 at 15:11
  • @Vor you can set a exchange for dealing "dead-lettered message", according to https://www.rabbitmq.com/dlx.html . This method will not throw an exception, but you can write some kind of code the deal with it when you received the "dead-lettered message" for the exchange, like making a warning. – bluebird_lboro Sep 13 '16 at 17:53
1

Do it like this and be happy!

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

Here in arguments you will also need to track queue overflow behaviour for your queue.

AlGiorgio
  • 497
  • 5
  • 25