Angel \”Java\” Lopez on Blog

December 9, 2010

Azure: a simple application

Filed under: .NET, Azure, Cloud Computing, Distributed Computing — ajlopez @ 9:19 am

This is my first post here, about Azure programming. An easy start: an application with one web role, and one worker role:

You can download the solution from my AjCodeKatas Google project. The code is at:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/Azure/AzureNumbers

In the initial web page you can enter a number to process:

If you send the number 10, this data is send to a queue:

protected void btnProcess_Click(object sender, EventArgs e)
{
    int number = Convert.ToInt32(txtNumber.Text);
    CloudQueueMessage msg = new CloudQueueMessage(number.ToString());
    WebRole.NumbersQueue.AddMessage(msg);
}

The worker role is reading the queue. It decrements the number, and if the result is still positive, it is reinjected in the queue:

        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("NumberWorkerRole entry point called", "Information");
            CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            QueueUtilities qutil = new QueueUtilities(account);
            CloudQueue queue = qutil.CreateQueueIfNotExists("numbers");
            while (true)
            {
                CloudQueueMessage msg = queue.GetMessage();
                if (msg != null)
                {
                    int number = Convert.ToInt32(msg.AsString);
                    Trace.WriteLine(string.Format("Processing number: {0}", number), "Information");
                    number--;
                    if (number > 0)
                    {
                        CloudQueueMessage newmsg = new CloudQueueMessage(number.ToString());
                        queue.AddMessage(newmsg);
                    }
                    queue.DeleteMessage(msg);
                }
                else
                {
                    Thread.Sleep(10000);
                    Trace.WriteLine("Working", "Information");
                }
            }
        }

You can see the output at Development Fabric UI:

Note the use of AzureLibrary to create a Queue:

        public CloudQueue CreateQueueIfNotExists(string queuename)
        {
            CloudQueueClient queueStorage = this.account.CreateCloudQueueClient();
            CloudQueue queue = queueStorage.GetQueueReference(queuename);
            
            Trace.WriteLine("Creating queue...", "Information");
            Boolean queuecreated = false;
            while (queuecreated == false)
            {
                try
                {
                    queue.CreateIfNotExist();
                    queuecreated = true;
                }
                catch (StorageClientException e)
                {
                    if (e.ErrorCode == StorageErrorCode.TransportError)
                    {
                        Trace.TraceError(string.Format("Connect failure! The most likely reason is that the local " +
                            "Development Storage tool is not running or your storage account configuration is incorrect. " +
                            "Message: '{0}'", e.Message));
                        System.Threading.Thread.Sleep(5000);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return queue;
        }

I borrowed part of this code from Azure SDK samples.

Next steps to explore:

– Add instrumentation to worker role

– Use more instances, and generate more message (an explosion-like pattern)

– Add multithreading support in the worker role

– Example using table and blob storage

And the big ones:

– Inject and run AjSharp (or AjTalk) code at worker roles

– Implements a distributed application using roles (distributed genetic algorithm, distributed fractal or ray-tracer, montecarlo simulation, etc…)

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: