I had declare a queue like below:
我已经声明了如下队列:
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-max-length-bytes", 2 * 1024 * 1024); // Max length is 2G
channel.queueDeclare("queueName", true, false, false, args);
When the queue messages count bytes is large than 2G, It will auto remove the message on the head of the queue.
But what I expected is That it reject produce the last message and return exception to the producer.
How can I get it?
当队列消息数字节数大于2G时,它将自动删除队列头部的消息。但我所期望的是,它拒绝生成最后一条消息,并将异常返回给生成器。我怎样才能得到它?
2 个解决方案
#1
0
A possible workaround is check the queue size before send your message using the HTTP API.
一个可能的解决方案是在使用HTTP API发送消息之前检查队列大小。
For example if you have a queue called: myqueuetest
with max size = 20.
例如,如果您有一个队列:myqueuetest和max size = 20。
Before send the message you can call the HTTP API in this way: http://localhost:15672/api/queues/
在发送消息之前,您可以这样调用HTTP API: http://localhost:15672/ API /queues/。
the result is a JSON
like this:
结果是这样的JSON:
"message_bytes":10,
"message_bytes_ready":10,
"message_bytes_unacknowledged":0,
"message_bytes_ram":10,
"message_bytes_persistent":0,
..
"name":"myqueuetest",
"vhost":"test",
"durable":true,
"auto_delete":false,
"arguments":{
"x-max-length-bytes":20
},
then you cloud read the message_bytes
field before send your message and then decide if send or not.
然后,在发送消息之前,您将读取message_bytes字段,然后决定是否发送消息。
Hope it helps
希望它能帮助
EDIT
编辑
- This workaround could kill your application performance
- 这种方法可能会破坏您的应用程序性能。
- This workaround is not safe if you have multi-threading/more publisher
- 如果您有多线程/更多的发布者,这个解决方案是不安全的。
- This workaround is not a very "best practise"
- 这种变通方法并不是一种“最佳实践”
Just try to see if it is ok for your application.
试着看看你的申请是否可以。
#2
0
As explained on the official docs:
正如官方文件所解释的那样:
Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached.
当到达极限时,消息将从队列的前端删除或挂起,以便为新消息腾出空间。
https://www.rabbitmq.com/maxlength.html
https://www.rabbitmq.com/maxlength.html
If you think RabbitMQ should drop messages form the end of the queue, feel free to open an issue here so we can discuss about it https://github.com/rabbitmq/rabbitmq-server/issues
如果您认为RabbitMQ应该从队列的末尾删除消息,那么您可以在这里打开一个问题,以便我们可以讨论它的https://github.com/rabbitmq/rabbitmq-server/issues。
#1
0
A possible workaround is check the queue size before send your message using the HTTP API.
一个可能的解决方案是在使用HTTP API发送消息之前检查队列大小。
For example if you have a queue called: myqueuetest
with max size = 20.
例如,如果您有一个队列:myqueuetest和max size = 20。
Before send the message you can call the HTTP API in this way: http://localhost:15672/api/queues/
在发送消息之前,您可以这样调用HTTP API: http://localhost:15672/ API /queues/。
the result is a JSON
like this:
结果是这样的JSON:
"message_bytes":10,
"message_bytes_ready":10,
"message_bytes_unacknowledged":0,
"message_bytes_ram":10,
"message_bytes_persistent":0,
..
"name":"myqueuetest",
"vhost":"test",
"durable":true,
"auto_delete":false,
"arguments":{
"x-max-length-bytes":20
},
then you cloud read the message_bytes
field before send your message and then decide if send or not.
然后,在发送消息之前,您将读取message_bytes字段,然后决定是否发送消息。
Hope it helps
希望它能帮助
EDIT
编辑
- This workaround could kill your application performance
- 这种方法可能会破坏您的应用程序性能。
- This workaround is not safe if you have multi-threading/more publisher
- 如果您有多线程/更多的发布者,这个解决方案是不安全的。
- This workaround is not a very "best practise"
- 这种变通方法并不是一种“最佳实践”
Just try to see if it is ok for your application.
试着看看你的申请是否可以。
#2
0
As explained on the official docs:
正如官方文件所解释的那样:
Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached.
当到达极限时,消息将从队列的前端删除或挂起,以便为新消息腾出空间。
https://www.rabbitmq.com/maxlength.html
https://www.rabbitmq.com/maxlength.html
If you think RabbitMQ should drop messages form the end of the queue, feel free to open an issue here so we can discuss about it https://github.com/rabbitmq/rabbitmq-server/issues
如果您认为RabbitMQ应该从队列的末尾删除消息,那么您可以在这里打开一个问题,以便我们可以讨论它的https://github.com/rabbitmq/rabbitmq-server/issues。