The maximum length of a queue can be limited to a set number of messages by supplying the x-max-lengthqueue declaration argument with a non-negative integer value. Queue length is a measure that takes into account ready messages, ignoring unacknowledged messages and message size. Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached.
一个 queue 中消息最大保存量可以在声明 queue 的时候通过设置 x-max-length 参数为非负整数进行指定。Queue 长度的选取需要考量 就绪消息量、被忽略的未确认消息量,以及消息大小。当 queue 中的消息量达到了设定的上限时,为了给新消息腾出空间,将会从该 queue 用于保存消息的队列的前端将“老”消息丢弃或者 dead-lettered 。This example in Java declares a queue with a maximum length of 10 messages: 下面的 Java 示例展示了如何声明一个最多保存 10 条消息的 queue :
MapConfiguration using policy To specify a maximum length using policy, add the key "max-length" to a policy definition. For example: 通过 policy 对 queue 的最大长度进行设置,例如可以在 policy 的定义中使用 key “max-length”:args = new HashMap ();args.put("x-max-length", 10);channel.queueDeclare("myqueue", false, false, false, args);
rabbitmqctl | rabbitmqctl set_policy Ten "^short$" '{"max-length":10}' --apply-to queues |
rabbitmqctl (Windows) | rabbitmqctl set_policy Ten "^short$" "{""max-length"":10}" --apply-to queues |