quartz.net 3.x 使用总结
2018-02-09 16:36
423 查看
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使用总结
- Quartz.net使用总结
- Quartz.net使用总结
- Quartz.Net使用总结
- quartz.net 使用总结
- Quartz.net使用总结
- (转贴).NET环境下水晶报表使用总结
- (转贴).NET环境下水晶报表使用总结
- ASP.NET设置数据格式与String.Format使用总结
- kshen转总结:ADO.NET在开发中的部分使用方法和技巧
- 初次使用Ajax.net,总结下心得并记录下来
- Asp.Net 2.0使用总结
- Quartz.net官方开发指南 第十课: 配置、资源使用以及SchedulerFactory
- NET环境下水晶报表使用总结
- ASP.NET设置数据格式与String.Format使用总结(引)
- Quartz.net官方开发指南 第一课:使用Quartz.net
- Quartz.net官方开发指南 第一课:使用Quartz.net 推荐
- 总结:ADO.NET在开发中的部分使用方法和技巧
- kindeditor在asp.net中如何使用的总结
- asp.net 2.0 自定义控件中的使用嵌入资源的一些总结