Quartz.NET 的使用
先貼使用代碼:
1 using Quartz; 2 using Quartz.Impl; 3 using Quartz.Logging; 4 using System; 5 using System.Collections.Specialized; 6 using System.Threading.Tasks; 7 8 namespace QuartzTest 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 GetTask().GetAwaiter().GetResult(); 15 } 16 public static async Task GetTask() 17 { 18 // construct a scheduler factory 19 NameValueCollection props = new NameValueCollection 20 { 21 { "quartz.serializer.type", "binary" } 22 }; 23 ISchedulerFactory factory = new StdSchedulerFactory(props); 24 IScheduler scheduler =await factory.GetScheduler(); 25 26 IJobDetail job = JobBuilder.Create<HelloJob>() 27 .WithIdentity("job1", "group1") 28 .Build(); 29 30 ITrigger trigger = TriggerBuilder.Create() 31 .WithIdentity("trigger1", "group1") 32 .WithCronSchedule("0/5 * * * * ?") 33 .Build(); 34 35 await scheduler.ScheduleJob(job, trigger); 36 await scheduler.Start(); 37 38 // some sleep to show what's happening 39 await Task.Delay(TimeSpan.FromSeconds(60)); 40 41 // and last shut down the scheduler when you are ready to close your program 42 await scheduler.Shutdown(); 43 } 44 } 45 46 public class HelloJob : IJob 47 { 48 public async Task Execute(IJobExecutionContext context) 49 { 50 await Console.Out.WriteLineAsync("Hello Quartz.Net..." + DateTime.Now + Environment.NewLine); 51 } 52 } 53 54 55 56 //class Program 57 //{ 58 // static void Main(string[] args) 59 // { 60 // GetTask().GetAwaiter().GetResult(); 61 // } 62 63 // public static async Task GetTask() 64 // { 65 // // construct a scheduler factory 66 // NameValueCollection props = new NameValueCollection 67 // { 68 // { "quartz.serializer.type", "binary" } 69 // }; 70 // StdSchedulerFactory factory = new StdSchedulerFactory(props); 71 72 // // get a scheduler 73 // IScheduler scheduler =await factory.GetScheduler(); 74 // await scheduler.Start(); 75 76 // // define the job and tie it to our HelloJob class 77 // IJobDetail job = JobBuilder.Create<HelloJob>() 78 // .WithIdentity("myJob", "group1") 79 // .Build(); 80 81 // // Trigger the job to run now, and then every 40 seconds 82 // ITrigger trigger = TriggerBuilder.Create() 83 // .WithIdentity("myTrigger", "group1") 84 // .StartNow() 85 // .WithSimpleSchedule(x => x 86 // .WithIntervalInSeconds(10) 87 // .RepeatForever()) 88 // .Build(); 89 90 // // Tell Quartz.Net to schedule the job using our trigger 91 // await scheduler.ScheduleJob(job, trigger); 92 93 // // some sleep to show what's happening 94 // await Task.Delay(TimeSpan.FromSeconds(60)); 95 96 // // and last shut down the scheduler when you are ready to close your program 97 // await scheduler.Shutdown(); 98 // } 99 //} 100 101 //public class HelloJob : IJob 102 //{ 103 // public async Task Execute(IJobExecutionContext context) 104 // { 105 // await Console.Out.WriteLineAsync("Hello Quartz.Net..." + DateTime.Now + Environment.NewLine); 106 // } 107 //} 108 109 110 111 // // Other Method 112 //class Program 113 //{ 114 // static void Main(string[] args) 115 // { 116 // LogProvider.SetCurrentLogProvider(new ConsoleLogProvider()); 117 118 // RunProgram().GetAwaiter().GetResult(); 119 120 // Console.WriteLine("Press any key to close the application"); 121 // Console.ReadKey(); 122 // } 123 124 // private static async Task RunProgram() 125 // { 126 // try 127 // { 128 // NameValueCollection props = new NameValueCollection 129 // { 130 // {"quartz.serializer.type", "binary" } 131 // }; 132 // StdSchedulerFactory factory = new StdSchedulerFactory(props); 133 // IScheduler scheduler = await factory.GetScheduler(); 134 135 // await scheduler.Start(); 136 137 // IJobDetail job = JobBuilder.Create<HelloJob>() 138 // .WithIdentity("job1", "group1") 139 // .Build(); 140 141 // ITrigger trigger = TriggerBuilder.Create() 142 // .WithIdentity("trigger1", "group1") 143 // .StartNow() 144 // .WithSimpleSchedule(x => x 145 // .WithIntervalInSeconds(10) 146 // .RepeatForever()) 147 // .Build(); 148 149 // await scheduler.ScheduleJob(job, trigger); 150 151 // await Task.Delay(TimeSpan.FromSeconds(60)); 152 153 // await scheduler.Shutdown(); 154 // } 155 // catch (SchedulerException se) 156 // { 157 // await Console.Error.WriteLineAsync(se.ToString()); 158 // } 159 // } 160 161 // private class ConsoleLogProvider : ILogProvider 162 // { 163 // public Logger GetLogger(string name) 164 // { 165 // return (level, func, exception, parameters) => 166 // { 167 // if (level >= LogLevel.Info && func != null) 168 // { 169 // Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters); 170 // } 171 // return true; 172 // }; 173 // } 174 175 // public IDisposable OpenNestedContext(string message) 176 // { 177 // throw new NotImplementedException(); 178 // } 179 180 // public IDisposable OpenMappedContext(string key, string value) 181 // { 182 // throw new NotImplementedException(); 183 // } 184 // } 185 //} 186 187 //public class HelloJob : IJob 188 //{ 189 // public async Task Execute(IJobExecutionContext context) 190 // { 191 // await Console.Out.WriteLineAsync("Greetings from HelloJob!"); 192 // } 193 //} 194 }
參考網址:
https://www.cnblogs.com/huyong/p/11091089.html
https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html
https://www.liujiajia.me/2019/02/13/quartz-net/