RabbitMQ:消费者优先级的介绍和使用

1.声明

当前内容用于本人学习和复习之用,当前内容主要包括优先级消费者和普通消费者区别

当前内容来源:RabbitMQ官方文档

2.官方Consumer Priorities的介绍

Consumer priorities allow you to ensure that high priority consumers receive messages while they are active, with messages only going to lower priority consumers when the high priority consumers block.

Normally, active consumers connected to a queue receive messages from it in a round-robin fashion. When consumer priorities are in use, messages are delivered round-robin if multiple active consumers exist with the same high priority.

高优先级消费者可以一直保持活跃状态,只有当前高优先级的消费者出现了阻塞,才会将消息发送给低优先级的消费者

通常连接到队列的活动的消费者是轮询接收消息的。当消费者的优先级开都启了,那么=这个消息将会轮询的发给优先级相同的消费者

分析发现:如果一个消费者的优先级高的时候,将会抢夺低优先级消费者的消息,并且只有高消费者阻塞那么才会把消息发送给低优先级的消费者。如果出现多个相同高优先级的消费者,那么消息只会在高优先级消费者之间轮询

3.官方Using Consumer Priorities的介绍

Set the x-priority argument in the basic.consume method to an integer value. Consumers which do not specify a value have priority 0. Larger numbers indicate higher priority, and both positive and negative numbers can be used.

==将调用的basic.consume方法并设置x-priority参数为整数;消费者不设置优先级,那么这个值就是0。优先级的值越大,那么优先级越高=,可以使用正数和负数=

Channel channel = ...;
Consumer consumer = ...;
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-priority", 10);
channel.basicConsume("my-queue", false, args, consumer);

4.测试消费者优先级

1.创建高优先级的消费者(优先级为5)

/**
 * @description 消费者优先级测试
 * @author hy
 * @date 2020-05-18
 */
public class ConsumerPriority5Test {
	public static void main(String[] args) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		Map<String, Object> arguments = new HashMap<String, Object>();
		arguments.put("x-priority", 5);//
		channel.basicConsume("hello", true, arguments, new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println("优先级为5的消费者开始处理消息===>" + new String(body, "utf-8"));
			}

		});
	}
}

2.创建优先级为0的消费者

/**
 * @description 消费者优先级测试
 * @author hy
 * @date 2020-05-18
 */
public class ConsumerPriority0Test2 {
	public static void main(String[] args) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		Map<String, Object> arguments = new HashMap<String, Object>();
		arguments.put("x-priority", 0);
		channel.basicConsume("hello", true,arguments ,new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println("优先级为0的消费者开始处理消息===>" + new String(body, "utf-8"));
			}

		});
	}
}

3.创建消息生产者

public class MsgSender {
	public static void main(String[] args) {
		RabbitMqUtils mqUtils=new RabbitMqUtils();
		for (int i = 0; i < 10; i++) {
			mqUtils.send("test", "", true, null, "你好【"+(i+1)+"】===>");
		}
		
	}
}

4.测试(先启动两个消费者,再启动生产者)
在这里插入图片描述
在这里插入图片描述

发现所有的消息都被高优先级的消费者消费,低优先级的没有消费任何消息

5.修改优先级为5的消费者,开启消费未确认限制、开启休眠

public class ConsumerPriority5Test {
	public static void main(String[] args) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		Map<String, Object> arguments = new HashMap<String, Object>();
		arguments.put("x-priority", 5);//
		channel.basicQos(5);// 最多不确认5条数据
		channel.basicConsume("hello", false, arguments, new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println("优先级为5的消费者开始处理消息===>" + new String(body, "utf-8"));
				try {
					Thread.sleep(2000l);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				channel.basicAck(envelope.getDeliveryTag(), false);
			}

		});
	}
}

6.再次测试(先启动两个消费者,再启动生产者)
在这里插入图片描述
在这里插入图片描述
此时发现高优先级的消费者已近限定消费,并且当前的低优先级可以消费了(也就是当高优先级的消费出现阻塞,低优先级的消费者才能拿到消息)

6.总结

1.消费者的优先级设定是通过建立消费的basicConsume的时候设定的arguements的x-priority

2.当高优先级的消费者未出现阻塞或者限制的时候,那么低优先级的消费者是不能拿到消息的

3.当高优先级的消费者出现阻塞等其他限制的情况的时候,消息才会发向给低优先级的消费者

4.对于是否设定消费者优先级需要按照需求进行设定,如果盲目添加可能导致其他消费者拿不到消息!

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值