Azure Basic - Windows Storage Enhancement (Queue)

盖和泰
2023-12-01

Queue

  1. Get messages in a batch (In Sample 1)
  2. Poison message : use dequeuecount (In Sample 1)
  3. Delete message: always add try-catch at multiple consumers situation (In Sample 1)
  4. Always add time sleep to avoid "throttling" & "Money, Money, Money" (In Sample 1)
  5. Retry Policy (Microsoft.WindowsAzure.StorageClient.RetryPolicies)
    1.  http://blogs.msdn.com/b/windowsazurestorage/archive/2011/02/03/overview-of-retry-policies-in-the-windows-azure-storage-client-library.aspx
    2. "

      We strongly recommend using the exponential backoff retry policy provided by default whenever possible in order to gracefully backoff the load to your account, especially if throttling was to occur due to going over the scalability targets posted here. You can set this manually by via [Client].RetryPolicy = RetryPolicies.RetryExponential(RetryPolicies.DefaultClientRetryCount, RetryPolicies.DefaultClientBackoff).

      Generally speaking a high throughput application that will be making simultaneous requests and can absorb infrequent delays without adversely impacting user experience are recommended to use the exponential backoff strategy detailed above. However for user facing scenarios such as websites and UI you may wish to use a linear backoff in order to maintain a responsive user experience.

      Joe Giardino

      "

  6. Always use partition key in the query
  7. Note: all messages will be encode using Base64

Sample 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string accountName = "devstoreaccount1";
            string key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
            string containerName = "containername";
            string blobName = "blobname";

            StorageCredentialsAccountAndKey credentials =
    new StorageCredentialsAccountAndKey(accountName, key);

            string baseUri = string.Format("http://127.0.0.1:10001/{0}", accountName);

            CloudQueueClient queueStorage = new CloudQueueClient(baseUri, credentials);
            //Retry Policy
            queueStorage.RetryPolicy = RetryPolicies
               .RetryExponential(RetryPolicies.DefaultClientRetryCount,
               RetryPolicies.DefaultMaxBackoff);
            CloudQueue queue = queueStorage.GetQueueReference("guestthumbs");
            queue.CreateIfNotExist();

            for (int i = 0; i < 20; i++)
            {
                var message = new CloudQueueMessage(String.Format("{0},{1},{2}",
                i+":uniqueBlobName",
               "entry.PartitionKey",
               "entry.RowKey"));
                queue.AddMessage(message);
                Console.WriteLine("Input Message :" + message);
            }

            while (true)
            {
                IEnumerable<CloudQueueMessage> msgList = queue.GetMessages(32, TimeSpan.FromSeconds(20));

                if (msgList == null)
                {
                    //Time Sleep
                    Thread.Sleep(TimeSpan.FromSeconds(5));
                }
                else
                {
                    try
                    {
                        foreach (CloudQueueMessage msg in msgList)
                        {
                            if (msg.DequeueCount < 8)
                            {
                                Console.WriteLine("Output Message : "+msg.AsString);
                            }
                            else
                            {
                                //Handle Poison Message
                            }
                            queue.DeleteMessage(msg);
                        }
                    }
                    catch (StorageClientException ex)
                    {
                        if (ex.ExtendedErrorInformation.ErrorCode == "MessageNotFound")
                        {
                            // pop receipt must be invalid
                            // ignore or log (so we can tune the visibility timeout)
                        }
                        else
                        {
                            // not the error we were expecting
                            throw;
                        }
                    }

                }
            }
            
        }
    }
}



 类似资料: