https://www.rabbitmq.com/maxlength.html
队列的最大长度可以限制为设定数量的消息,或设定的字节数(所有消息体长度的总和,忽略消息属性和任何开销),或两者。
对于任何给定队列,最大长度(任一类型)可以由使用队列参数的客户端定义,也可以在使用策略的服务器中定义。在策略和参数都指定最大长度的情况下,应用这两个值中的最小值。
队列长度设置也可以由operator policies.强制执行。
在所有情况下,都使用就绪消息的数量; 未确认的消息不计入限制。
rabbitmqctl list_queues的结果字段messages_ready和message_bytes_ready以及管理API显示将受限制的值。
默认最大队列长度限制行为
当设置最大队列长度或大小并达到最大值时,RabbitMQ的默认行为是从队列前面丢弃或写入 死信(即队列中最旧的消息)。要修改此行为,请使用下面描述的overflow设置。
队列溢出行为
使用overflow设置配置队列溢出行为。如果overflow设置为reject-publish,则将丢弃最近发布的消息。此外,如果 启用了publisher confirms,则会通过basic.nack消息通知发布者拒绝 。如果邮件被路由到多个队列并被至少其中一个队列拒绝,则该频道将通过basic.nack通知发布者。该消息仍将发布到可以将其排队的所有其他队列。
Define Max Queue Length Using a Policy
To specify a maximum length using policy, add the key max-length and / or max-length-bytesto a policy definition. For example:
rabbitmqctl | rabbitmqctl set_policy my-pol "^one-meg$" \ '{"max-length-bytes":1048576}' \ --apply-to queues |
---|---|
rabbitmqctl on Windows | rabbitmqctl.bat set_policy my-pol "^one-meg$" ^ "{""max-length-bytes"":1048576}" ^ --apply-to queues |
The my-pol policy ensures that the one-meg queue contains no more than 1MiB of message data. When the 1MiB limit is reached, the oldest messages are discarded from the head of the queue.
To define an overflow behaviour - whether to drop messages from head or to reject new publishes, add the key overflow to a policy definition. For example:
rabbitmqctl | rabbitmqctl set_policy my-pol "^two-messages$" \ '{"max-length":2,"overflow":"reject-publish"}' \ --apply-to queues |
---|---|
rabbitmqctl on Windows | rabbitmqctl.bat set_policy my-pol "^two-messages$" ^ "{""max-length"":2,""overflow"":""reject-publish""}" ^ --apply-to queues |
The my-pol policy ensures that the two-messages queue contains no more than 2 messages and all additional publishes are sent basic.nack responses as long as the queue contains 2 messages and publisher confirms are enabled.
Policies can also be defined using the management plugin, see the policy documentation for more details.
Define Max Queue Length Using x-arguments During Declaration
Maximum number of messages can be set by supplying the x-max-length queue declaration argument with a non-negative integer value.
Maximum length in bytes can be set by supplying the x-max-length-bytes queue declaration argument with a non-negative integer value.
If both arguments are set then both will apply; whichever limit is hit first will be enforced.
Overflow behaviour can be set by supplying the x-overflow queue declaration argument with a string value. Possible values are drop-head (default) or reject-publish
This example in Java declares a queue with a maximum length of 10 messages:
Map<String, Object> args = new HashMap<String, Object>(); args.put("x-max-length", 10); channel.queueDeclare("myqueue", false, false, false, args);