繁体   English   中英

用c#从RabbitMQ的队列中获取所有消息

[英]Get all messages from a queue in RabbitMQ in c#

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

我在 msgCount 变量中获得了队列中的消息数,在 strBody 变量中获得了第一条消息。 但我需要给定队列中的所有消息。 当我单击“获取消息”按钮时,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);
                }
            }
        }

向 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM