Skip to content

Instantly share code, notes, and snippets.

@codereflection
Created May 27, 2011 21:54
Show Gist options
  • Save codereflection/996263 to your computer and use it in GitHub Desktop.
Save codereflection/996263 to your computer and use it in GitHub Desktop.
Custom Quartz Facility for Castle Windsor to dynamically set the quartz_jobs.xml
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
</configSections>
<appSettings>
<!-- MACHINE KEYS -->
<!-- Dev Environment -->
<add key="DevMachineName" value="Dev"/>
<!-- Test Environment -->
<add key="TestServerName" value="Test"/>
<!-- Uat Environment -->
<add key="UatServerName" value="Uat"/>
<!-- Prod Environment -->
<add key="ProdServerName" value="Prod"/>
<!-- APP SETTINGS -->
<!-- Dev Environment -->
<add key="Dev.QuartzXmlFile" value="quartz_jobs_dev_test.xml"/>
<!-- Test Environment -->
<add key="Test.QuartzXmlFile" value="quartz_jobs_dev_test.xml"/>
<!-- Uat Environment -->
<add key="Uat.QuartzXmlFile" value="quartz_jobs_uat_prod.xml"/>
<!-- Prod Environment -->
<add key="Prod.QuartzXmlFile" value="quartz_jobs_uat_prod.xml"/>
</appSettings>
<castle>
<facilities>
<facility id="quartznet">
<quartz>
<item key="quartz.scheduler.instanceName">XmlConfiguredInstance</item>
<item key="quartz.threadPool.type">Quartz.Simpl.SimpleThreadPool, Quartz</item>
<item key="quartz.threadPool.threadCount">10</item>
<item key="quartz.threadPool.threadPriority">Normal</item>
<item key="quartz.plugin.xml.type">Quartz.Plugin.Xml.JobInitializationPlugin, Quartz</item>
<item key="quartz.plugin.xml.overwriteExistingJobs">true</item>
<item key="quartz.plugin.xml.scanInterval">3600</item>
</quartz>
</facility>
</facilities>
<components>
<component id="myQuartzJob" type="FuBard.myQuartzJob, FuBard"/>
</components>
</castle>
</configuration>
using System;
using System.Linq;
using Castle.Core.Configuration;
using Castle.Facilities.QuartzIntegration;
using Castle.MicroKernel.Facilities;
using Castle.MicroKernel.Registration;
using Quartz;
using Quartz.Job;
using Quartz.Spi;
namespace Ris.Conduit.Service
{
public class ConduitQuartzFacility : AbstractFacility
{
public virtual void BuildQuartzXmlFileNames(MutableConfiguration config)
{
var env = Kernel.Resolve<IApplicationEnvironment>();
Func<IConfiguration, bool> withFileNameSettings = x => x.Name == "item" && x.Attributes["key"] == "quartz.plugin.xml.fileNames";
if (config.Children.Any(withFileNameSettings))
(config.Children.First(withFileNameSettings) as MutableConfiguration).Value = env.QuartzXmlFile;
else
config.CreateChild("item", env.QuartzXmlFile).Attribute("key", "quartz.plugin.xml.fileNames");
}
protected override void Init()
{
Kernel.ConfigurationStore.AddComponentConfiguration(typeof(QuartzNetScheduler).AssemblyQualifiedName, BuildConfig(FacilityConfig));
AddComponent<FileScanJob>();
AddComponent<IJobScheduler, QuartzNetSimpleScheduler>();
AddComponent<IJobFactory, WindsorJobFactory>();
AddComponent<IScheduler, QuartzNetScheduler>();
}
internal IConfiguration BuildConfig(IConfiguration config)
{
if (config == null)
throw new FacilityException("Please define the configuration for Quartz.Net facility");
var quartzNet = config.Children["quartz"];
if (quartzNet == null)
throw new FacilityException("Please define the Quartz.Net properties");
var componentConfig = new MutableConfiguration(typeof(QuartzNetScheduler).AssemblyQualifiedName);
var parameters = componentConfig.CreateChild("parameters");
BuildProps(quartzNet, parameters.CreateChild("props"));
var globalJobListeners = config.Children["globalJobListeners"];
if (globalJobListeners != null)
BuildServiceArray<IJobListener>(globalJobListeners, parameters.CreateChild("SetGlobalJobListeners"));
var globalTriggerListeners = config.Children["globalTriggerListeners"];
if (globalTriggerListeners != null)
BuildServiceArray<ITriggerListener>(globalTriggerListeners, parameters.CreateChild("SetGlobalTriggerListeners"));
var jobListeners = config.Children["jobListeners"];
if (jobListeners != null)
BuildServiceDictionary<string, IJobListener[]>(jobListeners, parameters.CreateChild("jobListeners"));
var triggerListeners = config.Children["triggerListeners"];
if (triggerListeners != null)
BuildServiceDictionary<string, ITriggerListener[]>(triggerListeners, parameters.CreateChild("triggerListeners"));
var schedulerListeners = config.Children["schedulerListeners"];
if (schedulerListeners != null)
BuildServiceArray<ISchedulerListener>(schedulerListeners, parameters.CreateChild("SetSchedulerListeners"));
return componentConfig;
}
internal void BuildServiceDictionary<K, V>(IConfiguration config, MutableConfiguration parameters)
{
var dict = parameters.CreateChild("dictionary");
dict.Attribute("keyType", typeof(K).AssemblyQualifiedName);
dict.Attribute("valueType", typeof(V).AssemblyQualifiedName);
foreach (IConfiguration c in config.Children)
{
var job = dict.CreateChild("entry")
.Attribute("key", c.Attributes["name"]);
BuildServiceArray<V>(c, job);
}
}
internal void BuildServiceList<T>(IConfiguration config, MutableConfiguration parameters)
{
var array = parameters.CreateChild("list");
array.Attribute("type", typeof(T).AssemblyQualifiedName);
foreach (IConfiguration c in config.Children)
{
array.CreateChild("item", c.Value);
}
}
internal void BuildServiceArray<T>(IConfiguration config, MutableConfiguration parameters)
{
var array = parameters.CreateChild("array");
array.Attribute("type", typeof(T).AssemblyQualifiedName);
foreach (IConfiguration c in config.Children)
{
array.CreateChild("item", c.Value);
}
}
internal void BuildProps(IConfiguration config, MutableConfiguration props)
{
var dict = props.CreateChild("dictionary");
foreach (IConfiguration c in config.Children)
{
dict.CreateChild("item", c.Value)
.Attribute("key", c.Attributes["key"]);
}
BuildQuartzXmlFileNames(dict);
}
internal string AddComponent<T>()
{
string key = typeof(T).AssemblyQualifiedName;
Kernel.Register(Component.For(typeof(T)).Named(key));
return key;
}
internal string AddComponent<I, T>() where T : I
{
string key = typeof(T).AssemblyQualifiedName;
Kernel.Register(Component.For(typeof(I)).ImplementedBy(typeof(T)).Named(key));
return key;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment