c# – RabbitMQ 3.5和消息优先级

前端之家收集整理的这篇文章主要介绍了c# – RabbitMQ 3.5和消息优先级前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
RabbitMQ 3.5现在 supports message priority;
但是,我无法建立一个有效的例子.我把我的代码放在下面.它包括我期望的输出和实际的输出.我会对更多文档和/或工作示例感兴趣.

所以我的问题很简单:如何在Rabbit 3.5.0.0中使用消息优先级?

出版商:

using System;
using RabbitMQ.Client;
using System.Text;
using System.Collections.Generic;

class Publisher
{

    public static void Main()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        {
            using (var channel = connection.CreateModel())
            {
                IDictionary <String,Object> args = new Dictionary<String,Object>() ;
                args.Add(" x-max-priority ",10);
                channel.QueueDeclare("task_queue1",true,false,args);

                for (int i = 1 ; i<=10; i++ )
                {
                    var message = "Message";
                    var body = Encoding.UTF8.GetBytes(message + " " + i);
                    var properties = channel.CreateBasicProperties();
                    properties.SetPersistent(true);
                    properties.Priority = Convert.ToByte(i);
                    channel.Basicpublish("","task_queue1",properties,body);
                }
            }
        }
    }
}

消费者:

using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Threading;
using System.Collections.Generic;

namespace Consumer
{ 
    class Worker
    {
        public static void Main()
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    IDictionary<String,Object>();                      
                    channel.BasicQos(0,1,false);
                    var consumer = new QueueingBasicConsumer(channel);
                    IDictionary<string,object> consumerArgs = new Dictionary<string,object>();
                    channel.BasicConsume( "task_queue1","",args,consumer);
                    Console.WriteLine(" [*] Waiting for messages. " +
                                      "To exit press CTRL+C");
                    while (true)
                    {
                        var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine(" [x] Received {0}",message);
                        channel.BasicAck(ea.DeliveryTag,false);
                    }
                }
            }
        }
    }
}

实际产量:

[*] Waiting for messages. To exit press CTRL+C
[x] Received Message 1
[x] Received Message 2
[x] Received Message 3
[x] Received Message 4
[x] Received Message 5
[x] Received Message 6
[x] Received Message 7
[x] Received Message 8
[x] Received Message 9
[x] Received Message 10

预期产量:

[*] Waiting for messages. To exit press CTRL+C
[x] Received Message 10
[x] Received Message 9
[x] Received Message 8
[x] Received Message 7
[x] Received Message 6
[x] Received Message 5
[x] Received Message 4
[x] Received Message 3
[x] Received Message 2
[x] Received Message 1

更新#1.
我在Java here中找到了一个例子.但是它是Rabbit 3.4.x.x.添加到3.5中的插入.
我能看到的唯一区别是它们将优先级表示为int而我的是一个字节.但我觉得那是一个红鲱鱼.我在这里有点不知所措.

解决方法

好吧,我解决了.
这是一个愚蠢的错误.
我写:
args.Add(" x-max-priority ",10);

它应该是

args.Add("x-max-priority",10);

我会把它留下来,以便其他人可以在C#中有一个Rabbitmq 3.5的优先级队列的工作示例.

猜你在找的C#相关文章