Jeff Brown recently contributed the new Scheduler component to the CastleContrib project. This is a great component but it needs some documentation. Recently he put out some instructions in the usergroup and I thought it would be nice to put it here.
To get this thing up and running you must follow the following steps:
First you need to run the scripts in the Castle.Components.Scheduler.Db directory create the test database and the store procedures.
After that you need to implement the IJob class. Here is a dummy one I used to test out the component:
using System; using Castle.Components.Scheduler; namespace ClassLibrary1 { public class MyJob: IJob { public bool Execute(JobExecutionContext context) { Console.WriteLine("Hello World - " + DateTime.Now.ToShortTimeString()); return true; } } }
And then you need to configure your Windsor configuration to look something like this:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /> </configSections> <castle> <components> <component id="Core.Scheduling.Scheduler" service="Castle.Components.Scheduler.IScheduler, Castle.Components.Scheduler" type="Castle.Components.Scheduler.DefaultScheduler, Castle.Components.Scheduler" /> <component id="Core.Scheduling.JobStore" service="Castle.Components.Scheduler.JobStores.IJobStore, Castle.Components.Scheduler" type="Castle.Components.Scheduler.JobStores.SqlServerJobStore, Castle.Components.Scheduler"> <parameters> <connectionString>Server=(local);Initial Catalog=schedulerTestDb;Integrated Security=SSPI</connectionString> </parameters> </component> <component id="Core.Scheduling.JobRunner" service="Castle.Components.Scheduler.IJobRunner, Castle.Components.Scheduler" type="Castle.Components.Scheduler.DefaultJobRunner, Castle.Components.Scheduler" /> <component id="Core.Scheduling.JobFactory" service="Castle.Components.Scheduler.IJobFactory, Castle.Components.Scheduler" type="Castle.Components.Scheduler.WindsorExtension.WindsorJobFactory, Castle.Components.Scheduler.WindsorExtension" /> <component id="MyJob" service="Castle.Components.Scheduler.IJob, Castle.Components.Scheduler" type="ClassLibrary1.MyJob, ClassLibrary1" /> </components> </castle> </configuration>
And now you are ready to start the scheduler
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected WindsorContainer container;
private void button1_Click(object sender, EventArgs e)
{
container = new WindsorContainer(new XmlInterpreter());
IScheduler scheduler = (IScheduler)container["Core.Scheduling.Scheduler"];
Trigger trigger = new PeriodicTrigger(new DateTime(1900, 1, 1), new DateTime(2900, 1, 1), new TimeSpan(0, 0, 10), 1000);
JobSpec jobSpec = new JobSpec("My job.", "A test job.", "MyJob", trigger);
scheduler.CreateJob(jobSpec, CreateJobConflictAction.Ignore);
scheduler.Start();
}
}
}
Now you are done...you need to check the output for the result.
