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();
            }
        }
    }
}
相关推荐
金刚猿4 分钟前
简单理解下基于 Redisson 库的分布式锁机制
分布式·分布式锁·redisson
我一直在流浪27 分钟前
Kafka - 消费者程序仅消费一半分区消息的问题
分布式·kafka
张彦峰ZYF2 小时前
投资策略规划最优决策分析
分布式·算法·金融
processflow流程图4 小时前
分布式kettle调度平台v6.4.0新功能介绍
分布式
全栈开发圈4 小时前
干货分享|分布式数据科学工具 Xorbits 的使用
分布式
运维&陈同学6 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列
时差9536 小时前
Flink Standalone集群模式安装部署
大数据·分布式·flink·部署
菠萝咕噜肉i7 小时前
超详细:Redis分布式锁
数据库·redis·分布式·缓存·分布式锁
只因在人海中多看了你一眼10 小时前
分布式缓存 + 数据存储 + 消息队列知识体系
分布式·缓存
zhixingheyi_tian13 小时前
Spark 之 Aggregate
大数据·分布式·spark