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

问题描述 投票:0回答:2

给出队列名称,我需要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);
                }
            }
        }
c# rabbitmq
2个回答
0
投票

向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


-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放入计数以获取所有消息。截断是可选的

完整代码:

string queuesUrl = Url + ":" + Port + "/api/queues/" + VirtualHost + "/" + queueName + "/get";
WebClient webClient = new WebClient { Credentials = new NetworkCredential(Username, Password) };
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
var content = "{ 'count': " + messagesCount + ",'ackmode':'ack_requeue_true','encoding':'auto'}";
string response = webClient.UploadString(queuesUrl, content);
© www.soinside.com 2019 - 2024. All rights reserved.