RabbitMQ一次使多条消息出队

时间:2019-07-16 08:54:52

标签: c# rabbitmq

我在.net核心(NuGet的最新版本)中使用RabbitMQ。我有一个队列,优先。

我的代码正在插入10条消息,并使10条消息出队。为了使消息出队,我使用了EventingBasicConsumer来通过Push获取消息。

这是我的代码:

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

namespace Test
{
    class Program
    {
        public static IConnection Connection = GetConnection("localhost", "xxx", "xxx");
        public static IModel Channel = Connection.CreateModel();
        public static void Main(string[] args)
        {
            IDictionary<String, Object> args2 = new Dictionary<String, Object>();
            args2.Add("x-max-priority", 256);
            Channel.QueueDeclare("IDG", true, false, false, args2);

            for (int i = 0; i < 10; i++)
            {
                Send("IDG", (i % 10).ToString(), (byte)(i % 10));
                Console.WriteLine("Queued: "+ (i % 10).ToString());
            }

            Receive("IDG", Channel);
            Console.ReadLine();
        }

        public static IConnection GetConnection(string hostName, string userName, string password)
        {
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.HostName = hostName;
            connectionFactory.UserName = userName;
            connectionFactory.Password = password;
            return connectionFactory.CreateConnection();
        }

        public static void Send(string queue, string data, byte priority)
        {
            var properties = Channel.CreateBasicProperties();
            properties.Priority = priority;
            Channel.BasicPublish(string.Empty, queue, properties, Encoding.UTF8.GetBytes(data));
        }

        public static void Receive(string queue, IModel channel)
        {
            IDictionary<String, Object> args2 = new Dictionary<String, Object>();
            args2.Add("x-max-priority", 256);
            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += Consumer1_Received;
            channel.BasicConsume(consumer, queue, autoAck: false, arguments: args2);
        }

        private static void Consumer1_Received(object sender, BasicDeliverEventArgs e)
        {
            var message = Encoding.UTF8.GetString(e.Body);
            Console.WriteLine($"{DateTime.Now}: [x] Received {message}");

            // Simulate Processing...
            Thread.Sleep(3000);

            Channel.BasicAck(e.DeliveryTag, false);
        }
    }
}

输出:

Queued: 0
Queued: 1
Queued: 2
Queued: 3
Queued: 4
Queued: 5
Queued: 6
Queued: 7
Queued: 8
Queued: 9
16-Jul-19 11:37:59 AM: [x] Received 9
16-Jul-19 11:38:02 AM: [x] Received 8
16-Jul-19 11:38:05 AM: [x] Received 7
16-Jul-19 11:38:08 AM: [x] Received 6
16-Jul-19 11:38:11 AM: [x] Received 5
16-Jul-19 11:38:14 AM: [x] Received 4
16-Jul-19 11:38:23 AM: [x] Received 3
16-Jul-19 11:38:23 AM: [x] Received 2
16-Jul-19 11:38:23 AM: [x] Received 1
16-Jul-19 11:38:23 AM: [x] Received 0

每个消息的处理需要3秒钟(请参见Thread.Sleep(3000)委托中的Consumer1_Received)。

问题

我想同时处理多条消息。假设我的机器可以同时处理5条消息。因此,处理全部10条消息大约需要6秒钟。

  1. 如何定义同时运行的消息的一致性 时间?
  2. 如何提取N条消息?我看到了BasicGet()方法,它提取一条消息。有可能拉出更多的信息吗?

更新1

我已经尝试使用多个使用者,但是吞吐量是相同的。这是代码:

public static void Receive(string queue, IModel channel)
{
    IDictionary<String, Object> args2 = new Dictionary<String, Object>();
    args2.Add("x-max-priority", 256);

    channel.BasicQos(0, 1, true);

    var consumer1 = new EventingBasicConsumer(channel);
    consumer1.Received += Consumer_Received;
    channel.BasicConsume(consumer1, queue, autoAck: false, arguments: args2);

    var consumer2 = new EventingBasicConsumer(channel);
    consumer2.Received += Consumer_Received;
    channel.BasicConsume(consumer2, queue, autoAck: false, arguments: args2);
}

private static void Consumer_Received(object sender, BasicDeliverEventArgs e)
{
    var message = Encoding.UTF8.GetString(e.Body);
    Console.WriteLine($"{DateTime.Now}: [x] Received {message}");

    // Simulate Processing...
    Thread.Sleep(3000);

    Channel.BasicAck(e.DeliveryTag, false);
}

输出为:(仍然3秒\消息)

16-Jul-19 12:29:30 PM: [x] Received 6
16-Jul-19 12:29:33 PM: [x] Received 5
16-Jul-19 12:29:36 PM: [x] Received 4
16-Jul-19 12:29:39 PM: [x] Received 3
16-Jul-19 12:29:45 PM: [x] Received 2
16-Jul-19 12:29:51 PM: [x] Received 1
16-Jul-19 12:30:00 PM: [x] Received 0

1 个答案:

答案 0 :(得分:1)

您使用相同的IModel实例进行消费和生产。创建两个单独的模型。 Thread.Sleep(3000)阻止用于接收新消息的专用Rabbitmq线程。