Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# AWS SQS Read all messages without deleting

I want to read all messages from queue in AWS SQS without deleting them , using C# , but I am only getting the single last message.

Here is the code

 ReceiveMessageRequest rmr = new ReceiveMessageRequest();
        rmr.QueueUrl = url;
        rmr.MaxNumberOfMessages = 10;
        AmazonSQSClient sq = new AmazonSQSClient("xx", "yy");
        ReceiveMessageResponse rmrep = sq.ReceiveMessage(rmr);
        ReceiveMessageResult rmres = rmrep.ReceiveMessageResult;
        //DeleteMessageRequest dmr = new DeleteMessageRequest();

        string msgid = "";
        string rephandle = "";
        XmlDocument xl = new XmlDocument();

        for (int i = 0; i < rmres.Message.Count;i++ )
        {
            rmrep = sq.ReceiveMessage(rmr);
            rmres = rmrep.ReceiveMessageResult;
            object y = JsonConvert.DeserializeObject<object>(rmres.Message[0].Body);
           int z= y.add();
           MessageBox.Show(z.ToString());
        }
like image 366
knowledgeseeker Avatar asked Jan 12 '23 02:01

knowledgeseeker


1 Answers

A queue is not a database table; you can't just 'read all the messages'. You tell SQS you want some messages, it picks some and sends them to you; you can't ask it, or expect it, to iterate thru a unique list of messages in the queue and present them to you, it is not intended for that purpose.

If you read 1 or messages, and delete them (or put them in another queue), then eventually you can 'read all the messages', but as long as you don't delete any, its likely that you will keep getting served back some messages multiple times.

like image 117
E.J. Brennan Avatar answered Jan 17 '23 23:01

E.J. Brennan