Search code examples
spring.netquartz.netspring.codeconfig

Spring.Net Quartz scheduling and code config


Is there a way to configure Quartz scheduling (Spring.Scheduling.Quartz) in Spring.Net using code config rather than xml files?

I want to completely avoid using config files when setting up Quartz jobs with Spring.Net.


Solution

  • Here's a sample that uses both programmatic and XML based job scheduling glued together with CodeConfig.

    [Definition]
    public virtual IScheduler Scheduler()
    {
        var scheduler = SchedulerFactory().GetScheduler();
        var dataCleanUpJob = JobBuilder.Create<DataCleanUpJob>()
            .WithIdentity("DataCleanUpJob", "System")
            .StoreDurably(true)
            .Build();
    
        ITrigger dataCleanUpTrigger = TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInHours(1)).Build();
        scheduler.ScheduleJob(dataCleanUpJob, dataCleanUpTrigger);
    
        scheduler.StartDelayed(TimeSpan.FromSeconds(10));
        return scheduler;
    }
    
    [Definition]
    public virtual ISchedulerFactory SchedulerFactory()
    {
        var properties = new NameValueCollection();
    
        // job initialization plugin handles our xml reading, without it defaults are used
        properties["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz";
        properties["quartz.plugin.xml.fileNames"] = "~/quartz_jobs.config";
    
        var factory = new StdSchedulerFactory(properties);
        return factory;
    }
    

    Key points here are to have singleton for scheduler factory and the actual scheduler so they will be available from Spring context.