天天看點

【原創】RabbitMQ 之 Queue Length Limit(翻譯)Queue Length Limit

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&lt;string, object&gt; args =</code><code>new</code> <code>hashmap&lt;string, object&gt;();</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 。