当前位置:网站首页>Quartz. Net getting started

Quartz. Net getting started

2022-06-21 05:59:00 Wind god Shura envoy

Preface
stay ASP.NET The common scheduling framework in is nothing more than Quartz.NET And Hangfire Two kinds of , In the past, I often used to develop Hangfire With its background management interface , It is very convenient and easy to use , Recently, I have encountered some problems in new projects schedule The needs of , Colleagues pushed Quartz.Net To work as a job scheduler ,Quartz.Net It is a fully functional work scheduling framework , from Java Popular scheduling framework Quartz Migration to .NET On ,open source And provide flexible settings for developers to use , In the new 3.0.7 Support .NET Core 2.1 edition , Today, let's briefly introduce Quartz.NET Installation and basic application of , If there are any problems or mistakes, you are welcome to give guidance .

install
First , First create a file named QuartzNetConsole Of Console Project , Then open Nuget Package Mnage Input “quartz” Search for , Install the latest version of Quartz.NET Kit
QuartzNetInstall

Or in Nuget Package Console Input instruction

Install-Package Quartz 

If there is Json Serialize requirements , You can also add Quartz.Serialization.Json

Use
Before using, introduce Quartz.Net A few important API And Interface

  • IScheduler : Main work schedule API、 through Start Method run Scheduling .

  • JobBuilderIJobDetail : through JobBuilder.Create produce IJobDetail Of Instance.

  • TriggerBuilderITrigger : through TriggerBuilder.Create produce ITrigger Of Instance.

IJob : The custom schedule category needs to be implemented Interface

The diagram is as follows
QuartzNetAPIInterface
.png)]

And then Console Project Program.cs Medium Main Add the following code

class Program
{
    
    static void Main(string[] args)
    {
    
        // trigger async evaluation
        RunProgram().GetAwaiter().GetResult();
    }
 
    private static async Task RunProgram()
    {
    
        try
        {
    
            //  establish  scheduler
            StdSchedulerFactory factory = new StdSchedulerFactory();
            IScheduler scheduler = await factory.GetScheduler();
 
            //  establish  Job
            IJobDetail job = JobBuilder.Create<ShowDataTimeJob>()
                .WithIdentity("job1", "group1")
                .Build();
 
            //  establish  Trigger, Run once per second 
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInSeconds(1)
                    .RepeatForever())
                .Build();
 
            //  Join in  ScheduleJob  in 
            await scheduler.ScheduleJob(job, trigger);
 
            //  start-up 
            await scheduler.Start();
 
            //  perform  10  second 
            await Task.Delay(TimeSpan.FromSeconds(10));
 
            // say goodbye
            await scheduler.Shutdown();
        }
        catch (SchedulerException se)
        {
    
            await Console.Error.WriteLineAsync(se.ToString());
        }
    }
}

Which is to be executed ShowDataTimeJob The category code is as follows

internal class ShowDataTimeJob :IJob
{
    
		public async Task Execute(IJobExecutionContext context)
    {
    
        await Console.Out.WriteLineAsync($" present time  {
      DateTime.Now}");
    }
}

Program description

  • establish scheduler : through factory.GetScheduler() obtain schedule

  • establish Job : Use JobBuilder.Create establish ShowDataTimeJob, And define its key And group name

  • establish Trigger : Use TriggerBuilder establish ITrigger , Define its key And group name , And set the immediate execution time to execute once every second , among ShowDataTimeJob Of Execute This is the execution . The results are as follows :

 Insert picture description here

feeling
Through simple description and operation , You can quickly create Quartz.NET The function of work scheduling , But in fact, the requirements and implementation code in real life are often not so simple , If you are interested, you can first refer to the development instructions of the official website , I will share it with you in the future ,Happy Coding .

in addition ,Quartz.Net It can also be used with other kits to set the profile XML In the file , It can even be matched with a little hand-made spirit , Hand engrave some code and place the profile in YAML In the file , Convenient configuration . Besides ,JOB The definition and implementation cycle of can also be achieved by installing other packages , Store it SQL Server or Redis in , Get a more flexible configuration . Or, , It can be placed in... Like a profile YAML In the file .

原网站

版权声明
本文为[Wind god Shura envoy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221612034276.html