.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 eitherconfiguration 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 DItemJobTriggerGroup ItemJob ItemJobGroup 0/3 * * * * ? ItemJob2 ItemJobGroup2 任务2 ConsoleQuartz.HelloQuartzJob2,ConsoleQuartz true false ItemJobTrigger2 DItemJobTriggerGroup2 ItemJob2 ItemJobGroup2 0/4 * * * * ?
8.按F5启动程序:
Hello, World!
任务调度器已启动
Hello HelloQuartzJob2
Hello Quartz.Net
Hello HelloQuartzJob2
Hello Quartz.Net
推荐阅读
- 基于微信小程序带后端ssm接口小区物业管理平台设计
- 事件处理程序
- 编写字典程序
- Java程序员阅读源码的小技巧,原来大牛都是这样读的,赶紧看看!
- 小程序有哪些低成本获客手段——案例解析
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- 微信小程序基础知识
- VueX(Vuex|VueX(Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式)
- 使用Promise对微信小程序wx.request请求方法进行封装
- 程序员客栈TOP收入的萌系开发者心得|程序员客栈TOP收入的萌系开发者心得 - 雨晴