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();
            }
        }
    }
}
相关推荐
Data跳动32 分钟前
Spark内存都消耗在哪里了?
大数据·分布式·spark
Java程序之猿2 小时前
微服务分布式(一、项目初始化)
分布式·微服务·架构
来一杯龙舌兰3 小时前
【RabbitMQ】RabbitMQ保证消息不丢失的N种策略的思想总结
分布式·rabbitmq·ruby·持久化·ack·消息确认
节点。csn4 小时前
Hadoop yarn安装
大数据·hadoop·分布式
NiNg_1_2346 小时前
基于Hadoop的数据清洗
大数据·hadoop·分布式
隔着天花板看星星7 小时前
Spark-Streaming集成Kafka
大数据·分布式·中间件·spark·kafka
技术路上的苦行僧11 小时前
分布式专题(8)之MongoDB存储原理&多文档事务详解
数据库·分布式·mongodb
龙哥·三年风水11 小时前
workman服务端开发模式-应用开发-后端api推送修改二
分布式·gateway·php
小小工匠12 小时前
分布式协同 - 分布式事务_2PC & 3PC解决方案
分布式·分布式事务·2pc·3pc
Allen Bright12 小时前
Spring Boot 整合 RabbitMQ:从入门到实践
spring boot·rabbitmq·java-rabbitmq