c#版本QUARTZ DEMO示例,其自带接口方法已足够全面。
注意
任务调度器:Scheduler,来自于Scheduler工厂,其会自动加载持久层的任务数据并等待启动。注意任务调度器关闭后不能重启,所以任务调度器应在进程关闭时调用。
任务:job,任务可以有多个触发器在,也就是任务可以有多个出发时间点。job停止会停止多触发器下的所有该job。
触发器:Trigger,用来设置时间,作用在于到达时间点时执行对应的job。触发器停止只停止该触发器去出发某一job,其他触发器仍可以触发该job。
配置方式
一般使用代码中直接配置。如下:
连接MYSQL配置方式,需要mysql驱动dll和Quartz.Serialization.Json.dll和Newtonsoft.Json.dll至同一文件夹
properties = new NameValueCollection
{
["quartz.scheduler.instanceName"] = "TestScheduler",
["quartz.scheduler.instanceId"] = "instance_one",
["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz",
["quartz.threadPool.threadCount"] = "5",
["quartz.jobStore.misfireThreshold"] = "60000",
["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
["quartz.jobStore.useProperties"] = "false",
["quartz.jobStore.dataSource"] = "default",
["quartz.jobStore.tablePrefix"] = "QRTZ_",
["quartz.jobStore.clustered"] = "true",
["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.MySQLDelegate, Quartz",
["quartz.dataSource.default.connectionString"] = "server=192.168.1.50;port=3306;user=root;password=handhand; database=quartznet;" + "SslMode=" + MySqlSslMode.None + ";",
["quartz.dataSource.default.provider"] = "MySql",
["quartz.serializer.type"] = "json"
};
连接sqlite方式,该方式不可启用集群,需要sqlite驱动dll和Quartz.Serialization.Json.dll和Newtonsoft.Json.dll至同一文件夹
string dataSourceFile = System.Environment.CurrentDirectory + "//quartz_demo.db";
properties = new NameValueCollection
{
["quartz.scheduler.instanceName"] = "TestScheduler",
["quartz.scheduler.instanceId"] = "instance_one",
["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz",
["quartz.threadPool.threadCount"] = "5",
["quartz.jobStore.misfireThreshold"] = "60000",
["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
["quartz.jobStore.useProperties"] = "true",
["quartz.jobStore.dataSource"] = "default",
["quartz.jobStore.tablePrefix"] = "QRTZ_",
["quartz.jobStore.clustered"] = "false",
["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SQLiteDelegate, Quartz",
["quartz.dataSource.default.connectionString"] = "Data Source=" + dataSourceFile + ";Pooling=true;FailIfMissing=false;Version=3;UTF8Encoding=True;Journal Mode=DELETE;",
["quartz.dataSource.default.provider"] = "SQLite",
["quartz.serializer.type"] = "json"
};
无数据库配置文件启动方式,需要引用Quartz.Jobs.dll和Quartz.Plugins.dll
properties = new NameValueCollection
{
["quartz.plugin.triggHistory.type"] = "Quartz.Plugin.History.LoggingJobHistoryPlugin, Quartz.Plugins",
["quartz.plugin.jobInitializer.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins",
["quartz.plugin.jobInitializer.fileNames"] = "quartz_jobs.xml",
["quartz.plugin.jobInitializer.failOnFileNotFound"] = "true",
["quartz.plugin.jobInitializer.scanInterval"] = "120"
};
建立任务调度器工厂
factory = new StdSchedulerFactory(properties);
获取任务调度器
scheduler = await factory.GetScheduler().ConfigureAwait(false);
建立任务
JobKey jKey = new JobKey("job1", "group1");
TriggerKey tKey = new TriggerKey("trigger1", "group1");
IJobDetail job = JobBuilder.Create<TestJob>()
.WithIdentity(jKey)
.Build();
建立触发器
ITrigger trigger = TriggerBuilder.Create()
.StartNow()
.WithIdentity(tKey)
.WithCronSchedule("* * * * * ? *")
.ForJob(job)
.Build();
任务附加到调度器
await scheduler.AddJob(job, true, true).ConfigureAwait(false);
触发器附加到调度器
if (!await scheduler.CheckExists(tKey).ConfigureAwait(false))
{
await scheduler.ScheduleJob(trigger).ConfigureAwait(false);
log.Info("Trigger Is Exited.");
return;
}
启动调度器
Task l = scheduler.Start();
l.Wait();
重启调度器
Task l = scheduler.ResumeAll();
l.Wait();
暂停调度器
Task l = scheduler.PauseAll();
l.Wait();
调度器彻底关闭
Task l = scheduler.Shutdown();
l.Wait();
暂停触发器
Task l = scheduler.PauseTrigger(new TriggerKey("trigger1", "group1"));
l.Wait();
重启触发器
Task l = scheduler.ResumeTrigger(new TriggerKey("trigger1", "group1"));
l.Wait();
暂停任务
Task l = scheduler.PauseJob(new JobKey("job1", "group1"));
l.Wait();
重启任务
Task l = scheduler.ResumeJob(new JobKey("job1", "group1"));
l.Wait();
删除任务
Task l = scheduler.DeleteJob(new JobKey("job1", "group1"));
l.Wait();