咱們都知道默認的Quartz底層採用的是RAMJobStore,全部的Job,Trigger,Calendar都是用Dictionary,SortSet等等這樣的數據結構進行儲存,相對來講性linux
能確定快的無法說,可是面對災難重啓的時候仍是很拿不出手的,並且都是全內存的,也無法實現多機器搭建Quartz集羣,這一點仍是很討厭,雖然官方已經git
提供了一些關係性持久化存儲方案,但面對現在這麼火的nosql,不進行官方支持仍是有點惋惜,不過基於Quartz自己的插拔式設計,一切都不是問題。github
一:IJobStoreredis
從github上下載源碼:https://github.com/quartznet/quartznet,從源碼你會發現IJobStore幾乎實現了全部對Trigger,Job和Scheduler全部的容器管理操做。sql
而後你能夠看到它的幾個實現子類,全內存的RAMJobStore。mongodb
public class RAMJobStore: IJobStore { .... }
以及JobStoreSupport下的帶鎖JobStoreTX和不帶鎖的JobStoreCMT。數據庫
public class JobStoreSupport: IJobStore { .... } //帶鎖機制 public class JobStoreTX: JobStoreSupport { .... } //不帶鎖 public class JobStoreCMT: JobStoreSupport { .... }
因此你應該明白,本節課跟你們講到的Redis和Mongodb的JobStore存儲,必然也是實現了IJobStore接口,對吧。centos
二:MongoDB的JobStore數據結構
1. 安裝mongodbnosql
既然要使用mongodb,你必然要有mongodb的安裝程序,能夠去官網: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.5.tgz 一下,
這裏我採用linux平臺的centos。
2. nuget上pull些dll
你們能夠在nuget控制檯執行Install-Package Quartz.Spi.MongoDbJobStore,以下所示:
PM> Install-Package Quartz.Spi.MongoDbJobStore 正在嘗試收集與目標爲「.NETFramework,Version=v4.5.2」的項目「ConsoleApplication1」有關的包「Quartz.Spi.MongoDbJobStore.2.0.0」的依賴項信息 正在嘗試解析程序包「Quartz.Spi.MongoDbJobStore.2.0.0」的依賴項,DependencyBehavior 爲「Lowest」 正在解析操做以安裝程序包「Quartz.Spi.MongoDbJobStore.2.0.0」 已解析操做以安裝程序包「Quartz.Spi.MongoDbJobStore.2.0.0」 正在將程序包「Common.Logging.Core.3.3.1」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「Common.Logging.Core.3.3.1」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「Common.Logging.Core.3.3.1」添加到「packages.config」 已將「Common.Logging.Core 3.3.1」成功安裝到 ConsoleApplication1 正在將程序包「Common.Logging.3.3.1」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「Common.Logging.3.3.1」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「Common.Logging.3.3.1」添加到「packages.config」 已將「Common.Logging 3.3.1」成功安裝到 ConsoleApplication1 正在將程序包「MongoDB.Bson.2.4.2」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「MongoDB.Bson.2.4.2」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「MongoDB.Bson.2.4.2」添加到「packages.config」 已將「MongoDB.Bson 2.4.2」成功安裝到 ConsoleApplication1 正在將程序包「Quartz.2.4.1」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「Quartz.2.4.1」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「Quartz.2.4.1」添加到「packages.config」 已將「Quartz 2.4.1」成功安裝到 ConsoleApplication1 正在將程序包「System.Runtime.InteropServices.RuntimeInformation.4.3.0」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「System.Runtime.InteropServices.RuntimeInformation.4.3.0」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「System.Runtime.InteropServices.RuntimeInformation.4.3.0」添加到「packages.config」 已將「System.Runtime.InteropServices.RuntimeInformation 4.3.0」成功安裝到 ConsoleApplication1 正在將程序包「MongoDB.Driver.Core.2.4.2」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「MongoDB.Driver.Core.2.4.2」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「MongoDB.Driver.Core.2.4.2」添加到「packages.config」 已將「MongoDB.Driver.Core 2.4.2」成功安裝到 ConsoleApplication1 正在將程序包「MongoDB.Driver.2.4.2」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「MongoDB.Driver.2.4.2」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「MongoDB.Driver.2.4.2」添加到「packages.config」 已將「MongoDB.Driver 2.4.2」成功安裝到 ConsoleApplication1 正在將程序包「Quartz.Spi.MongoDbJobStore.2.0.0」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「Quartz.Spi.MongoDbJobStore.2.0.0」添加到文件夾「C:\1\ConsoleApplication1\packages」 已將程序包「Quartz.Spi.MongoDbJobStore.2.0.0」添加到「packages.config」 已將「Quartz.Spi.MongoDbJobStore 2.0.0」成功安裝到 ConsoleApplication1
也能夠到github中下載源碼:https://github.com/chrisdrobison/mongodb-quartz-net
3. 啓動運行
而後能夠看一下此頁面上的Basic Usage##上的默認配置:
1 var properties = new NameValueCollection(); 2 properties[StdSchedulerFactory.PropertySchedulerInstanceName] = instanceName; 3 properties[StdSchedulerFactory.PropertySchedulerInstanceId] = $"{Environment.MachineName}-{Guid.NewGuid()}"; 4 properties[StdSchedulerFactory.PropertyJobStoreType] = typeof (MongoDbJobStore).AssemblyQualifiedName; 5 // I treat the database in the connection string as the one you want to connect to 6 properties[$"{StdSchedulerFactory.PropertyJobStorePrefix}.{StdSchedulerFactory.PropertyDataSourceConnectionString}"] = "mongodb://localhost/quartz"; 7 // The prefix is optional 8 properties[$"{StdSchedulerFactory.PropertyJobStorePrefix}.collectionPrefix"] = "prefix"; 9 10 var scheduler = new StdSchedulerFactory(properties); 11 return scheduler.GetScheduler();
<1> PropertySchedulerInstanceName: 就是對Scheduler的Name進行的配置,你們能夠根據狀況定義一個簡明釋義的名字。
<2> PropertySchedulerInstanceId: 能夠看到這個項採用的是machineName+NewGuid來保證Scheduler容器的SchedulerID惟一,惟一性特別重要,由於在
Cluster 中就是用它來保證惟一性的,不過上面的代碼有點累贅,其實只要寫上「AUTO」就能夠了,由底層的
SimpleInstanceIdGenerator來保證uniqueID的生成,如StdSchedulerFactory.Instantiate方法源碼所示:
1 if (schedInstId.Equals(AutoGenerateInstanceId)) 2 { 3 autoId = true; 4 instanceIdGeneratorType = LoadType(cfg.GetStringProperty(PropertySchedulerInstanceIdGeneratorType)) ?? typeof(SimpleInstanceIdGenerator); 5 } 6 else if (schedInstId.Equals(SystemPropertyAsInstanceId)) 7 { 8 autoId = true; 9 instanceIdGeneratorType = typeof(SystemPropertyInstanceIdGenerator); 10 }
<3> PropertyJobStoreType:這個屬性將MongoDbJobStore做爲底層的IJobStore實現者。
<4> PropertyDataSourceConnectionString,collectionPrefix: 這兩個沒什麼好說的,一個是mongodb的connectionstring,一個是collection的前綴。
好了,下面就是個人完整代碼:
1 static void Main(string[] args) 2 { 3 4 LogManager.Adapter = new Common.Logging.Simple.TraceLoggerFactoryAdapter() 5 { 6 Level = LogLevel.All 7 }; 8 9 var properties = new NameValueCollection(); 10 properties[StdSchedulerFactory.PropertySchedulerInstanceId] = "AUTO"; 11 properties[StdSchedulerFactory.PropertyJobStoreType] = typeof(MongoDbJobStore).AssemblyQualifiedName; 12 13 // I treat the database in the connection string as the one you want to connect to 14 properties[$"{StdSchedulerFactory.PropertyJobStorePrefix}.{StdSchedulerFactory.PropertyDataSourceConnectionString}"] = "mongodb://192.168.23.163/quartz"; 15 16 // The prefix is optional 17 properties[$"{StdSchedulerFactory.PropertyJobStorePrefix}.collectionPrefix"] = "prefix"; 18 19 var factory = new StdSchedulerFactory(properties); 20 21 //scheduler 22 IScheduler scheduler = factory.GetScheduler(); 23 24 scheduler.Start(); 25 26 var job = JobBuilder.Create<HelloJob>().WithIdentity("test", "datamip").Build(); 27 28 var trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").Build(); 29 30 if (!scheduler.CheckExists(job.Key)) 31 { 32 scheduler.ScheduleJob(job, trigger); 33 } 34 35 Console.Read(); 36 }
這個我自定義的HelloJob中,我特地記錄一下scheduler的調度時間schedulertime和Trigger應該觸發的時間nextFireTime。
1 class HelloJob : IJob 2 { 3 static int index = 1; 4 5 public void Execute(IJobExecutionContext context) 6 { 7 Console.WriteLine("{4} index={0},current={1}, scheuler={2},nexttime={3}", 8 index++, DateTime.Now, 9 context.ScheduledFireTimeUtc?.LocalDateTime, 10 context.NextFireTimeUtc?.LocalDateTime, 11 context.JobDetail.JobDataMap["key"]); 12 } 13 }
接下來執行一下:
而後經過robomongo到數據庫看一下,有5個collection,裏面都有數據,沒毛病。
好了,本篇就說到這裏了,固然還有基於redis的JobStore,有興趣你們能夠本身嘗試一下。