Tuesday 2 October 2012

Scheduling with Quartz and Mono on OS X

Quartz.Net is a brilliant library for scheduling in .Net and a great addition to the tool kit when developing C# applications on OS X.

At the time of writing the latest version is 2.0.1. Getting this to run on Mono is a little tricky though, for starters it uses a call to a method in the System.TimeZoneInfo that at the time of writing hasn't been implemented in Mono 2.10:


public static DateTimeOffset ConvertTime (DateTimeOffset dateTimeOffset, TimeZoneInfo destinationTimeZone){throw new NotImplementedException();}
So we have a few options, trying to implement the missing feature is a little daunting so I took a shortcut and downloaded the previous version of Quartz.net 1.0.3, tweaked my code so that it used the old feature set and discovered that it works!

using System;
using Quartz;
using Quartz.Impl;
namespace Quartz
{
    class MainClass
{
        private static IScheduler _scheduler;
 public static void Main (string[] args)
 {
     ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); 
     _scheduler = schedulerFactory.GetScheduler();
     _scheduler.Start();
     Console.WriteLine("Starting Scheduler");
     AddJob();
 }
public static void AddJob()
 {
     JobDetail job = new JobDetail("job1", "group1", typeof(MyJob));
     Trigger trigger = TriggerUtils.MakeSecondlyTrigger("MyTrigger", 10, 10);
     DateTime ft = _scheduler.ScheduleJob(job, trigger); 
}
}
    internal class MyJob : IMyJob
{
        public void Execute(JobExecutionContext context)
 {
     Console.WriteLine("In MyJob class");
     DoMoreWork();
 }
 public void DoMoreWork()
 {
     Console.WriteLine("Do More Work");
 }
}
internal interface IMyJob : IJob
{
}
}
Here is it running: