quartz文档:https://www.quartz-scheduler.net/documentation/index.html
这里用新建的控制台项目进行演示。
目标效果为每隔一秒在控制台上输出一句
Greetings from HelloJob!
using System;using System.Collections.Specialized;using System.Threading.Tasks;using Quartz;using Quartz.Impl;using Quartz.Logging;namespace ConsoleApp1{ ???????public class Program ???{ ???????private static void Main(string[] args) ???????{ ???????????LogProvider.SetCurrentLogProvider(new ConsoleLogProvider()); ???????????????RunProgramRunExample().GetAwaiter().GetResult(); ???????????Console.WriteLine("Press any key to close the application"); ???????????Console.ReadKey(); ???????} ???????private static async Task RunProgramRunExample() ???????{ ???????????try ???????????{ ???????????????// Grab the Scheduler instance from the Factory ???????????????NameValueCollection props = new NameValueCollection ???????????????{ ???????????????????{ "quartz.serializer.type", "binary" } ???????????????}; ???????????????StdSchedulerFactory factory = new StdSchedulerFactory(props); ???????????????IScheduler scheduler = await factory.GetScheduler(); ???????????????// and start it off ???????????????await scheduler.Start(); ???????????????// define the job and tie it to our HelloJob class ???????????????IJobDetail job = JobBuilder.Create<HelloJob>() ???????????????????.WithIdentity("job1", "group1") ???????????????????.Build(); ???????????????// Trigger the job to run now, and then repeat every 10 seconds ???????????????ITrigger trigger = TriggerBuilder.Create() ???????????????????.WithIdentity("trigger1", "group1") ???????????????????.StartNow() ???????????????????.WithSimpleSchedule(x => x ???????????????????????.WithIntervalInSeconds(1) ???????????//在这里配置执行延时 ???????????????????????.RepeatForever()) ???????????????????.Build(); ???????????????// Tell quartz to schedule the job using our trigger ???????????????await scheduler.ScheduleJob(job, trigger); ???????????????// some sleep to show what‘s happening// ???????????????await Task.Delay(TimeSpan.FromSeconds(5));// and last shut down the scheduler when you are ready to close your program// ???????????????await scheduler.Shutdown(); ??????????
???
//如果解除await Task.Delay(TimeSpan.FromSeconds(5))和await scheduler.Shutdown()的注释,
//5秒后输出"Press any key to close the application",
//scheduler里注册的任务也会停止。
???????????} ???????????catch (SchedulerException se) ???????????{ ???????????????Console.WriteLine(se); ???????????} ???????} ???????// simple log provider to get something to the console ???????private class ConsoleLogProvider : ILogProvider ???????{ ???????????public Logger GetLogger(string name) ???????????{ ???????????????return (level, func, exception, parameters) => ???????????????{ ???????????????????if (level >= LogLevel.Info && func != null) ???????????????????{ ???????????????????????Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters); ???????????????????} ???????????????????return true; ???????????????}; ???????????} ???????????public IDisposable OpenNestedContext(string message) ???????????{ ???????????????throw new NotImplementedException(); ???????????} ???????????public IDisposable OpenMappedContext(string key, string value) ???????????{ ???????????????throw new NotImplementedException(); ???????????} ???????} ???} ???public class HelloJob : IJob ???{ ???????public async Task Execute(IJobExecutionContext context) ???????{
//任务主体,这里强制要求必须是异步方法,如果不想用异步可以使用quartz 2.x版本 ???????????await Console.Out.WriteLineAsync("Greetings from HelloJob!"); ???????} ???}}
quartz.net 3.x 使用总结
原文地址:https://www.cnblogs.com/axel10/p/8436077.html