【问题标题】:Get all messages from a queue in RabbitMQ in c#在c#中从RabbitMQ中的队列中获取所有消息
【发布时间】:2019-12-20 13:57:48
【问题描述】:

给定队列名称,我需要 RabbitMQ 中该队列中的所有消息。

我在 msgCount 变量中获得了队列中的消息数,在 strBody 变量中获得了第一条消息。 但我需要给定队列中的所有消息。当我单击 Get Message(s) 按钮时,RabbitMQ 管理 UI 在浏览器中提供给我的东西

using (var conn = _connectionFactory.CreateConnection())
        {
            using (var channel = conn.CreateModel())
            {
                var queueName = "myqueuename";

                var response = channel.QueueDeclarePassive(queueName);
                var msgCount = response.MessageCount;
                var consCount = response.ConsumerCount;

                BasicGetResult result = channel.BasicGet(queueName, noAck);


                if (result == null)
                {
                    //No msgs available 
                }
                else
                {
                    IBasicProperties properties = result.BasicProperties;
                    byte[] body = result.Body;

                    string strBody = System.Text.Encoding.UTF8.GetString(body);
                    Console.WriteLine(strBody);
                    //channel.BasicAck(result.DeliveryTag, false);
                }
            }
        }

【问题讨论】:

    标签: c# rabbitmq


    【解决方案1】:

    向 rabbitMQ 服务器发出一个简单的 http 请求就可以完成这项工作。 向以下网址发出 POST 请求:

    string queuesUrl = Url + ":" + Port + "/api/queues/" + VirtualHost + "/" + queueName + "/get";
    

    Url 是托管 rabbitMQ 的位置

    发送以下有效载荷

    {"count":5,"ackmode":"ack_requeue_true","encoding":"auto","truncate":50000}
    

    输入 -1 以获取所有消息。截断是可选的

    RabbitMQ 管理 HTTP API: https://rawcdn.githack.com/rabbitmq/rabbitmq-management/v3.8.0/priv/www/api/index.html

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2013-05-17
      • 2017-09-16
      • 2013-12-13
      • 2016-04-27
      相关资源
      最近更新 更多