Queues, table storage and blob storage are unified under a Storage Account and you probably have to create one now. From the Azure Management portal click the New button in the lower left hand corner and select Data Services, Storage, Quick Create. Just specify a name, the location of your storage and your preferred replication strategy.
Set up your project
For the first API tests I’ll create a simple Console application to demo a few things. To access your queues from .NET you will need the Storage API which can be referenced easily as a NuGet package named Windows Azure Storage (the sample code uses Storage API 4.3.0).
Create a queue
Let’s create a queue. Of course you can do that with your favorite tool too (e.g. Visual Studio or Azure Management Studio), but as you’ve seen already it’s also possible through the API. The main class for your communication is called CloudQueueClient and an instance is provided through a factory method from the CloudStorageAccount class.
To access your storage account you will need StorageCredentials: take the previously specified name of your storage account and one of the access keys that you can find for your storage account in the Azure portal (How to: View, copy, and regenerate storage access keys). The second parameter of the CloudStorageAccount constructor states if you want to use HTTPS for the communication.
With the CloudQueueClient.GetQueueReference method you can get a reference to your queue. Before working with the queue, you have to create it by using CreateAsync or Think carefully if you want to spend this additional call every time you’re accessing your queue, usually you can setup your queue once and there’s no need to ensure the existence again and again.
Inserting messages
Next we want to insert a TaaS ticket request in our newly created queue. Queue messages can be stored as string or as byte array but be aware of the size limits and the base64 encoding of the .NET Storage API (see my previous article, “Adding a message: Don’t be chatty”).
I use Newtonsoft.Json to serialize my simple TicketRequest object and pass that JSON formatted string to the constructor of theCloudQueueMessage class. Just call AddMessageAsync on your queue instance and you’re done!
There are some overloads for the AddMessageAsync method that enable you to specify additional options for the message, e.g. the maximum time to allow the message to be in the queue (time-to-live) or the initial invisibility delay. See MSDN for further information.
Processing and deleting messages
Worker roles are probably the best place to process queue messages. Worker roles can be run in parallel, you have auto-scaling features that can depend on queue sizes and they are quite easy to implement. I’ll show a realistic architecture in my last article about queues, but for now you can put the code in your Console application as well.
var messages = await queue.GetMessagesAsync(32, TimeSpan.FromMinutes(2), null, null);
foreach (var message in messages)
{
try
{
// if processing was not possible, delete the messagecheck for unprocessable messages
if (message.DequeueCount < 5)
{
var ticketRequest = JsonConvert.DeserializeObject<TicketRequest>(message.AsString);
// process the ticket request (expensive operation)
ProcessTicket(ticketRequest);
}
else
{
Log(message, "Processing failed.");
}
// delete message so that it becomes invisible for other workers
await queue.DeleteMessageAsync(message);
}
catch (Exception e)
{
Log(message, e.Message);
}
}
|