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();
            }
        }
    }
}
相关推荐
一个有温度的技术博主3 小时前
Redis主从同步原理:从全量同步到增量同步的完整解析
redis·分布式·缓存
电磁脑机12 小时前
人脑电磁路由拓扑与外耦合脑机接口基础理论
分布式·神经网络·安全·交互
马剑威(威哥爱编程)13 小时前
HarmonyOS 6.0 分布式任务调度 API 详解:把多设备玩成单设备
分布式·华为·harmonyos
嵌入式老牛13 小时前
SST专题3-1 基于光分路器的MMC分布式控制系统架构
分布式·架构·驱动·光纤·sst
F_D_Z14 小时前
Word Embedding :从分布式假设到神经网络语言模型
分布式·word·embedding
feifeigo12315 小时前
航天器交会的分布式模型预测控制(DMPC)MATLAB实现
开发语言·分布式·matlab
總鑽風15 小时前
数据一致性springcloud+rabbitmq+mysql+redis
mysql·spring cloud·rabbitmq
CET中电技术15 小时前
CET中电技术如何助光伏企业在“四可“时代抢占先机?
分布式
人间打气筒(Ada)16 小时前
「码动四季·开源同行」go语言:如何使用 ELK 进行日志采集以及统一处理?
开发语言·分布式·elk·go·日志收集·分布式日志系统
黑牛儿17 小时前
MySQL 实战进阶:从单表优化到分布式数据库适配
数据库·分布式·mysql