Azure Basic - Windows Storage Enhancement (Queue)

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;
                        }
                    }

                }
            }
            
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值