一、引言
在具体业务中可能会遇到一些要提前处理的消息,比如普通客户的消息按先进先出的顺序处理,Vip客户的消息要提前处理。在RabbitMQ中,消息优先级的实现方式是:在声明queue时设置队列的x-max-priority属性,然后在publish消息时,设置消息的优先级即可。
RabbitMQ优先级队列注意事项:
1)RabbitMQ3.5以后才支持优先级队列。
2)只有当消费者不足,不能及时进行消费的情况下,优先级队列才会生效。
3)优先级取值范围在0~9之间,数值越大则优先级越高。
二、示例
2.1、发送端(生产端)
新建一个控制台项目Send,并添加一个类RabbitMQConfig。
class RabbitMQConfig { public static string Host { get; set; } public static string VirtualHost { get; set; } public static string UserName { get; set; } public static string Password { get; set; } public static int Port { get; set; } static RabbitMQConfig() { Host = "192.168.2.242"; VirtualHost = "/"; UserName = "hello"; Password = "world"; Port = 5672; } }