RabbitMq的消息优先级
一,场景 有VIP客户处理需求,需要优先处理
rabbitmq 创建消息队列的时候 x-max-priority 属性 可配置
-
优先级属性是 byte类型的,所以它的取值范围:[0,255];
-
消息优先级为0或不设优先级的效果一样;
-
消息优先级大于设定的优先级最大值时,效果同优先级的最大值(我们可以自行实验);

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading;
namespace Receive
{
class Program
{
static void Main(string[] args)
{
//1. 实例化连接工厂
var factory = new ConnectionFactory()
{
HostName = "localhost",
Port = 5672,
VirtualHost = "/",
UserName = "guest",
Password = "guest"
};
//2. 建立连接
using (var connection = factory.CreateConnection())
{
//3. 创建信道
using (var channel = connection.CreateModel())
{
//4. 声明队列(和发送端保持一致)
channel.QueueDeclare(queue: "hello", durable: true, exclusive: false, autoDelete: false, arguments: new Dictionary<string, object>
{
{"x-max-priority",9 }
});
//5. 构造消费者实例
var consumer = new EventingBasicConsumer(channel);
//6. 绑定消息接收后的事件委托
consumer.Received += (model, ea) =>
{
var message = Encoding.UTF8.GetString(ea.Body.ToArray());
Console.WriteLine(" [x] Received {0}", message);
Thread.Sleep(1000);//模拟耗时
Console.WriteLine(" [x] Done");
};
//7. 启动消费者
channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
}
}

RabbitMQ:实现VIP消息优先级处理策略
本文介绍了如何在RabbitMQ中利用x-max-priority属性为VIP客户的消息设置优先级,确保高优先级消息的及时处理。通过实例演示了配置队列和创建消费者的过程。
471

被折叠的 条评论
为什么被折叠?



