Created
January 10, 2011 11:03
-
-
Save mhenrixon/772643 to your computer and use it in GitHub Desktop.
A quick way to setup quartz with structuremap
This file contains hidden or 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
<config> | |
<section name="Scheduler"> | |
<task name="somjob" | |
group="mygroup" | |
args="" | |
status="active" | |
type="cron" | |
starttime="2008-07-23 13:52:20" | |
cronstring="0 0 4 * * ?" | |
maxretries="10" | |
retry="true" | |
delaytype="minute" | |
delay="5" /> | |
</section> | |
<quartz> | |
<item name="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" /> | |
<item name="quartz.threadPool.threadCount" value="10" /> | |
<item name="quartz.threadPool.threadPriority" value="Normal" /> | |
<item name="quartz.overwriteExistingJobs" value="true" /> | |
</quartz> | |
</config> |
This file contains hidden or 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
internal class JobRegistry :Registry | |
{ | |
public JobRegistry() | |
{ | |
ForSingletonOf<PlaynGO.Common.InversionOfControl.IContainer>().Use<PlaynGO.StructureMap.Container>(); | |
ForSingletonOf<IJobFactory>().Use<WpsJobFactory>(); | |
var col = new NameValueCollection(); | |
ForSingletonOf<ISchedulerFactory>().Use<StdSchedulerFactory>().Ctor<NameValueCollection>("props").Is(ctx => | |
{ | |
foreach (var xElement in ctx.GetInstance<ICfg>() | |
.Config.Descendants("item").Where(x => x.AttributeValue("key").Contains("quartz"))) | |
{ | |
col.Add(xElement.Value("name"), xElement.Value("value")); | |
} | |
return col; | |
}); | |
ForSingletonOf<IScheduler>().Use(ctx => | |
{ | |
var scheduler = ctx.GetInstance<ISchedulerFactory>().GetScheduler(); | |
scheduler.JobFactory = ctx.GetInstance<IJobFactory>(); | |
return scheduler; | |
}); | |
} | |
} |
This file contains hidden or 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 partial class QuartzService : ServiceBase | |
{ | |
internal static IScheduler Sched; | |
public QuartzService() | |
{ | |
InitializeComponent(); | |
} | |
/// <summary> | |
/// Service is requested to start | |
/// </summary> | |
protected override void OnStart(string[] args) | |
{ | |
try | |
{ | |
Sched = IoC.Container.Resolve<IScheduler>(); | |
Sched.Start(); | |
foreach (XElement te in IoC.Container.Resolve<ICfg>().GetSection("Scheduler").Descendants("task")) | |
{ | |
AddJob(te); | |
} | |
foreach (var groupName in Sched.TriggerGroupNames) | |
{ | |
foreach (var trigName in Sched.GetTriggerNames(groupName)) | |
{ | |
var trigger = Sched.GetTrigger(trigName, groupName); | |
var logMsg = string.Format("{0} in {1} will run at : {2}", trigName, groupName, trigger.GetNextFireTimeUtc()); | |
Console.WriteLine(logMsg); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
throw; | |
} | |
_scheduler.StartScheduling(); | |
Console.WriteLine("Scheduling has started"); | |
} | |
void AddJob(XElement task) | |
{ | |
var status = task.AttributeValue("status"); | |
if (status.ToLowerInvariant() == "active") | |
{ | |
var jobname = task.AttributeValue("name"); | |
var group = task.AttributeValue("group"); | |
JobDetail jobDetail = GetJobDetail(jobname, group, task); | |
Trigger trigger = GetTrigger(jobname, group, task); | |
Jobs.ActiveJobs[jobname] = new DefaultJob(jobDetail, trigger); | |
Sched.ScheduleJob(jobDetail, trigger); | |
Console.WriteLine(string.Format("Scheduling job : {0} in {1} to run at : {2}", jobname, group, trigger.GetNextFireTimeUtc())); | |
} | |
} | |
JobDetail GetJobDetail(string jobname, string group, XElement task) | |
{ | |
var retries = task.AttributeValue<int>("maxretries"); | |
var repeat = task.AttributeValue<int>("repeat"); | |
var delaytype = task.AttributeValue("delaytype"); | |
var delay = task.AttributeValue<int>("delay"); | |
var args = task.AttributeValue("args"); | |
var jobDetail = new JobDetail(jobname, typeof(TestPlugin)) | |
{ | |
RequestsRecovery = true, | |
}; | |
jobDetail.JobDataMap.Put("args", args.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); | |
jobDetail.JobDataMap.Put("maxretries", retries); | |
jobDetail.JobDataMap.Put("repeat", repeat); | |
jobDetail.JobDataMap.Put("delaytype", delaytype); | |
jobDetail.JobDataMap.Put("delay", delay); | |
jobDetail.Group = group; | |
return jobDetail; | |
} | |
Trigger GetTrigger(string jobname, string group, XElement te) | |
{ | |
string type = te.AttributeValue("type").ToLowerInvariant(); | |
var interval = te.AttributeValue<int>("interval"); | |
var cronstring = te.AttributeValue("cronstring"); | |
var starttime = te.AttributeValue<DateTime>("starttime"); | |
int hour = 0; | |
int day = 0; | |
int minute = 0; | |
Trigger trigger = null; | |
switch (type) | |
{ | |
case "second": | |
trigger = TriggerUtils.MakeSecondlyTrigger(interval); | |
break; | |
case "minute": | |
trigger = TriggerUtils.MakeMinutelyTrigger(interval); | |
break; | |
case "hour": | |
trigger = TriggerUtils.MakeHourlyTrigger(interval); | |
break; | |
case "day": { | |
hour = te.AttributeValue<int>("hour"); | |
minute = te.AttributeValue<int>("minute"); | |
trigger = TriggerUtils.MakeDailyTrigger(hour, minute); | |
} | |
break; | |
case "week": | |
hour = te.AttributeValue<int>("hour"); | |
minute = te.AttributeValue<int>("minute"); | |
day = te.AttributeValue<int>("day"); | |
trigger = TriggerUtils.MakeWeeklyTrigger((DayOfWeek)day, hour, minute); | |
break; | |
case "cron": | |
if (!CronExpression.IsValidExpression(cronstring)) | |
{ | |
throw new SchedulerException(string.Format("The cronstring {0} is no valid", cronstring)); | |
} | |
var cron = new CronTrigger | |
{ | |
CronExpressionString = cronstring, | |
CronExpression = new CronExpression(cronstring), | |
}; | |
trigger = cron; | |
break; | |
default: | |
throw new NotImplementedException(type + " is not implemented"); | |
} | |
trigger.Name = jobname; | |
trigger.JobName = jobname; | |
trigger.Group = group; | |
trigger.JobGroup = group; | |
trigger.StartTimeUtc = type == "cron" ? DateTime.UtcNow : starttime; | |
trigger.Validate(); | |
return trigger; | |
} | |
/// <summary> | |
/// Service is requested to stop | |
/// </summary> | |
protected override void OnStop() | |
{ | |
Console.WriteLine("Closing service"); | |
_scheduler.StopScheduling(); | |
Sched.Shutdown(true); | |
Console.WriteLine("All shutdown"); | |
} | |
} |
It was something to do with C# but that's about as far as I can remember. Quartz is a library for dealing with cron jobs. I haven't done C# development in 10 years so...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is ForSingletonOf ?
What is IoC: IoC.Container.Resolve<IScheduler ?