Azure Worker Role

Worker Roles are little different than Web Role, there are lot of extra things expected beside the actual functional things. So XecMe is at your rescue. Below given are the typical Worker Role

public class MyWorkerRole: RoleEntryPoint
{
   ....
    public override bool OnStart()
    {
         //Setting up a Queue for processing order
    .....
    }

    public override void Run()  
    {
        while(true)
        {
           //Read the Queue
           // Process the order aand update
        }
    }
}

By using XecMe your Worker Role will slim down to

public class MyWorkerRole: RoleEntryPoint
{
   ....
    public override bool OnStart()
    {
    }

    public override void Run()  
    {
        TaskManager.Start(new TaskManagerConfig());
        TaskManager.WaitTaskToComplete();
    }
}

Add a Task that does the order processing

public class MyOrderProcessing: ITask
{
   ....
    public void OnStart(ExecutionContext context)
    {
         //Setting up a Queue for processing order
    .....
    }

    public ExecutionState OnExecute(ExecutionContext context)  
    {
           //Read the Queue
           // Process the order aand update

    }

    public void OnStop(ExecutionContext context)
    {
         //Clean up the Queue connection
    .....
    }
}

Configure it to run in parallel using ParalelTaskRunner

<configuration>
  <configSections>
    <sectionGroup name="xecMe.Core" type="XecMe.Configuration.XecMeSectionGroup, XecMe.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e34de6d98c38471a">
      <section name="extensions" type="XecMe.Configuration.ExtensionsSection, XecMe.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e34de6d98c38471a"/>
      <section name="taskManager" type="XecMe.Core.Configuration.TaskManagerSection, XecMe.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e34de6d98c38471a"/>
    </sectionGroup>
  </configSections>
  <xecMe.Core>
    <extensions>
      <taskRunners>
        <!-- Whatever is defined in the name below the same tags are to be used in taskRunners section under taskManager -->
        <!-- The type is the type of the element for the defined tag -->
        <!-- One can implement other type of TaskRunner by inheriting from TaskRunner class-->
        <!-- TaskRunnerElement inheriting from TaskRunnerElement class-->
        <add name="parallelTaskRunner" type="XecMe.Core.Configuration.ParallelTaskRunnerElement, XecMe.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e34de6d98c38471a"/>
        <add name="timerTaskRunner" type="XecMe.Core.Configuration.TimerTaskRunnerElement, XecMe.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e34de6d98c38471a"/>
        <add name="eventTaskRunner" type="XecMe.Core.Configuration.EventTaskRunnerElement, XecMe.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e34de6d98c38471a"/>
      </taskRunners>
    </extensions>
    <taskManager>
      <taskRunners>
        <parallelTaskRunner name="OrderProcessing" taskType="MyOrderProcessing, WorkerRole, Version=1.0.0.0, Culture=nuetral, PublicKeyToken=null" minInstances="1" maxInstances="10">
          <parameters>
            <parameter name="param1" value="val1"/>
            <parameter name="other" value="val2"/>
          </parameters>
        </parallelTaskRunner>
      </taskRunners>
    </taskManager>
  </xecMe.Core>
</configuration>

This will simplify your coding making it neat & clean while enabling you to run Order Processing on multiple thread. Isn't it simple and cool :-)

Last edited Feb 5, 2013 at 11:47 PM by slolam, version 2

Comments

No comments yet.