Quartz.Net 学习随手记之03 配置文件

第一种方式:直接写入代码中

复制代码
            NameValueCollection properties = new NameValueCollection();

            properties["quartz.scheduler.instanceName"] = "ConsoleScheduler";
            properties["quartz.scheduler.instanceId"] = "instance_one";
            properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
            properties["quartz.threadPool.threadCount"] = "10";
            properties["quartz.threadPool.threadPriority"] = "Normal";
            properties["quartz.jobStore.misfireThreshold"] = "60000";
            properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
            properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz";
            properties["quartz.jobStore.useProperties"] = "true";
            properties["quartz.jobStore.dataSource"] = "default";
            properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
            properties["quartz.dataSource.default.connectionString"] = "Server=xx;Database=xx;User Id=xx;Password=xx";
            properties["quartz.dataSource.default.provider"] = "SqlServer-20";

            ISchedulerFactory sf = new StdSchedulerFactory(properties);
            IScheduler sched = sf.GetScheduler();
复制代码

第二种方式:写入app.config或web.config中

复制代码
  <quartz>
    <add key="quartz.scheduler.instanceName" value="ConsoleScheduler"/>
    <add key="quartz.scheduler.instanceId" value="instance_one"/>
    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
    <add key="quartz.threadPool.threadCount" value="10"/>
    <add key="quartz.threadPool.threadPriority" value="Normal"/>
    <add key="quartz.jobStore.misfireThreshold" value="60000"/>
    <add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"/>
    <add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz"/>
    <add key="quartz.jobStore.useProperties" value="true"/>
    <add key="quartz.jobStore.dataSource" value="default"/>
    <add key="quartz.jobStore.tablePrefix" value="QRTZ_"/>
    <add key="quartz.dataSource.default.connectionString" value="Server=xx;Database=xx;User Id=xx;Password=xx"/>
    <add key="quartz.dataSource.default.provider" value="SqlServer-20"/>
  </quartz>
复制代码

第三种方式:写入quartz.config文件中

复制代码
# You can configure your scheduler in either <quartz>
  configuration section
  # or in quartz properties file
  # Configuration section has precedence

  quartz.scheduler.instanceName = ServerScheduler

  # configure thread pool info
  quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
  quartz.threadPool.threadCount = 10
  quartz.threadPool.threadPriority = Normal

  # job initialization plugin handles our xml reading, without it defaults are used
  quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
  quartz.plugin.xml.fileNames = ~/quartz_jobs.xml

  # export this server to remoting context
  quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
  quartz.scheduler.exporter.port = 555
  quartz.scheduler.exporter.bindName = QuartzScheduler
  quartz.scheduler.exporter.channelType = tcp
  quartz.scheduler.exporter.channelName = httpQuartz

  # job store
  quartz.jobStore.misfireThreshold =60000
  quartz.jobStore.type = Quartz.Impl.AdoJobStore.JobStoreTX, Quartz
  quartz.jobStore.driverDelegateType = Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz
  quartz.jobStore.useProperties = true
  quartz.jobStore.dataSource = default
  quartz.jobStore.tablePrefix = QRTZ_
  quartz.dataSource.default.connectionString = Server=xx;Database=xx;User Id=xx;Password=xx
  quartz.dataSource.default.provider = SqlServer-20
复制代码

第二和第三中方式调用方式如下

ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler();

 为什么第一种方式可以,我们Debug源码可以发现如下

复制代码
public StdSchedulerFactory(NameValueCollection props)
{
    Initialize(props);
}

public virtual void Initialize(NameValueCollection props)
{
    cfg = new PropertiesParser(props);
    ValidateConfiguration();
}
复制代码

后两者方式原因如下

复制代码
public virtual IScheduler GetScheduler()
{
    if (cfg == null)
    {
        Initialize();
    }

    SchedulerRepository schedRep = SchedulerRepository.Instance;

    IScheduler sched = schedRep.Lookup(SchedulerName);

    if (sched != null)
    {
        if (sched.IsShutdown)
        {
            schedRep.Remove(SchedulerName);
        }
        else
        {
            return sched;
        }
    }

    sched = Instantiate();

    return sched;
}

public void Initialize()
{
    // short-circuit if already initialized
    if (cfg != null)
    {
        return;
    }
    if (initException != null)
    {
        throw initException;
    }

    NameValueCollection props = (NameValueCollection) ConfigurationManager.GetSection("quartz");

    string requestedFile = QuartzEnvironment.GetEnvironmentVariable(PropertiesFile);

    string propFileName = requestedFile != null && requestedFile.Trim().Length > 0 ? requestedFile : "~/quartz.config";

    // check for specials
    try
    {
        propFileName = FileUtil.ResolveFile(propFileName);
    }
未完
复制代码

注意代码NameValueCollection props = (NameValueCollection) ConfigurationManager.GetSection("quartz"); (寻找app.config或web.config)

和string requestedFile = QuartzEnvironment.GetEnvironmentVariable(PropertiesFile);(寻找quartz.config)

string propFileName = requestedFile != null && requestedFile.Trim().Length > 0 ? requestedFile : "~/quartz.config";

http://www.cnblogs.com/panchunting/archive/2013/04/12/3016899.html

原文地址:https://www.cnblogs.com/tianciliangen/p/8378324.html