写在前面
RabbitMQ 3.5.0 及以上版本优先级队列插件已经集成到broker中,不需要自行下载安装。
开始
VS2013新建一个C#控制台项目,添加引用,添加引用RabbitMQ.Client.dll
新建一个类MyRabbitMq,具体代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using RabbitMQ.Client;
using System.Collections;
namespace RabbitmqClient
{
/// <summary>
/// P2P模式,即一个生产者一个消费者
/// </summary>
///
public enum MessegePriority : byte
{
Low = 0,
Normal = 5,
High = 9
}
public class MyRabbitMq
{
private readonly ConnectionFactory rabbitMqFactory;
const string ExchangeName = "test.exchange";
const string QueueName = "my.queue";
/// <summary>
/// Declare priority queues using the x-max-priority argument
/// </summary>
internal static IDictionary<string, object> QueueArguments
{
get
{
IDictionary<string, object> arguments = new Dictionary<string, object>();
arguments["x-max-priority"] = 10;//定义队列优先级为10个级别