如何阅读亚马逊SQS中的所有队列消息? [英] How to read all message of queue in the amazon SQS ?

查看:247
本文介绍了如何阅读亚马逊SQS中的所有队列消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Amazon SQS中有队列名称  testqueue 。 Testqueue包含100-500条消息。我使用从 testqueue 获得 10 消息下面给出的代码,但我想获取所有 可用的消息 testqueue中。 
public class 计划
{
public static void Main( string [] args)
{
AmazonSQSClient objClient = new AmazonSQSClient
A ************ ************ t ***********,Amazon.RegionEndpoint.USWest2);

// 确认队列存在
ListQueuesRequest listQueuesRequest = < span class =code-keyword> new
ListQueuesRequest();
ListQueuesResponse listQueuesResponse = objClient.ListQueues(listQueuesRequest);

Console.WriteLine( 打印Amazon SQS队列列表。\ n);
foreach 字符串 queueUrl in listQueuesResponse.QueueUrls)
{
Console.WriteLine( QueueUrl:{0},queueUrl);

// 接收消息
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.QueueUrl = queueUrl;
receiveMessageRequest.MaxNumberOfMessages = 10 ;

ReceiveMessageResponse receiveMessageResponse = objClient.ReceiveMessage(receiveMessageRequest);

Console.WriteLine( 打印收到的消息。\ n) ;
foreach (消息消息 in receiveMessageResponse.Messages)
{
Console.WriteLine( Message);
Console.WriteLine( MessageId:{0},message.MessageId);
Console.WriteLine( ReceiptHandle:{0},message.ReceiptHandle);
Console.WriteLine( MD5OfBody:{0},message.MD5OfBody);
Console.WriteLine( 正文:{0},message.Body);

foreach (KeyValuePair< string,string> entry in message.Attributes)
{
Console.WriteLine( Attribute);
Console.WriteLine( 名称:{0},entry.Key);
Console.WriteLine( 值:{0},entry.Value);
}
}
字符串 messageRecieptHandle = receiveMessageResponse.Messages [ 0 ] .ReceiptHandle;
}
Console.WriteLine();

Console.WriteLine( 读取队列);

Console.ReadLine();
}
}

谢谢,
The Rock





< b>我尝试了什么:



上面给出的代码只返回10条消息,输出1000条消息。但我想要1000条消息。有人可以帮我摆脱这种情况。



谢谢

快乐编码

解决方案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



从您的代码中, foreach 可能不是正确的方式获取所有消息。或者列表没有正确填写。

[更新]

问题的根源是否可能是这行?

Quote:

 receiveMessageRequest.MaxNumberOfMessages =  10 ; 


I have queue name is testqueue in Amazon SQS. Testqueue contain 100-500 message. I got 10 message from the testqueue using below given code but i want to get all message which is available in testqueue.
 public class Program
    {
        public static void Main(string[] args)
        {
            AmazonSQSClient objClient = new AmazonSQSClient
            ("A************", "************t***********", Amazon.RegionEndpoint.USWest2);

            //Confirming the queue exists
            ListQueuesRequest listQueuesRequest = new ListQueuesRequest();
            ListQueuesResponse listQueuesResponse = objClient.ListQueues(listQueuesRequest);

            Console.WriteLine("Printing list of Amazon SQS queues.\n");
            foreach (String queueUrl in listQueuesResponse.QueueUrls)
            {
                Console.WriteLine("  QueueUrl: {0}", queueUrl);

                //Receiving a message
                ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
                receiveMessageRequest.QueueUrl = queueUrl;
                receiveMessageRequest.MaxNumberOfMessages = 10;

                ReceiveMessageResponse receiveMessageResponse = objClient.ReceiveMessage(receiveMessageRequest);
             
                Console.WriteLine("Printing received message.\n");
                foreach (Message message in receiveMessageResponse.Messages)
                {
                    Console.WriteLine("Message");
                    Console.WriteLine("MessageId: {0}", message.MessageId);
                    Console.WriteLine("ReceiptHandle: {0}", message.ReceiptHandle);
                    Console.WriteLine("MD5OfBody: {0}", message.MD5OfBody);
                    Console.WriteLine("Body: {0}", message.Body);

                    foreach (KeyValuePair<string, string> entry in message.Attributes)
                    {
                        Console.WriteLine("Attribute");
                        Console.WriteLine("Name: {0}", entry.Key);
                        Console.WriteLine("Value: {0}", entry.Value);
                    }
                }
                String messageRecieptHandle = receiveMessageResponse.Messages[0].ReceiptHandle;
            }
            Console.WriteLine();

            Console.WriteLine("Read the queue");
            
            Console.ReadLine();
        }
    }

Thanks,
The Rock



What I have tried:

Above given code return only 10 message out 0f 1000 message. but i want 1000 message. Can some one help me to get rid of this situation.

Thanks
Happy coding

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

From your code, a foreach may not be the right way to get all messages. Or the list is not filled correctly.
[Update]
Is it possible that the root of your problem is this line ?

Quote:

receiveMessageRequest.MaxNumberOfMessages = 10;


这篇关于如何阅读亚马逊SQS中的所有队列消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆