Job scheduling framework Quartz.NET- off now -01- Quick Start

Original: job scheduling framework Quartz.NET- off now -01- Quick Start

Foreword

You need to apply to perform a task? This task weekly on Tuesday at 11:30 pm, perhaps only the last day of month of daily or execution. A task is performed automatically without intervention during execution if a serious error occurs, the application can perform its failure to know and try to execute it? You and your team are using .NET programming? If any of these questions you answer yes, then you should use Quartz.NET scheduler. Quartz.NET allows developers to schedule jobs time interval (or days) in accordance with. It implements many relationships and triggers the job, but also to multiple jobs associated with different triggers. Integrated Quartz.NET application can reuse jobs from different events, one event can also combine multiple jobs.

Quartz.NET Profile

Quartz.NET is written in C # based .NetCore pure .Net library, is a very popular open source Java job scheduling framework of the Quartz .Net version. The project owes much to the original Java project. This item has been updated to 3.0+ version is the version used by bloggers to learn. Official documents .

Quartz.NET Quick Start

Quartz.NET key interfaces and classes

  • IScheduler: - The main API to interact with the scheduler.
  • IJob - by the interface component you want to be executed by the scheduler implementation.
  • IJobDetail - for defining instances of Jobs.
  • ITrigger - a trigger that defines the implementation of a plan given the job.
  • JobBuilder - used to identify / build JobDetail instance, define Examples of Jobs.
  • TriggerBuilder - used to identify / build triggers instance.

Sample Application

using Quartz;
using Quartz.Impl;
using System;

namespace MGToastServer
{
    class Program
    {
        static void Main(string[] args)
        {
            StartUpJobs.StartUp().GetAwaiter().GetResult();
            Console.ReadKey();
        }
    }

    public static class StartUpJobs
    {
        public static async Task StartUp()
        {
            try
            {
                //第一步:从工厂中获取Scheduler实例
                NameValueCollection props = new NameValueCollection();
                StdSchedulerFactory factory = new StdSchedulerFactory(props);
                IScheduler scheduler = await factory.GetScheduler();
                //第二步:然后运行它
                await scheduler.Start();
                //第三步:定义作业并绑定到HelloJob类,HelloJob类继承IJob接口
                IJobDetail job = JobBuilder.Create<HelloJob>()
                        .WithIdentity("job1", "group1")
                        //UsingJobData 可以用来传参数
                        .UsingJobData("appKey", "123456QWE")
                        .UsingJobData("appName", "小熊猫")
                        .UsingJobData("api", "https://www.baidu.com")
                        .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(5));
                //await scheduler.Shutdown();
            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
            }
        }
    }

    public class HelloJob : IJob
    {
        private string appKey;
        private string appName;
        private string appApi;

        public async Task Execute(IJobExecutionContext context)
        {
            JobKey jkey = context.JobDetail.Key;
            TriggerKey tKey = context.Trigger.Key;

            JobDataMap dataMap = context.MergedJobDataMap;
            appKey = dataMap.GetString("appKey");   //通过键值获取数据
            appName = dataMap.GetString("appName");
            appApi = dataMap.GetString("api");

            await Console.Error.WriteLineAsync("[" + DateTime.Now.ToLongTimeString() + "]" + "开始推送:\n" + "JobKey:" + jkey + "\nTriggerKey:" + tKey + "\nAppKey:" + appKey + " appName: " + appName + ", and AppAPI: " + appApi);
        }
    }
}

Experimental results

As shown in the screenshot, once every ten seconds perform tasks. And you may receive an incoming parameters.

Part II

Part II: job scheduling framework for task monitor Quartz.NET-02-

Thanks

Quartz.NET

Zhang Shanyou's blog

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12048546.html