RabbitMQ优先级队列的使用

RabbitMQ优先级队列的使用

生产者

csharp 复制代码
public class PriorityQueue
{
    public static void Send()
    {

        string path = AppDomain.CurrentDomain.BaseDirectory;
        string tag = path.Split('/', '\\').Last(s => !string.IsNullOrEmpty(s));
        Console.WriteLine($"这里是 {tag} 启动了。。");

        ConnectionFactory factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (IConnection connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                //创建队列的时候,指定队列的优先级;x-max-priority:最大的优先级是10
                channel.QueueDeclare(queue: "PriorityQueue", durable: true, exclusive: false, autoDelete: false, arguments: new Dictionary<string, object>() { 
                        {"x-max-priority",10 }  //指定队列要支持优先级设置;
                   });

                
                channel.ExchangeDeclare(exchange: "PriorityQueueExchange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null); 
                channel.QueueBind(queue: "PriorityQueue", exchange: "PriorityQueueExchange", routingKey: "PriorityKey");
                
                
                string[] questionList = { "vip学员1 来请教", "甲 同学来请教问题", 
                                         "乙 同学来请教问题", "丙 同学来请教问题", 
                                         "丁 同学来请教问题", "vip学员2 来请教" };
                //设置消息优先级
                //VIP学员和公开课学员同时来请教问题解答,当然是优先VIP学员;
                IBasicProperties props = channel.CreateBasicProperties();
                foreach (string questionMsg in questionList)
                {
                    if (questionMsg.StartsWith("vip"))
                    {
                        props.Priority = 9;
                        channel.BasicPublish(exchange: "PriorityQueueExchange",
                                             routingKey: "PriorityKey",
                                             basicProperties: props,
                                             body: Encoding.UTF8.GetBytes(questionMsg));
                    }
                    else
                    {
                        props.Priority = 1;
                        channel.BasicPublish(exchange: "PriorityQueueExchange",
                                             routingKey: "PriorityKey",
                                             basicProperties: props,
                                             body: Encoding.UTF8.GetBytes(questionMsg));
                    }
                    Console.WriteLine($"{questionMsg} 已发送~~");
                }
                Console.Read();
            }
        }
    }
}

消费者

csharp 复制代码
public class PriorityQueue
{
    public static void Consumption()
    {
        var factory = new ConnectionFactory();
        factory.HostName = "localhost";//RabbitMQ服务在本地运行
        factory.UserName = "guest";//用户名
        factory.Password = "guest";//密码 
        using (var connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                //定义消费者                                      
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    string msg = Encoding.UTF8.GetString(ea.Body.ToArray());
                    Console.WriteLine(msg);
                    channel.BasicReject(deliveryTag: ea.DeliveryTag, requeue: false);
                };
                Console.WriteLine("消费者准备就绪....");
                //处理消息
                channel.BasicConsume(queue: "PriorityQueue", autoAck: false, consumer: consumer);
                Console.ReadKey();
            }
        }
    }
}
相关推荐
weixin_453965004 分钟前
[单master节点k8s部署]31.ceph分布式存储(二)
分布式·ceph·kubernetes
坎坎坷坷.14 分钟前
分布式理论:拜占庭将军问题
分布式
Xua30554 小时前
MQ高级:RabbitMQ小细节
rabbitmq
极客先躯6 小时前
高级java每日一道面试题-2024年10月3日-分布式篇-分布式系统中的容错策略都有哪些?
java·分布式·版本控制·共识算法·超时重试·心跳检测·容错策略
niu_sama7 小时前
仿RabbitMQ实现消息队列三种主题的调试及源码
分布式·rabbitmq
鸡c7 小时前
rabbitMq------客户端模块
分布式·rabbitmq·ruby
Dylanioucn8 小时前
【分布式微服务云原生】探索Redis:数据结构的艺术与科学
数据结构·redis·分布式·缓存·中间件
路上^_^8 小时前
00_概览_kafka
分布式·kafka
极客先躯15 小时前
Hadoop krb5.conf 配置详解
大数据·hadoop·分布式·kerberos·krb5.conf·认证系统
CopyLower16 小时前
Kafka 消费者状态及高水位(High Watermark)详解
分布式·kafka