简体   繁体   English

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

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

Given the queue name, i need all the messages in that queue in RabbitMQ.给定队列名称,我需要 RabbitMQ 中该队列中的所有消息。

I have got the number of messages in the queue in msgCount variable and the first message in strBody variable.我在 msgCount 变量中获得了队列中的消息数,在 strBody 变量中获得了第一条消息。 But i need all the messages in the given queue.但我需要给定队列中的所有消息。 Something that the RabbitMQ Management UI gives me in the browser when i click the Get Message(s) button当我单击“获取消息”按钮时,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);
                }
            }
        }

Making a simple http request to the rabbitMQ server does the job.向 rabbitMQ 服务器发出一个简单的 http 请求即可完成这项工作。 Make a POST request to the following url:向以下网址发出 POST 请求:

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

where Url is where your rabbitMQ is hosted Url 是您的 rabbitMQ 所在的位置

Send the following payload发送以下有效载荷

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

Put -1 in count to get all the messages.将 -1 计入计数以获取所有消息。 Truncate is optional截断是可选的

RabbitMQ Management HTTP API : https://rawcdn.githack.com/rabbitmq/rabbitmq-management/v3.8.0/priv/www/api/index.html 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