天天看點

Quartz.NET--AdoJobStore作業存儲

Quartz提供兩種基本作業存儲類型。第一種類型叫做RAMJobStore,它利用通常的記憶體來持久化排程程式資訊。這種作業存儲類型最容易配置、構造和運作。Quartz.net預設使用的就是RAMJobStore。對許多應用來說,這種作業存儲已經足夠了。

然而,因為排程程式資訊是存儲在被配置設定在記憶體裡面,是以,當應用程式停止運作時,所有排程資訊将被丢失。如果你需要在重新啟動之間持久化排程資訊,則将需要第二種類型的作業存儲。為了修正這個問題,Quartz.NET 提供了 AdoJobStore。顧名思義,作業倉庫通過 ADO.NET把所有資料放在資料庫中。資料持久性的代價就是性能降低和複雜性的提高。它将所有的資料通過ADO.NET儲存到資料庫可中。它的配置要比RAMJobStore稍微複雜,同時速度也沒有那麼快。但是性能的缺陷不是非常差,尤其是如果你在資料庫表的主鍵上建立索引。

AdoJobStore

幾乎可以在任何資料庫上工作,它廣泛地使用Oracle, MySQL, MS SQLServer2000, HSQLDB, PostreSQL以及 DB2。要使用AdoJobStore,首先必須建立一套Quartz使用的資料庫表,可以在Quartz的database\tables找到建立庫表的SQL腳本。如果沒有找到你的資料庫類型的腳本,那麼找到一個已有的,修改成為你資料庫所需要的。需要注意的一件事情就是所有Quartz庫表名都以QRTZ_作為字首(例如:表"QRTZ_TRIGGERS",及"QRTZ_JOB_DETAIL")。實際上,可以你可以将字首設定為任何你想要的字首,隻要你告訴AdoJobStore那個字首是什麼即可(在你的Quartz屬性檔案中配置)。對于一個資料庫中使用多個scheduler執行個體,那麼配置不同的字首可以建立多套庫表,十分有用。

SQL Server代碼連接配接如下所示:

//AdoJobStoreRunner
            NameValueCollection properties = new NameValueCollection();
            properties["quartz.scheduler.instanceName"] = "TestScheduler";
            properties["quartz.scheduler.instanceId"] = "instance_one";
            properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
            properties["quartz.threadPool.threadCount"] = "5";
            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"] = "false";
            properties["quartz.jobStore.dataSource"] = "default";
            properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
            properties["quartz.jobStore.clustered"] = "true";
            // if running MS SQL Server we need this
            properties["quartz.jobStore.selectWithLockSQL"] = "SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = @lockName";
            properties["quartz.dataSource.default.connectionString"] = "server=local;database=quartz;user id=sa;pwd=sa;pooling=false;";
            properties["quartz.dataSource.default.provider"] = "SqlServer-20";      
ISchedulerFactory sf = new StdSchedulerFactory(properties);
            IScheduler sched = sf.GetScheduler();
            string schedId = sched.SchedulerInstanceId;
            int count = 1;
            for (int i = 0; i < 10; i++)
            {

                //作業 
                IJobDetail job = JobBuilder
                        .Create<Job1>()
                        .WithIdentity("計算作業" + i.ToString(), "組1")
                        .RequestRecovery() // ask scheduler to re-execute this job if it was in progress when the scheduler went down...
                        .Build();

                DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);

                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger" + i.ToString(), "group1")
                    .StartAt(runTime)
                    .Build();
                //關聯任務和觸發器 
                sched.ScheduleJob(job, trigger);
            
            }
            //開始任務 
            sched.Start();      
<configuration>
  <configSections>
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <quartz>
    <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler" />

    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
    <add key="quartz.threadPool.threadCount" value="10" />
    <add key="quartz.threadPool.threadPriority" value="2" />

    <add key="quartz.jobStore.misfireThreshold" value="60000" />
    <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />
  </quartz>
</configuration>