件夹“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 Nameva lueCollection();
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 Nameva lueCollection();
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.PropertyDataSourceConnectionS