The use of quartz task s c heduling

Let's talk about the simple use of task scheduler like quartz

This is the 3.x version of quartz used

The 2.x version is slightly different from this. You can view the 2.x version tutorial online

Use language c#

 

The use of quartz is divided into several steps

  1. Create an ISchedulerFactory and get the Scheduler
  2. Start Scheduler
  3. Create job task
  4. Create trigger
  5. Tel quartz to schedule the job using our trigger

Two of them can also be put after step 5

 

Also, don't forget to introduce quartz into the nuget Manager

 

 

The code is as follows

class Program
    {
        /// <summary>
        /// Entry procedure
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Run();
            Console.ReadKey();
        }

        /// <summary>
        /// Using process of task scheduling
        /// </summary>
        /// <returns></returns>
        public async static Task Run()
        {
            // 1.Establish scheduler Quotation
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            IScheduler sched = await schedFact.GetScheduler();

            //2.start-up scheduler
            await sched.Start();

            // 3.Establish job
            IJobDetail job = JobBuilder.Create<SimpleJob>()
                    .WithIdentity("job1", "group1")
                    .Build();

            // 4.Establish trigger
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
                .Build();

            // 5.Use trigger Plan and execute tasks job
            await sched.ScheduleJob(job, trigger);
        }

    }
    


    /// <summary>
    /// task
    /// </summary>
    public class SimpleJob : IJob
    {
        public virtual Task Execute(IJobExecutionContext context)
        {
            return Console.Out.WriteLineAsync($"job Working at{DateTime.Now}");
        }
        
    }

 

Run the program as follows:

Every 5 seconds

 

In addition, in the actual project use, the above steps can be disassembled and encapsulated for use

For example, step 1 and step 2 can be encapsulated together to generate a scheduler

Encapsulate step 3, step 4 and step 5 to give different task scheduler s (equivalent to registering different job s and trigger s)

 

In addition, there are more uses of quartz

Here is the website for further study:

quartz learning

Keywords: C#

Added by edg322 on Sun, 10 Nov 2019 23:13:41 +0200