Windows Azure Storage : Message Operations (part 2) - Get Messages

11/29/2010 2:40:29 PM

2. Get Messages

In the previous section, you learned to send messages to queues in the Queue service. In this section, you learn to retrieve these messages using the Get Messages operation. The URI for the Get Messages operation is of the format account name><queue name>/messages. The URI for the Get Messages operation supports additional optional parameters, as listed in Table 3.

Table 3. Get Messagse URI Parameters
numofmessagesAn integer value specifying the total number of messages you want retrieved. You can retrieve a maximum of 32 messages in a single call. By default, the operation retrieves only one message at a time.account name><queue name>/messages?numofmessages=10
visibilitytimeoutAn integer value representing the visibility of the message in seconds after it's received by a receiving application. The default visibilitytimeout value is 30 seconds, which means that after a message is received, it will remain invisible to other applications for 30 seconds, unless it's deleted by the receiving application. The maximum visibilitytimeout value is 2 hours.account name><queue name>/messages?visibilitytimeout=60

Listing 4 shows the REST API request for the Get Messages operation.

Example 4. Get Messages REST Request
GET /myfirstazurequeue/messages?numofmessages=10&visibilitytimeout=60&timeout=30_
x-ms-date: Thu, 18 Jun 2009 05:34:13 GMT
Authorization: SharedKey proazurestorage:qB9P717GTC6nd6rX4Ed16r6QkxO2QwJxLcr

In Listing 4, the URI points to the myfirstazurequeue queue. numofmessages=10 instructs the Queue service to retrieve only 10 messages. visibilitytimeout=60 instructs the Queue service to make the retrieved messages invisible to other applications for 60 seconds, unless the receiving application deletes them. Listing 5 shows the REST API response from the Queue service for the Get Messages operation.

Example 5. Get Messages REST Response
HTTP/1.1 200 OK
Content-Type: application/xml
Server: Queue Service Version 1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: c10542ae-fa9e-45fd-b036-3f0b77ed611e
Date: Thu, 18 Jun 2009 05:35:43 GMT
Content-Length: 3900

<?xml version="1.0" encoding="utf-8"?>
<InsertionTime>Thu, 18 Jun 2009 05:36:43 GMT</InsertionTime>
<ExpirationTime>Thu, 18 Jun 2009 05:37:28 GMT</ExpirationTime>
<TimeNextVisible>Thu, 18 Jun 2009 05:36:43 GMT</TimeNextVisible>
<InsertionTime>Thu, 18 Jun 2009 05:36:43 GMT</InsertionTime>
<ExpirationTime>Thu, 18 Jun 2009 05:37:28 GMT</ExpirationTime>
<TimeNextVisible>Thu, 18 Jun 2009 05:36:43 GMT</TimeNextVisible>
<InsertionTime>Thu, 18 Jun 2009 05:36:43 GMT</InsertionTime>
<ExpirationTime>Thu, 18 Jun 2009 05:37:28 GMT</ExpirationTime>
<TimeNextVisible>Thu, 18 Jun 2009 05:36:43 GMT</TimeNextVisible>

Listing 5 shows the HTTP header and body of the Get Messages operation response. For the sake of brevity, only three messages are shown. The HTTP response body consists of a list of messages in XML format. Every <QueueMessage /> element represents a message. When you retrieve a message, the MessageId and the PopReceipt properties of the message are important for deletion purposes. The recommended pattern is to receive the message, process it, and then delete it before it becomes visible to other applications when the visibilitytimeout period expires. The TimeNextVisible value specifies the expiration time of the visibilitytimeout period. The ExpirationTime specifies the time when the message will be marked for deletion if not retrieved and/or deleted by a receiving application. This value was set when the message was sent to the queue. Figure 3 shows the working of the Get Messages operation in the Windows Azure Storage Operations.exe application.

Figure 3. Get Messages in the Windows Azure Storage Operations application

As illustrated in Figure 3, you can use the Get Messages operation for a queue using the Windows Azure Storage Operations application. The steps for retrieving messages are as follows:

  1. Select a queue (such as myfirstazurequeue) that already contains some messages.

  2. In the Queues section, select the Get Messages operation.

  3. Click the Execute button to get a list of messages from the selected queue.

  4. Optionally, you can specify the Number of Messages and Visibility Timeout in the Parameters section.

The retrieved messages are populated in the DataGridView control in the Messages section. Each message is represented by a row in the DataGridView control. The control displays all the properties of the retrieved messages. To delete a message, select a row in the DataGridView and press the Delete button on your keyboard.

The WindowsAzureStorage.cs file in the Windows Azure Storage Operations project consists of a GetMessages() method, as shown in Listing 6.

Example 6. GetMessages() Method in WindowsAzureStorage.cs
private void GetMessages()
msgs = StorageHelper.GetMessages(txtQueueName.Text, int.Parse(txtNumberOf
Messages.Text), int.Parse(txtVisibilityTimeoutSecs.Text));

if (msgs != null)
IList<Microsoft.Samples.ServiceHosting.StorageClient.Message> messages =
new List<Microsoft.Samples.ServiceHosting.StorageClient.Message>(msgs);


As shown in Listing 6, the GetMessages() method calls the GetMessages() method of WindowsAzureStorageHelper (StorageHelper object). The numofmessages and visibilitytimeout parameters are parsed appropriately into integer values and passed as parameters to the GetMessages() method. The returned messages are then packaged into a list and passed to the PopulateMessagedataGridView() method for display. Figure 4 illustrates the sequence diagram for the Get Messages operation.

Figure 4. Get Messages sequence diagram

As shown in Figure 4, the Windows Azure Storage Operations application calls the GetMessages() method on the WindowsStorageHelper object in the ProAzureCommonLib.dll. The WindowsStorageHelper object calls the GetQueue() method on the QueueStorage object to get an instance of the MessageQueue object. The WindowsStorageHelper object then calls the GetMessages() method on the MessageQueue object. The MessageQueue object calls a private method InternalGet() to retrieve the messages from the specified queue. The body of the HTTP response contains messages in XML format. The XML contents are parsed and packaged into an IEnumerable<Message> collection and passed as a return parameter to the GetMessages() operation. Finally, the Windows Azure Storage Operations application displays these messages in a DataGridView control, as shown earlier in this section.

2.1. Message Event

The StorageClient API also supports an event-driven model for retrieving messages from a queue. The MessageQueue class defines an event called MessageReceived that you can subscribe to. The MessageQueue class also defines two methods StartReceiving() and StopReceiving() that start and stop the event subscription, respectively. The property PollInterval specifies the polling interval in milliseconds. Based on the PollInterval value, a thread calls the Get Messages operation periodically and returns the messages as parameters of the event delegate.

The steps for implementing an event-driven approach to the Get Messages operation are as follows.

  1. Create an instance of the MessageQueue object:

    Microsoft.Samples.ServiceHosting.StorageClient.MessageQueue mq =

  2. Create an event handler for the MessageReceived event:

    mq.MessageReceived += new MessageReceivedEventHandler(mq_MessageReceived);

    1. The event handler method signature for mq_MessageReceived has MessageReceivedEventArgs as one of the parameters, which has a Message property representing the retrieved message.

  3. Set the PollInterval property of the MessageQueue object to the desired polling interval in milliseconds:

    mq.PollInterval = 10000;

  4. Start receiving messages by calling the StartReceiving() method:


  5. To stop receiving messages, call the StopReceiving() method:



The even-driven model is a purely client-side implementation for ease of client programming. In the background, the event is fired periodically and calls the same Get Messages operation discussed in this section. The REST API for the Queue service doesn't offer asynchronous invocations.

Other -----------------
- Windows Azure Storage : Queue Operations
- Windows Azure Storage : Account Operations
- Windows 7 : Removing an Icon from Control Panel
- Windows 7 : Showing Only Specified Control Panel Icons
- Windows 7 : Easier Access to Control Panel
- Windows 7 : Understanding Control Panel Files
- Windows 7 : Reviewing the Control Panel Icons
- Windows 7 : Touring the Control Panel Window
- Windows 7 : Reviewing Event Viewer Logs
- Windows 7 : Checking for Updates and Security Patchess
- Windows 7 : Backing Up Your Files
- Windows 7 : Preparing for Trouble
- Windows 7 : Defragmenting Your Hard Disk
- Windows 7 : Deleting Unnecessary Files
- Windows 7 : Checking Free Disk Space
- Windows 7 : Checking Your Hard Disk for Errors
- Windows Azure : Understanding Message Operations
- Windows Azure : Understanding Queue Operations
- Windows Azure Queue Overview
- Tuning Windows 7’s Performance : Optimizing Virtual Memory
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- Sigma 24mm f/1.4 DG HSM Art

- Canon EF11-24mm f/4L USM

- Creative Sound Blaster Roar 2

- Alienware 17 - Dell's Alienware laptops

- Smartwatch : Wellograph

- Xiaomi Redmi 2
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8