Pavel Voronin
Pavel Voronin

Reputation: 13995

How many schedulers can I create per application?

It seems like StdSchedulerFactory returns a singleton with the name defined in config:

<add key="quartz.scheduler.instanceName" value="MyQuartzScheduler" />

As quartz config section consists of key value pairs it looks like using factory for instantiating scheduler limits the number of available schedulers to one.

Upvotes: 0

Views: 654

Answers (1)

Set
Set

Reputation: 49779

AFIAK, you can create as many schedulers as you like within any application, but you cannot use default quartz config approach for this, as it expect only one collection of scheduler properties (look into StdSchedulerFactory implementation and this blog if interesting):

By default, In Quartz.Net, the StdSchedulerFactory is responsible for configuring the scheduler. When the Quartz.Net scheduler is started, the factory will try to automatically configure a scheduler by looking for configuration information in different places:

  • the hosting application’s configuration file
  • a file specified in an environment variable
  • the quartz.config file
  • the embedded configuration file

SO what you can do is not to use automatic scheduler configuration, but
by himself create a separate collections of properties and pass them to scheduler creation constructors:

public StdSchedulerFactory(NameValueCollection props);
  • using code approach:

    NameValueCollection scheduler1Properties = new NameValueCollection();     
    properties["quartz.scheduler.instanceName"] = "SingleThreadScheduler"; 
    properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
    properties["quartz.threadPool.threadCount"] = "1";
    ...
    var factory = new StdSchedulerFactory(scheduler1Properties);
    
  • or you can create separate quartz configs and directly use quartz PropertiesParser class to read

    /// <summary>
    /// Reads the properties from file system.
    /// </summary>
    /// <param name="fileName">The file name to read resources from.</param>
    /// <returns></returns>
    public static PropertiesParser ReadFromFileResource(string fileName)
    

    and get collection:

    /// <summary>
    /// Gets the underlying properties.
    /// </summary>
    /// <value>The underlying properties.</value>
    public virtual NameValueCollection UnderlyingProperties
    {
        get { return props; }
    }
    

    // PropertiesParser class is directly used in default config reading implementation.

Upvotes: 2

Related Questions