Microsoft Azure .NET Queuing Patterns

Azure Queues are part of the Azure Storage offerings.  Microsoft’s Azure SDK for .NET makes developing with Azure Queues accessible to .NET developers.   SDK documentation is, for the most part, complete.  There are, however, underlying patterns to understand if a developer wants to utilize some more advanced features.  Some examples are the patterns behind message creation/consumption timeouts; and .NET Asynchronous support.  These patterns will be explained and demonstrated in the paragraphs that follow.

    Windows Azure 90 day free trial. Try it Now!    

Getting Started

All code samples are based on the June 2012 Azure CTP and developed in Visual Studio 2010.  A complete setup explanation is beyond the scope of this article.  Instead I’ll provide a synopsis.

First, developers must install the Azure SDK for .NET.  The June 2012 SDK release can be found here http://www.microsoft.com/en-us/download/details.aspx?id=29988.  An Azure Account is required to experiment with Azure Queues.  A quick way to get access is to setup a Trial Account.  The Azure Trial link is everywhere in the Azure documentation.  As of writing this article, there was a trial link here http://www.windowsazure.com/en-us/.  Setting up the Trial version requires an email address, Windows Live Id, a phone number, and a Credit card.  More account setup details can be found here http://www.developer.com/services/step-by-step-guide-to-setting-up-a-windows-azure-free-trial.html

Azure communication is done through email.  The Windows Live Id is for authentication with the Admin portal.  “Keyword” setup information is passed via the phone number or text option to a wireless phone.

After setting up an Azure Account a developer must create a storage account.  An Azure Storage account provisions Azure for code access to Storage mechanisms like: Blobs, Tables, and Queues.  Like all Storage, accessing Queuing from .NET code requires the AccountKey.

Queuing Overview

As stated earlier, what follows is a deeper dive into some important Azure .NET Queuing patterns.  If you’re looking for a basic you’ll find one here http://www.windowsazure.com/en-us/develop/net/how-to-guides/queue-service/

The following code opens access to Azure Queue Storage, sends a message, and then retrieves the message.

var acct = "DefaultEndpointsProtocol=https;AccountName=nametest;AccountKey=??????????";
 
var cloudAcct = CloudStorageAccount.Parse(acct);
var client = cloudAcct.CreateCloudQueueClient();
 
var queue = client.GetQueueReference("testqueue");
queue.CreateIfNotExist();
 
var msg = new CloudQueueMessage("Some data");
 
//2 minutes time to live
queue.AddMessage(msg, new TimeSpan(0, 2, 0));
 
//Unlock if delete not called in 30 seconds.
var recMsg = queue.GetMessage(new TimeSpan(0, 0, 30));
 
Console.WriteLine("Sent and received test message Data == " + recMsg.AsString);
 
queue.DeleteMessage(recMsg);

The CloudStorageAccount class provides a way to validate connect string formatting and create various Azure Storage Objects.  Access to Queuing Storage happens through the CloudQueueClient.  CloudQueueMessage handles the message packaging details.  Developers can send a string or use various .NET Serialization techniques to encode a message payload into bytes.

Queuing Storage requires the Windows.Azure.StorageClient assembly and the Configuration assembly.  Developers will find references to these in the .NET References dialog.  Assemblies are based on .NET 2.0 so they should work with older .NET versions.

Some of the methods in the sample code passed TimeSpan parameters.  The Timespan parameters are part of another Azure Queue Pattern requiring a more detailed explanation.

Lock and Message Timeouts

When deciding how to leverage Azure Queues; there are a number of architectural decisions a developer must make.  Here are sample requirements that have a substantial impact on architecture:

  • Though Message Queuing is Asynchronous; processing a message may require some timeliness. 
  • Overall throughput may be important and Azure storage cost must be considered.  Every message may not be important to process. 
  • Message Queues often control or throttle access to a processing resource like a database. 
  • Queue clients may number in the thousands, but there may be just a handful of processing resources.  Multiple processing resources may be sharing a single Queuing resource. 
  • Resources like Databases may occasionally go offline creating failures during message processing.

Azure Storage charges by usage.  There are containment and accessing costs.  Systems that collect, for example, logging information may be simply transmitting a set of state values on a particular interval.  At times it may not be important to collect every update.  Setting a TimeToLive value ensures that a message will only be consumed if it is meaningful.   TimeToLive is demonstrated in the following code taken from the previous sample.

//2 minutes time to live
queue.AddMessage(msg, new TimeSpan(0, 2, 0));

LockTimeouts are important for message consumption.  If a message causes an Exception in a Queuing client attempting to process the message, a Queuing system should attempt some type of retry.  A retry may not be possible if the Queuing Client has completely crashed.  Azure Queues support LockTimeouts that return a message if it hasn’t been processed within a time interval.  LockTimeouts are demonstrated in the following code.

//Unlock if delete not called in 30 seconds.
var recMsg = queue.GetMessage(new TimeSpan(0, 0, 30));

If client responsiveness is important, one way to achieve responsiveness is to use Asynchronous calls.  Asynchronous methods are part of another Azure Queue pattern.

Asynchronously with Tasks

The .NET Task Parallel Library (TPL) has become a foundation for Concurrency and Asynchronous operations.  TPL is new to .NET 4.0.  Some new .NET 4.5 features are built around the Task Parallel Library.   Understanding how Tasks work is the key to understanding the TPL.  A complete introduction to Tasks is beyond the scope of this article, but you’ll find one here http://www.codeguru.com/columns/experts/article.php/c17197/Understanding-Tasks-in-NET-Framework-40-Task-Parallel-Library.htm .  So, what follows is a Task overview.

Microsoft built TPL to maximize a multi-CPU computer’s potential.  A Task class is a means to package a workload so it can be scheduled on a Thread Pool.  Tasks can be chained together each waiting on the success or failure of the other.  New Tasks can be automatically created contingent on completion of a prior Task; this is called a Continuation.

As mentioned earlier; the Azure SDK is based on an older .NET version so it supports an older Asynchronous operation model called the Asynchronous Programming Model (APM).  In the APM model an operation has a Begin Method that starts the operation and an End Method that concludes the operation.  In between Begin and End a developer must supply a Callback method linking the two operations together.  Luckily TPL includes methods to marry the old model and the new model.  Following is code demonstrating sending and receiving a message asynchronously.

var acct = "DefaultEndpointsProtocol=https;AccountName=nametest;AccountKey=??????????";
var cloudAcct = CloudStorageAccount.Parse(acct);
var client = cloudAcct.CreateCloudQueueClient();
 
var queue = client.GetQueueReference("testqueue");
queue.CreateIfNotExist();
 
var msg = new CloudQueueMessage("Some data");
 
var taskAdd = Task.Factory.FromAsync<CloudQueueMessage>(new 
    Func<CloudQueueMessage,AsyncCallback,object,IAsyncResult>((m,cb,obj)=>
{
    return queue.BeginAddMessage(m,new TimeSpan(0, 2, 0),cb,obj);
}), queue.EndAddMessage,msg,null);
 
taskAdd.Wait();
 
var taskGet = Task.Factory.FromAsync<TimeSpan,CloudQueueMessage>(queue.BeginGetMessage, queue.EndGetMessage,new TimeSpan(0, 0, 30), null);
taskGet.Wait();
 
Console.WriteLine("Sent and received test message Data == " + taskGet.Result.AsString);
 
queue.DeleteMessage(taskGet.Result);
 

Both samples demonstrate Waiting for a Task to complete.  Tasks implement the IAsyncResult interface making them interoperable with the old and the new way.

When the Task completes any returned data can be found in the Result property.  Operations that end with an Exception will have a non-null Exception property.  Asynchronous operations creating an Exception will bubble the exception to the method containing the Wait call.  There are alternatives to calling Task.Wait, however, an Exception must always be inspected. 

Tasks support Waiting on multiple Tasks.  So, for example, the sample could have waited on the Write and the Read rather than waiting on each independently.

Resources at the end of the article include more TPL details.

Conclusion

Azure Queues are part of the Azure Storage offerings.  Microsoft’s Azure SDK for .NET makes developing with Azure Queues accessible to .NET developers.   There is considerable documentation on Azure Queuing basics; so this article did a deep dive into some more advanced Queuing patterns.

Resources

Task Parallel Library resources

Understanding Tasks in .NET Framework 4.0 Task Parallel Library
http://www.codeguru.com/columns/experts/article.php/c17197/Understanding-Tasks-in-NET-Framework-40-Task-Parallel-Library.htm

Microsoft .NET Framework 4.0 Task Parallel Library Continuations
http://www.codeguru.com/csharp/article.php/c18361/Microsoft-NET-Framework-40-Task-Parallel-Library-Continuations.htm

Advanced Task Parallel Library Continuations
http://www.codeguru.com/csharp/article.php/c19209/Advanced-Task-Parallel-Library-Continuations.htm

SpinWait.SpinUntil and the Task Parallel Library
http://www.codeguru.com/csharp/article.php/c19283/SpinWaitSpinUntil-and-the-Task-Parallel-Library.htm

Azure Home site
http://www.windowsazure.com/en-us/

Azure Blog
http://www.windowsazure.com/en-us/community/blog/

June 2012 SDK
http://www.microsoft.com/en-us/download/details.aspx?id=29988



Related Articles

Comments

  • ANhsS XPS UiMo

    Posted by NckFAIIYof on 06/22/2013 02:26am

    http://mudchute.org/stats/vicodin/#15983 vicodin extra strength dosage - generic vicodin high

    Reply
  • Beats musik komme til dig af Dr. Dre

    Posted by wanzixiao on 06/04/2013 12:37pm

    [url=http://www.nyebeatsbydrdre.350.com/]beats by dre danmark[/url] Fordelene og ulemperne ved de forskellige typer af hovedtelefonerHeadset hovedtelefoner, takket være en større enhed, lydkvaliteten er mere bedre, men på grund af nogle store volumen, ikke er nem at bære. Ørepropper lille enhed design, kompakt og let at bære, velegnet til udendørs brug. Ørekrog øretelefon typen headset med ørepropper i mellem et smukt udseende, komfortable at bære, lyde bedre egenskaber end ørepropper ulempe er følsom over for støj udefra, som er egnet til at bære i foråret, og denne sæson . Forskellige typer hovedtelefoner er anderledes, for at se om du ønsker at bruge under hvilke omstændigheder. [url=http://www.kbhretelefonerbillig.webstarts.com/]Køb høretelefoner billig[/url] Projektet kan opnås på en måde at bruge god til handling og magt lav overflod, og værdiskabende tilbehør. Den faktiske headset komfort element er virkelig stor. Du vil finde disse slag Dre taktslag buy berømtheder certificering af DRE pro at slå Dre studio hovedtelefoner i øret. BIII komme med en rengøringsklud til at sikre, at de er på udkig efter. For nylig fandt jeg en fælles ven kørte vej sat på afDr. Dre Studio hovedtelefoner, derfor vil jeg opfordre til at spørge hende, hvordan de ser ud. [url=http://www.beatsbydrdredanmark.webgarden.com/]beats by dre tilbud[/url] Beats enkel sammen med skiftes i sammen med frigivet eventuelle udskiftelige pandebånd er nået smertefri sammen med sat på mod stillet af blot beats by dremagneter. Dybest set vælter enhver pandebånd på grund af en indehaver toppen inden for headsets sammen med tilføje en anden. Progressionen modtage en smule af med en minimal sammen med før du kender det hele, vil du have et helt andet sæt af moderigtigt koordinerede headsets. Brug hvad vores firma er næsten garanteret er helt sikkert pude-soft kunstlæder høre kopper kaffe ud over et pandebånd, at leveres i en god samling for farvestoffer sammen med sorter disse virkelig er nødt til at føle, udseende sammen med tone enhver utroligt perfekt under forretningen.

    Reply
  • Jordan shoes mentioned Gene to come by the brand, a disunion of Nike

    Posted by TaddyGaffic on 04/24/2013 01:58pm

    The venue has beautiful lighting and last time we played Big Kitty opened for us ( they are now broken up) but at the time they were a smooth sangin honky-tonk bluegrass band that made you wanna cry. This time our openers were The Nim Nims a local band that sounds [url=http://markwarren.org.uk/property-waet.cfm]air max 90[/url] kinda Christian but not. So as you can imagine Then our cousin band Lily and the Tigers who are touring with us to sxsw. With just about any exercise routine, you will need to wear sneakers (excluding yoga, Pilates, etc Athletic shoes support your ankles and prevent your bare feet from stepping on sharp objects. Plain and simply put, they help you avoid injury. However, simply jumping into your latest pair of kicks doesn mean you [url=http://fossilsdirect.co.uk/glossarey.cfm]nike huarache free[/url] ready to hit the gym. Instead of "outdoors", think "hiking". Instead of hiking, think "hiking boots". If you know what you are talking about and enjoy the subject, it will be easier to talk to people (online or in person) and transmit your message of enjoyment to them. You need some arch support to conform the rough shape roads. Mens Timberland boots are just the boots which have the ankle support to conform to the shape of the bottom of your foot. They apply the very hard fiberglass shank to strengthen the sole and arch supports.. In [url=http://markwarren.org.uk/property-waet.cfm]air max 90 uk[/url] midfield, Frank Lampard, Raul Meireles and Rafael Ramirez lined up. Back online, John Terry wear adipower predator sl and David Luiz wear nike mercurial vapor superfly iii cr partner defender, Ashley Cole wear adidas adipure iv sl and Jose Bosingwa wear nike ctr360 maestri ii elite either side, goalkeeper Petr Cech wear adidas adipower predator. 47 minutes, Florent Malouda wear nike tiempo legend iv elite to form a breakthrough in the left wing after the transfer, Juan Mata wear adipure iv within a small restricted area shot the ball into the bottom left corner, Chelsea 1-0 Portsmouth

    Reply
  • New nike E book Unveils The Simplest Way To Dominate The nike Scene

    Posted by icoppyapedcap on 04/24/2013 03:23am

    EqxIamTuoVjt [url=http://www.nikeyasuijp.com/]ナイキゴルフ[/url]ZstZkbIuxOoq [url=http://www.nikeyasuijp.com/nike-air-force1エアフォース1-c-14.html]ナイキ エアフォース[/url]MysCarRlgHim [url=http://www.nikeyasuijp.com/nike-air-maxエアマックス-c-12.html]nike air max[/url]YqnUsyHunBml [url=http://www.nikeyasuijp.com/nike-air-jordanエア-ジョーダン-c-13.html]ナイキランニング[/url]SprIohYemAlw

    Reply
  • A earliest vue, la Nike Dunk Short LR Thermo est une not entirely paire en daim et cuir noir et bien non

    Posted by cnbpitldb on 03/22/2013 01:16pm

    Show Jordan de perk en bonus [url=http://www.hollistercoefrance.fr]hollister france[/url] de la contend quotidienne, et maintenant vous etes sur le milieu de basket pouvez voir l'ombre de la Akin to like to to Jordan, Fail prohibited Jordan [url=http://www.abercrombiefrancevparis.fr]abercrombie[/url] empreintes peuvent etre trouves dans la rue, et les gens de with an on of en with an bourgeon of comme la Eager Jordan un pro, est toute la famille loose-fitting portant la Aver shared to Jordan [url=http://www.airjordanfrpascherz.com]air jordan[/url] jouer partout, les membres des 80 les fans AJ n'y a pas de telle absent-mindedness sur elle? L'ikon de marque Abercrombie & Fitch est fortement promue comme un concept de gross out room de smack into proche-luxe international. La societe [url=http://www.abercrombieafranceusolde.fr]abercrombie france[/url] a upon a cultiver une ornamented haut de gamme apres l'ouverture 2005 de son magasin phare a c?te de la Cinquieme Avenue Prada et autres detaillants haut de gamme. Ayant utilise lavaliere des annees materiaux de haute qualite dans la [url=http://www.hollisteruonlineshops.de]hollister[/url] construction de sa marchandise et les prix de ?quasi-luxe?, les niveaux, la marque de luxe decontracte a ete introduite [23] comme un terme du dictionnaire de fiction avec de multiples definitions telles que "[l'aide] le added beau cachemire, coton pima et cuir [url=http://www.hollisterfranceamagesin.fr]hollister france[/url] haute qualite with creer l'ultime dans les vetements decontractes richness of men, conscient ?et? la mise en ?uvre et / ou l'incorporation temps honore machines ... gush produire du denim le accessory exclusif ... ?[24] Cette chuck haut de gamme a permis A & F sooner than the bucketful ouvrir des magasins dans les instruments internationaux haut de gamme endroits et de promouvoir davantage Délocalisation de la moulding vers des pays à bas salaires comme le Bangladesh, la Chine, l'Inde et le Sri Lanka est devenu doable lorsque l'Accept multifibres (AMF) a été supprimé. Le ministère des Affaires étrangères, qui a [url=http://www.abercrombiexandfitchukes.co.uk]abercrombie uk[/url] instauré des quotas sur les importations de textiles, a été considérée comme une mesure protectionniste. [Citation nécessaire] La mondialisation est souvent cité comme le facteur le succour contributif aux mauvaises conditions de travail des [url=http://www.abercrombiesdeutschlandshopu.com]abercrombie[/url] travailleurs du vêtement. Bien que de nombreux pays reconnaissent les traités comme l'Organisation internationale du Travail, qui tentent d'établir des normes crop up la sécurité des travailleurs et des droits, de nombreux pays ont fait [url=http://www.abercrombiesdeutschlandshopu.com]abercrombie deutschland[/url] des exceptions à certaines parties des traités ou pas de bien les appliquer. Inde canon in excess of quest of the most scrap exemple n'a pas ratifié les articles 87 et 92 du traité.

    Reply
  • Herbal Diet Pills That Work

    Posted by GoosyIdory on 01/05/2013 06:54am

    7030 ovljc He gained 30 pounds in 1992 to play an alcoholic baseball coach in "A League of Their Own," and lost all of it and more for the 1993 movie "Philadelphia," where he played an AIDS patient. [url=http://mygreencoffeebean.net/what-are-the-benefits-of-green-coffee-bean-extract-to-me/]pure green coffee extract[/url] 4377 mhzil After following a computer or online weight-loss program for six months, participants in various studies shed more pounds than those who received more basic care, such as a paper workbook or pamphlet. Individuals who were in weight-maintenance programs that used computer-guided tools also kept weight off better than those who got the basic care. The online programs often included interactive features, message boards and messaging capabilities. 3525 vrjjx But Ms Franklin said in a recent interview that she 'would never' go under the surgeon's knife for weight loss surgery. Make sure to set realistic goals that you know you can actually attain. Don't say I want to lose 30-40 pounds in a month, this is unrealistic and will usually result in you quitting the program. You want to be healthy in your journey to lose weight. Always do you own complete due diligence before buying and selling any stock. To lose weight, some people are still buying into HCG drops and injections, which have received criticism from the FDA because they aren't proven to help people shed pounds.

    Reply
  • How Coffee Diet Is Hot!

    Posted by reerneNow on 01/04/2013 03:32pm

    6571 pqojp But it was the super-strict Macrobiotic Diet, branded 'a mix of sound dietary guidance, mysticism, folklore, and nonsense' by the experts, that claimed last place. Although the rate of alcohol abuse climbed only 2 percentage points a fter the procedures, this translates into more than 2,000 new cases of abuse every year in the United States, according to the findings, which were presented at the annual meeting of the American Society for Metabolic and Bariatric Surgery in San Diego. [url=http://mygreencoffeebean.net/what-are-the-benefits-of-green-coffee-bean-extract-to-me/]pure green coffee extract[/url] 6361 sojug MetabolismThe vast majority of people who are interested in weight loss are already familiar with the fact that metabolism is directly linked to weight loss, and that a fast metabolism results in fast weight loss. Unfortunately however, many people fail to realize that if they cut back on their calorie intake to severely, it can actually result in their metabolism slowing down, thus slowing down the weight loss process. 1546 tswmg One study followed patients up to a month after surgery found that 7 percent experienced some problems, mainly minor wound infections. But massive bleeding, kidney failure and other serious complications occurred in more than 2 percent of patients. Yet it's more than just a cosmetic procedure, said Dr. Jason Spector, who presented the findings at the annual meeting of the American Society of Plastic Surgeons ASPS in Denver this week.

    Reply
  • fax

    Posted by Nicole Lee on 10/11/2012 07:19pm

    Thanks, This is a good article

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds