• Quartz.NET快速入门指南


    Quartz.NET 简介

    Quartz.NET是一个用C#编写的基于.NetCore的纯.Net库,是一个非常流行的开源Java作业调度框架Quartz的.Net版本。这个项目很大程度上归功于原始的Java项目。此项目已更新到3.0+版本,也是博主学习使用的版本。官方文档

    Quartz.NET 快速入门

    Quartz.NET 关键接口和类

    • IScheduler : - 与调度程序交互的主要API。
    • IJob - 由您希望由调度程序执行的组件实现的接口。
    • IJobDetail - 用于定义Jobs的实例。
    • ITrigger - 一个触发器,用于定义执行给定作业的计划。
    • JobBuilder - 用于定义/构建JobDetail实例,用于定义Jobs的实例。
    • TriggerBuilder - 用于定义/构建触发器实例。

    示例应用程序

    using Quartz.Impl;
    using Quartz.Logging;
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Quartz
    {
        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
                {
                    // 第一步:从工厂中获取Scheduler实例
                    NameValueCollection props = new NameValueCollection{
                        { "quartz.serializer.type", "binary" }
                    };
                    StdSchedulerFactory factory = new StdSchedulerFactory(props);
                    IScheduler scheduler = await factory.GetScheduler();
    
    
                    // 第二步:然后运行它
                    await scheduler.Start();
    
                    // 第三步:定义作业并绑定到HelloJob类,HelloJob类继承IJob接口
                    IJobDetail job = JobBuilder.Create<HelloJob>()
                        .WithIdentity("job1", "group1")
                        .Build();
    
                    // 第四步:创建触发器。设定,每十秒执行一次作业。永远重复。
                    ITrigger trigger = TriggerBuilder.Create()
                        .WithIdentity("trigger1", "group1") //指定唯一标识,触发器名字,和组名字,这对于将作业和触发器组织成“报告作业”和“维护作业”等类别非常有用。
                        .StartNow() //作业或触发器的键的名称部分在组内必须是唯一的
                        .WithSimpleSchedule(x => x //从现在开始执行
                            .WithIntervalInSeconds(10) //每十秒执行一次
                            .RepeatForever()) //永远重复
                        .Build();
    
                    // 第五步:作业与触发器组合,安排任务
                    await scheduler.ScheduleJob(job, trigger);
    
                    // 延时
                    await Task.Delay(TimeSpan.FromSeconds(60));
    
                    // 可以设置关闭该调度
                    //await scheduler.Shutdown();
                }
                catch (SchedulerException se)
                {
                    Console.WriteLine(se);
                }
            }
    
            // 向控制台输出日志
            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)
            {
                await Console.Out.WriteLineAsync("欢迎使用Quartz.NET!!");
            }
        }
    }

    参考:

    1、https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html

    2、https://www.cnblogs.com/lonelyxmas/p/11789938.html

  • 相关阅读:
    内存泄露检测工具之DMalloc
    五年后你在何方
    程序员技术练级攻略
    Windows编程革命简史
    su的时候密码认证失败的解决方法
    ruby 元编程 meta programming
    内存对齐分配策略(含位域模式)
    Ruby 之 Block, Proc, Lambda 联系区别,转载
    c++异常处理机制示例及讲解
    ruby 常见问题集 1 不断更新中
  • 原文地址:https://www.cnblogs.com/winchance/p/11961675.html
Copyright © 2020-2023  润新知