DESKTOP

Windows Azure : Processing with worker roles - A simple worker role service

2/13/2011 8:57:12 AM
When it’s all said and done, working with worker roles is quite easy. The core of the code for the worker role is the normal business code that gets the work done. There isn’t anything special about this part of a worker role. It’s the wrapper or handler around the business code that’s interesting. There are also some key concepts you’ll want to pay attention to, in order to build a reliable and manageable worker role.

In this section, we’ll show you how to build a basic worker role service. You have to have some way to communicate with the worker role, so we’ll first send messages to the worker through a queue, showing you how to poll a queue.  We’ll then upgrade the service so you can use inter-role communication to send messages to your service.

We’ll use the term service fairly loosely when we’re talking about worker roles. We see worker roles as providing a service to the rest of the application, hopefully in a decoupled way. We don’t necessarily mean to imply the use of WS-* and Web Service protocols, although that’s one way to communicate with the role.

Let’s roll up our sleeves and come up with a service that does something a little more than return a string saying “Hello World.” In the next few sections, we’ll build a new service from scratch.

15.1.1. No more Hello World

Because Hello World doesn’t really cut it as an example this late in any book, we’re going to build a service that reverses strings. This is an important service in any business application, and the string-reversal industry is highly competitive.

There will be two parts to this service. The first part will be the code that actually does the work of reversing strings—although it’s composed of some unique intellectual property, it isn’t very important in our example. This is the business end of the service. The other part of the service gets the messages (requests for work) into the service. This second part can take many shapes, and which design you use comes down to the architectural model you want to support. Some workers never receive messages; they just constantly poll a database, or filesystem, and process what they find.

To build this string-reversal service you need to open up Visual Studio 2010 and start a new cloud project. For this project, add one worker role, and give it the name Worker-Process String, as shown in figure 1.

Figure 1. To build the service, you’ll start with a worker role. It’ll do all of the work and make it easy to scale as your business grows, especially during the string-reversal peak season.


At the business end will be our proprietary and award-winning algorithm for reversing strings. We intend to take the string-reversal industry by storm and really upset some industry captains. The core method will be called ReverseString, and it will take a string as its only parameter. You can find the secret sauce in the following listing. Careful, don’t post it on a blog or anything.

Listing 1. The magical string-reversal method
private string ReverseString(string originalString)
{
int lengthOfString = originalString.Length;
char[]reversedBuffer = new char[lengthOfString];

for (int i = 0; i < lengthOfString; i++)
{
reversedBuffer [i] = originalString[lengthOfString - 1 - i];
}

return new string(reversedBuffer);
}

The code in the previous listing is fairly simple—it’s normal .NET code that you could write on any platform that supports .NET (mobile, desktop, on-premises servers, and so on), not just for the cloud. The method declares a character array to be a buffer that’s the same length as the original string (because our R&D department has discovered that every reversed string is exactly as long as the original string). It then loops over the string, taking characters off the end of the original string and putting them at the front of the buffer, moving along the string and the buffer in opposite directions. Finally, the string in the buffer is returned to the caller.

For this example, we’ll put this business logic right in the WorkerRole.cs class. Normally this code would be contained in its own class, and would be referenced into the application. You can do that later if you want, but we want to keep the example simple so you can focus on what’s important.

We’ve chosen to put this service in a worker in the cloud so that we can dynamically scale how many servers we have running the service, based on usage and demand. We don’t want to distract our fledgling company from writing the next generation of string-reversal software with the details and costs of running servers.

If you ran this project right now, you wouldn’t see anything happen. The cloud simulator on your desktop would start up, and the worker role would be instantiated, but nothing would get done. By default, the worker service comes with an infinite polling loop in the Run method. This Run method is what is called once the role instance is initialized and is ready to run. We like that they called it Run, but calling it DoIt would have been funnier.

Now that you have your code in the worker role, how do you access it and use it? The next section will focus on the two primary ways you can send messages to a worker role instance in an active way.

Other  
  •  Windows 7 : Configuring Disks and Drives (part 2) - Converting a Basic Disk to a Dynamic Disk
  •  Windows 7 : Configuring Disks and Drives (part 1) - Using Disk Management
  •  Windows Server 2008 : Domain Name System and IPv6 - DNS in Windows Server 2008 R2
  •  Windows Server 2008 : Domain Name System and IPv6 - Understanding the Evolution of Microsoft DNS
  •  Windows Server 2008 : Domain Name System and IPv6 - Other DNS Components
  •  Windows 7 : Keeping Your Family Safe While Using Your Computer (part 2)
  •  Windows 7 : Keeping Your Family Safe While Using Your Computer (part 1)
  •  Windows 7 : Managing Access Permissions with Group Accounts
  •  Windows Server 2008 : Domain Name System and IPv6 - Understanding DNS Queries
  •  Windows Server 2008 : Domain Name System and IPv6 - Performing Zone Transfers
  •  Windows Server 2008 : Domain Name System and IPv6 - Understanding DNS Zones
  •  Windows 7 : Managing Other People’s User Accounts (part 2)
  •  Windows 7 : Managing Other People’s User Accounts (part 1)
  •  Windows Server 2008: Domain Name System and IPv6 - Resource Records
  •  Getting Started with DNS on Windows Server 2008 R2
  •  Windows Server 2008: Domain Name System and IPv6 - Understanding the Need for DNS
  •  Windows 7 : Managing Your User Account
  •  Windows 7 : Troubleshooting Problems with Windows Media Center
  •  Windows 7 : Fine-Tuning the Settings for Windows Media Center
  •  Windows 7 : Installing and Configuring Windows Media Center Using the Wizard
  •  
    Top 10
    What To Do With An Old Mac (Part 4)
    What To Do With An Old Mac (Part 3)
    What To Do With An Old Mac (Part 2)
    What To Do With An Old Mac (Part 1)
    Technology News: Cloud Computing To Influence Security Offerings
    We Help You Find Your Ideal Smartphone (Part 5)
    We Help You Find Your Ideal Smartphone (Part 4)
    We Help You Find Your Ideal Smartphone (Part 3)
    We Help You Find Your Ideal Smartphone (Part 2)
    We Help You Find Your Ideal Smartphone (Part 1)
    Most View
    Communicate Between Two Machines on the Same Network (WCF)
    SQL Server 2008 : Demystifying Data Types - Computed Columns
    Retouch Images In iPhoto
    Becoming an Excel Programmer : Navigate Samples and Help
    Blackberry World 2012 (Part 3) - Mobile computing platform
    Becoming an Excel Programmer : View Results
    Bundle Up To Save Some Cash!
    Crucial Ballistix Tactical LP 16GB Kit
    Promote Your Photography (Part 1)
    Improve Your Mac (Part 9) - Understand Activity Monitor
    Three Quick Ways To View Notification Center
    Programming the Mobile Web : Testing and Debugging (part 1) - Remote Labs
    Canon EOS 650D - High-Ranking DSLR
    All About Nexus 7 (Part 3)
    Homeplug Problems In A Factory Setting (Part 2)
    Windows Server 2008: DHCP/WINS/Domain Controllers - Exploring Global Catalog Domain Controller Placement
    Broadberry CyberServe XE5-R224 - Top-Value Rack Server
    Fractal Design Node 605 – Elegant HTPC Case
    Rise Of The Mobile Processors (Part 3)
    The key to security