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 :
<a href="http://my.oschina.net/moooofly/blog/142128#">?</a>
1
2
3
<code>map<string, object> args =</code><code>new</code> <code>hashmap<string, object>();</code>
<code>args.put(</code><code>"x-max-length"</code><code>,</code><code>10</code><code>);</code>
<code>channel.queuedeclare(</code><code>"myqueue"</code><code>,</code><code>false</code><code>,</code><code>false</code><code>,</code><code>false</code><code>, args);</code>
configuration 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”:
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
this applies a maximum length of 10 to the queue called short.
这使得名字叫 short 的 queue 的最大消息保存量被限制在 10 。