Here's a C# code sample that defines a queue with a range of priorities:
using RabbitMQ.Client;
public void Setup()
{
ConnectionFactory factory = new() { host = "", username = "", password = "" };
var connection = factory.CreateConnection();
var model = connection.CreateModel();
var args = new Dictionary<string, object>
{
{ "x-min-priority", 0 },
{ "x-max-priority", 9 }
};
model.QueueDeclare("Queue1", arguments: args);
}
And here's a function for sending messages with priority:
private static void Send(IModel model, string queue, string message, byte priority)
{
var body = Encoding.UTF8.GetBytes(message);
var basicProperties = model.CreateBasicProperties();
basicProperties.Priority = priority;
model.BasicPublish(exchange: "",
routingKey: queue,
basicProperties: basicProperties,
body: body);
}
As mentioned by other people, Rabbit doesn't strictly obey these priorities.
If you send a message outside the range defined for the queue, it's treated as the min or max value.