RabbitMQ:Queue长度限制的介绍和使用

1.声明

当前内容用于本人学习和复习只用,内容包括:限制Queue中消息总数、限制Queue中消息的byte长度、溢出类型的设定

当前内容来源:RabbitMQ官方

2.官方的Queue Length Limit介绍

The maximum length of a queue can be limited to a set number of messages, or a set number of bytes (the total of all message body lengths, ignoring message properties and any overheads), or both.

For any given queue, the maximum length (of either type) can be defined by clients using the queue’s arguments, or in the server using policies. In the case where both policy and arguments specify a maximum length, the minimum of the two values is applied.

队列的的最大长度能够限制队列中的消息长度、bytes的长度(所有的消息题总和,忽略消息属性和任何开销)

对于任何一个队列,这个最大长度都被客户端通过队列的参数设定,或者在server中使用配置。在策略和参数中都指定最大长度那么取两者的最小值

The default behaviour for RabbitMQ when a maximum queue length or size is set and the maximum is reached is to drop or dead-letter messages from the front of the queue (i.e. the oldest messages in the queue). To modify this behaviour, use the overflow setting described below

当前的消息放入的时候如果超过了队列的最大长度那么默认RabbitMQ的就是直接删除最前面的消息(即老的消息)或者将消息放入死信队列。可以通过溢出属性设定并修改行为

Use the overflow setting to configure queue overflow behaviour. If overflow is set to reject-publish, the most recently published messages will be discarded. In addition, if publisher confirms are enabled, the publisher will be informed of the reject via a basic.nack message. If a message is routed to multiple queues and rejected by at least one of them, the channel will inform the publisher via basic.nack. The message will still be published to all other queues which can enqueue it.

使用溢出属性修改队列的溢出行为。如果溢出属性被设置为reject-publish,那么发送的消息就会被拒绝,如果发布者开启了发布确认,将通过basic.nack来将拒绝消息发给发布者。如果这个消息被路由到多个队列,产生了至少一个拒绝。这个句柄将会使用basic.nack推送给发布者。这个发布的消息仍然被推送给给其他的队列中并入队

分析发现:如果消费者开启发布确认(confirmSelect),并设定了溢出属性为reject-publish,那么如果消息发布者发布消息到队列中,消息队列中的消息是满的那么,server将会接收到一个nack

3.使用那些参数配置

可以通过为x-max-length队列声明参数提供非负整数值来设置最大消息 数

可以通过为x-max-length-bytes队列声明参数提供非负整数值来设置最大长度(以字节 为单位)。

如果同时设置了两个参数,则两者都适用;无论先达到哪个限制,都会强制执行

可以通过为x-overflow队列声明参数提供字符串值来设置溢出行为 。可能的值为drop-head(默认)或 reject-publish

4.测试当前的队列长度限制和溢出属性

public class QueueLengthLimitTest {
	public static void main(String[] args) throws Exception {
		RabbitMqUtils mqUtils = new RabbitMqUtils();
		Connection connection = mqUtils.getConnection();
		Channel channel = connection.createChannel();
		
		
		channel.addConfirmListener((deliveryTag, multiple) -> {
			System.out.println("认证接收消息ACK===>deliveryTag=" + deliveryTag + ",multiple=" + multiple);
		}, (deliveryTag, multiple) -> {
			System.out.println("发现拒绝消息NACK====>deliveryTag=" + deliveryTag + ",multiple=" + multiple);
			
		});

		// 开启发布者消息确认
		SelectOk confirmSelect = channel.confirmSelect();
		System.out.println(confirmSelect);
		Map<String, Object> arguments = new HashMap<String, Object>(8);
		arguments.put("x-max-length", 10);
		// 默认为drop-head就是还是入队,只是删除头部前面的数据,并将入队的数据放在后面
		// 如果设定x-overflow为reject-publish,那么表示如果这个队列达到最大限制,就不能入队(其他的都舍弃)
		arguments.put("x-overflow", "reject-publish");
		DeclareOk declareOk = channel.queueDeclare("maxlength-10-queue", true, false, false, arguments);
		System.out.println(declareOk);
		BindOk bindOk = channel.queueBind("maxlength-10-queue", "test", "limit");
		System.out.println(bindOk);
		for (int i = 8; i < 15; i++) {
			// mandatory=true表示目的地不可达的时候才会返回消息
			channel.basicPublish("test", "limit", false, null, ("发送限制消息===>" + (i) + "").getBytes());
			channel.waitForConfirms();//这个就是为了保证上面的拒绝监听可以一个一个的接收到
		}

	}

}

第一次发送消息
在这里插入图片描述

第二次发送消息
在这里插入图片描述

第三次发送消息
在这里插入图片描述

从上面发现multiple为true的时候,表示当前消息被拒绝

查看ui结果
在这里插入图片描述

创建消息消费者

public class QueueLengthLimitConsumerTest {
	public static void main(String[] args) throws Exception {
		RabbitMqUtils mqUtils = new RabbitMqUtils();
		Connection connection = mqUtils.getConnection();
		Channel channel = connection.createChannel();

		channel.basicConsume("maxlength-10-queue", true, new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				// TODO Auto-generated method stub
				System.out.println("消费者消费===>" + new String(body, "utf-8"));
			}

		});
		// 发现默认如果发送的消息超出了限制,默认从消息队列的头部开始删除指定条数的数据,然后将添加的数据放在后面

	}
}

启动消费者查看结果
在这里插入图片描述

通过测试发现:将x-overflow设定为reject-publish那么发送超过队列限定的数量将会返回拒绝,x-overflow设定为drop-head(就是从头部开始删除,然后将消息放在后面)

5.总结

1.通过设定x-max-length来限定当前的queue的消息的最大长度

2.通过x-overflow来设置queue中的消息达到最大值时的消息处理方式,是丢弃还是替换

3.要查看拒绝的消息需要消息生产者启用确认:confirmSelect,可以通过waitForConfirms等待确认

以上纯属个人见解,如有问题请联本人!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值