How Web Role talks to Worker Role in Azure : Queue

The storage services in Azure is accessible through REST API, which is awesome to allow other technology stack to make use of it. However, I don't want to call the REST API myself and deal with the low level puts, responses code, etc.

At the moment, the closest thing to a wrapper for the REST API is the StorageClient project included in the Azure SDK Demo Project.

By default the project is located in c:\program files\windows azure sdk\1.0\sample.zip. Extract it and there's a solution called StorageClient.

Putting stuff in into the queue in a Web Role

public class AspNetPage_In_AWebRole : Page
{
  protected void SubmitButton(object sender, eventargs e)
  {
    // create queue from config
    QueueStorage queueStorage = QueueStorage.Create(GetQueueStorageAccountFromConfiguration());
    MessageQueue queue = queueStorage.GetQueue("workItems");

    // usually wrap in a retry mechanism to make sure
    // it handles the situation when storage service is down
    queue.CreateQueue();

    // add stuff into the queue
    queue.PutMessage(new Message("work item"));
  }
}

There's 2 construct for Message which takes either string or bytes. So it's pretty handy for most situation. If only we can use BinarySerializer on MediumTrust. Transporting objects along through queues would be a breeze.

Getting stuff out on the worker role

public class AWorkerRole : RoleEntryPoint
{
  public override void start()
  {
    // create queue from config - exactly the same from webrole
    QueueStorage queueStorage = QueueStorage.Create(GetQueueStorageAccountFromConfiguration());
    MessageQueue queue = queueStorage.GetQueue("workItems");

    queue.CreateQueue();

    while(true)
    {
      // usually wrapped in some sort of error handling
      // to maintain a perpetual loop

      // get work item from the queue
      Message msg = queue.GetMessage();

      if (msg != null)
      {
        // do stuff
        queue.DeleteMessage(msg);
      }
      else
      {
        // poll again later
        Thread.Sleep(1000);
      }
    }
  }

  public override RoleStatus GetHealthStatus()
  {
    // maybe check the queue length
    return RoleStatus.Healthy;
  }
}

I wish I don't have to do the polling myself. I feel uneasy about looking at while(true) and the Thread.Sleep() line. Hopefully Microsoft will be handling the poll queue scheduling by release.

  1. says:

    mas saya lagi di dubai juga nih.. boleh minta japrinya? :)

    pengen banget ketemu sesama orang it dari indonesia disini.

    wassalam

  2. says:

    @Iwan Hi, you sure can. my email is ronaldwidha at gmail dot com!

  3. Single Bank Platform: F# Cloud It! (Part 8) « Tales from a Trading Desk says:

    [...] How Web Role talks to Worker Role in Azure : Queue [...]

  4. Ryan CrawCour says:

    Did you know you can talk directly between roles without going through a queue first. You can setup internal endpoints to enable direct communication between roles.

  5. ronaldwidha says:

    This post was written in May 2009, at that point in time, I couldn't do that :)

  6. Raja Dev says:

    it is good