Retrieving Messages - Understanding Queue Storage - Part Three

In this article, we will see how to retrieve a message from Queue Storage

To retrieve and process messages, you can use the GetMessage method. Using the method will pull out the message off the queue. We need to process the message within 30 seconds, and after successful processing you have to dequeue that message by calling DeleteMessage method which will permanently delete the message by considering that it is complete. Running the GetMessage method will make the message invisible to all other event listeners.

Azure Queue Storage also allows you to peek at the message in the front of a queue without removing it from the queue to have a look at the message by calling the PeekMessage method.

Step 1

First we are going to see how to peek at a message. Open your project and replace your code to insert a message with the following code.

Azure

 


Step 2

Run your application and you can see the first message is shown in your console window. If you check the portal also at the same time you can see the message is unaffected.

Azure  

 

Azure

 

Step 3

Now let us see how we can dequeue the next message as we described above. First, we have to use GetMessage method which will make the message invisible to all other event listeners wand on the successful competition you have to delete your message.

Replace the code to peek at the message with the following code. 

  1. CloudQueueMessage retrievedMessage = queue.GetMessage();  
  2. Console.WriteLine(retrievedMessage.AsString);  
  3. queue.DeleteMessage(retrievedMessage);  

Azure  

Step 4

Run your application and you can see the first message is shown in your console window. If you check the portal also at the same time you can see the message is dequeued.
 
Azure
 
Azure