.NET6控制台程序使用quartz.net

1.新建一个名为“ConsoleQuartz”的.NET6控制台程序。
【.NET6控制台程序使用quartz.net】2.nuget中安装Quartz和Quartz.Plugins,这2个DLL。
3.新建一个HelloQuartzJob类:

using Quartz; namespace ConsoleQuartz { public class HelloQuartzJob : IJob { public Task Execute(IJobExecutionContext context) { return Task.Factory.StartNew(() => { Console.WriteLine("Hello Quartz.Net"); }); } } }

4.新建一个HelloQuartzJob2:
using Quartz; namespace ConsoleQuartz { public class HelloQuartzJob2 : IJob { public Task Execute(IJobExecutionContext context) { return Task.Factory.StartNew(() => { Console.WriteLine("Hello HelloQuartzJob2"); }); } } }

5.修改Program.cs:
// See https://aka.ms/new-console-template for more information using Quartz; using Quartz.Impl; Console.WriteLine("Hello, World!"); await MainAsync(); Console.ReadKey(); static async Task MainAsync() { IScheduler Scheduler=await StdSchedulerFactory.GetDefaultScheduler(); await Scheduler.Start(); Console.WriteLine("任务调度器已启动"); }

6.在编译输出目录(bin\Debug\net6.0)下新建一个quartz.config,内容如下:
# You can configure your scheduler in either configuration section # or in quartz properties file # Configuration section has precedencequartz.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.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins 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 = 5552 quartz.scheduler.exporter.bindName = QuartzScheduler quartz.scheduler.exporter.channelType = tcp quartz.scheduler.exporter.channelName = httpQuartz

注意quartz.plugin.xml.type,使用的是Quartz.Plugins,老版本是Quartz,如果写成
Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
会报错:
Quartz.SchedulerException:“SchedulerPlugin of type 'Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz' could not be instantiated.”
TypeLoadException: Could not load type 'Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin' from assembly 'Quartz'.

7.在编译输出目录(bin\Debug\net6.0)下新建一个quartz_jobs.xml,内容如下:

true ItemJob ItemJobGroup 任务ConsoleQuartz.HelloQuartzJob,ConsoleQuartz true false ItemJobTrigger DItemJobTriggerGroupItemJobItemJobGroup 0/3 * * * * ? ItemJob2 ItemJobGroup2 任务2ConsoleQuartz.HelloQuartzJob2,ConsoleQuartz true false ItemJobTrigger2 DItemJobTriggerGroup2 ItemJob2 ItemJobGroup2 0/4 * * * * ?



8.按F5启动程序:
Hello, World!
任务调度器已启动
Hello HelloQuartzJob2
Hello Quartz.Net
Hello HelloQuartzJob2
Hello Quartz.Net

    推荐阅读