Created
April 7, 2011 15:20
-
-
Save ntotten/907987 to your computer and use it in GitHub Desktop.
AzureToolkit Worker Role with MEF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Export] | |
[PartCreationPolicy(CreationPolicy.NonShared)] | |
public class MyFirstJob : IJob | |
{ | |
[Import] | |
public IMyFirstService MyFirstService { get; set; } | |
public void Run() | |
{ | |
// TODO: Do Some Work | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WorkerRole : WorkerRoleEntryPoint | |
{ | |
public override void Run() | |
{ | |
var catalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory); | |
ServiceLocator.SetLocatorProvider(() => | |
{ | |
var container = new CompositionContainer(catalog); | |
return new MefServiceLocator(container); | |
}); | |
using (var tempContainer = new CompositionContainer(catalog)) | |
{ | |
var initializers = tempContainer.GetExportedValues<IInitializer>(); | |
foreach (var initializer in initializers) | |
{ | |
try | |
{ | |
initializer.Initialize(); | |
} | |
catch (Exception ex) | |
{ | |
Trace.TraceError(ex.TraceInformation()); | |
} | |
} | |
} | |
CloudEngine engine = new CloudEngine(); | |
Action<MessageHandlerSettings> configSettings = c => | |
{ | |
c.BatchSize = 32; | |
c.MaxThreads = 1; | |
}; | |
engine.WithMessageHandler<MyFirstMessage, MyFirstCommand>(configSettings); | |
engine.WithMessageHandler<MySecondMessage, MySecondCommand>(configSettings); | |
engine.WithJobScheduler(c => | |
{ | |
c.QueueName = "scheduledjobs"; | |
c.VisibilityTimeout = TimeSpan.FromHours(2); | |
}) | |
// Schedule the first job | |
.ScheduleJob<MyFirstJob>(c => | |
{ | |
c.CronSchedule = Cron.Hour(4).ToString(); | |
}) | |
// Schedule the second job | |
.ScheduleJob<MySecondJob>(c => | |
{ | |
c.CronSchedule = Cron.Hour(8).ToString(); | |
}); | |
engine.Run(); | |
base.Run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment