16.11. Queues

Queues are an important concept in Azure storage, and they consist of an unlimited number of messages that are generally read in the order they are added (the Azure documentation says this is not guaranteed). Messages are removed as they are read, so if you don't want this to occur, make sure you use the peek method instead.

Messages can be up to 8KB in size each, so if you need more space than this, you can use a blob field and link the two by using the blob's metadata. Messages added to queues have a default timeout of seven days (called time to live). After that passes, then they will be destroyed.

You will create a new application to add and read items from a queue:

  1. Create a new Azure project called Chapter16.QueueTest with a web role called Chapter16.QueueTestWebRole. Open Default.aspx, and add the following code inside the <form> tag:

    <asp:TextBox ID="txtMessage" runat="server" Width="300"></asp:TextBox>
    <asp:Button ID="cmdAddToQueue" Text="Add" runat="server" />
    
    <asp:Button ID="cmdShowQueue" Text="Show Queue" runat="server"  />
    <br /><br />
    
    Queue contents:
    <br />
    <asp:Literal ID="litQueueContents" runat="server"></asp:Literal>

  2. Open Default.aspx.cs, and add the following using statements:

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;
    using Microsoft.WindowsAzure.ServiceRuntime;

  3. Add the following code to Default.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        cmdAddToQueue.Click += new EventHandler(cmdAddToQueue_Click);
        cmdShowQueue.Click += new EventHandler(cmdShowQueue_Click);
    }
    
    void cmdShowQueue_Click(object sender, EventArgs e)
    {
        CloudStorageAccount.SetConfigurationSettingPublisher(
          (configName, configSetter) =>
        {
            // Provide the configSetter with the initial value
            configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
        });
    
    
        var storageAccount =
          CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
    
        CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient();
        CloudQueue queue = queueStorage.GetQueueReference("testqueue");
        queue.CreateIfNotExist();
    
        string queueContents = "";
    
        while (queue.PeekMessage() != null)
        {
            queueContents += queue.GetMessage().AsString + "<BR>";
        }
    
        litQueueContents.Text = queueContents;
    
        CloudQueueMessage readMessage = queue.GetMessage();
    }
    
    void cmdAddToQueue_Click(object sender, EventArgs e)
    {
        CloudStorageAccount.SetConfigurationSettingPublisher(
          (configName, configSetter) =>
        {
            // Provide the configSetter with the initial value
            configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
        });
    
        var storageAccount =
          CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
    
        CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient();
        CloudQueue queue = queueStorage.GetQueueReference("testqueue");
        queue.CreateIfNotExist();

    CloudQueueMessage message = new CloudQueueMessage(txtMessage.Text);
        queue.AddMessage(message);
    
        txtMessage.Text = "";
    
    }

  4. Tell Azure how to access the storage. Open ServiceDefinition.csdef, and add the following inside the ConfigurationSettings block:

    <Setting name="DataConnectionString" />

  5. Add the following settings in the ServiceConfiguration.cscfg configuration block:

    <Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />

  6. Press F5 to run the application.

  7. You will see a text box and a button. Enter something like Message1 in the text box, and then click the Add button.

  8. Click Show Queue to display the contents of the queue. The queue should show your message.

  9. Click Show Queue again. No items should be returned because they have been read already.

  10. Enter another item into the text box, and click Add.

  11. Enter a further item into the text box, and click Add.

  12. Click Show Queue, noting all the items displayed (Figure 16-19).

Figure 16.19. Test queue application

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.93.169