![](/img/trans.png)
[英]In .net 6.0, RabbitMQ.Client v6.4.0 queue returns nothing
[英]Get messages from queue using RabbitMQ.Client
我正在使用.NET的RabbitMQ.Client( https://www.rabbitmq.com/dotnet-api-guide.html )。
我遇到的一個問題是如何從隊列中獲取所有消息內容。
這里的問題是我不想使用這些消息,我只想列出它們。
文檔中列出的所有方法(BasicConsume,BasicGet)都會消耗該消息,然后將其從隊列中清除,這不是我想要的。
你們有什么主意嗎?
您不能將消息作為數據庫瀏覽。
唯一的方法是使用autoack = false
消耗消息,然后重新排隊消息。
RabbitMQ不是數據庫,通常,您不應該瀏覽消息。 瀏覽該消息不是最好的做法,當您重新排隊的消息標志redelivered
被設置為true
,也許你需要為你的應用。
**Here is the solution for you.**
1. **Connect with rabbitMQ using API**
public WebClient GetRabbitMqConnection(string userName, string password)
{
var client = new WebClient();
client.Credentials = new NetworkCredential(userName, password);
return client;
}
2. **Retrieve message by providing appropriate parameter in below method.**
public string GetRabbitMQMessages(string domainName, string port, string
queueName, string virtualHost, WebClient
client, string methodType)
{
string messageResult = string.Empty;
string strUri = "http://" + domainName + ":" + port + "/api/queues/"
+ virtualHost + "/";
var data = client.DownloadString(strUri + queueName + "/");
var queueInfo = JsonConvert.DeserializeObject<QueueInfo>(data);
if (queueInfo == null || queueInfo.messages == 0)
return string.Empty;
if (methodType == "POST")
{
string postbody = "
{\"ackmode\":\"ack_requeue_true\",\"count\":
\"$totalMessageCount\",
\"name\":\"${DomainName}\",\"requeue\":\"false\",
\"encoding\":\"auto\",\"vhost\" : \"${QueueName}\"}";
postbody = postbody.Replace("$totalMessageCount",
queueInfo.messages.ToString()).Replace("${DomainName}",
domainName).Replace("${QueueName}", queueName);
messageResult = client.UploadString(strUri + queueName + "/get",
"POST", postbody);
}
return messageResult;
}
**I wish it will solve your problem. All the best and cheers !!!!!!**
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.