1024programmer Asp.Net The use of Quartz in C#

The use of Quartz in C#

The use of Quartz in C#

  • Install the NuGet package: Open the project in Visual Studio, right click on the project name, select “Manage NuGet Packages”, search for “Quartz” and install.

  • Create a job: Create a class that implements the IJob interface that contains an Execute method that will be called when the job runs. For example:

    public class MyJob : IJob
     {
         public Task Execute(  IJobExecutionContext context)
         {
             Console.WriteLine("Hello, world!");
             return Task.CompletedTask  ;
         }
     }
  • Pass data to Job:

    // Create a JobDataMap  object
     JobDataMap jobDataMap = new JobDataMap();
    
     // to JobDataMap  Add parameters in
     jobDataMap.Add("param1", "Hello");
     jobDataMap.Add("param2",   "World");
    
     // Create a  JobDetail object, set JobDataMap
     JobDetail jobDetail = JobBuilder. Create()
         .UsingJobData(jobDataMap)
         .Build();
    
     // in Job  Get parameters in the class
     public class MyJob : IJob
     {
         public void</span  span> Execute(IJobExecutionContext context)
         {
             JobDataMap jobDataMap = context.JobDetail.JobDataMap;
             string param1 = jobDataMap. GetString("param1");
             string param2 = jobDataMap. GetString("param2");
    
             Console.WriteLine("{0} {1}", param1, param2);
         }
     }

Note: Every time a job is executed, a new job object will be created and the data in the JobDataMap will be passed to it. This means that even if you store some state or data in the job, they will not be preserved the next time the job is executed.

  • Create trigger: Create a trigger to specify when to run the job. For example, the following trigger will run the job every day at 10:00 AM:
ITrigger trigger = TriggerBuilder.Create()
     .WithIdentity("  trigger1", "  group1")
     .WithDailyTimeIntervalSchedule(
         x => x.OnEveryDay()
               .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(10, 0))
     )
     .Build();

You can also use WithCronSchedule instead of WithDailyTimeIntervalSchedule, for example: the following triggers at 9:00 every day

.WithCronSchedule(cron) //"0 0 9 * * ?"

The meaning of this expression is:

    • The first field: 0, indicating that the second is 0.
    • The second field: 0, indicating that the minute is 0.
    • The third field: 14, indicating that the hour is 14, that is, 2 o’clock in the afternoon.
    • The fourth field: *, means match every day.
    • Fifth field: *, means matching every month.
    • The sixth field: ?, which means that the day of the week is not specified.
    • The seventh field: not used.
  • Create a scheduler: use StdSchedulerFactory to create a scheduler, and add jobs and triggers to the scheduler:

ISchedulerFactory schedulerFactory = new StdSchedulerFactory  ();
 IScheduler scheduler = await  schedulerFactory. GetScheduler();
 IJobDetail job = JobBuilder. Create()
     .WithIdentity("  job1", "  group1")
.StartNow() .Build();
await scheduler.ScheduleJob(job, trigger);

Use the StartNow() method to start executing the task immediately. In this way, no matter when the scheduler is created, task scheduling will be executed immediately (not job execution, task scheduling execution means that if I set a trigger at 9 o’clock in the morning, now it is the task scheduling created at 8 o’clock in the morning , then it will not be executed at 9:00 today, and it will not be executed until 9:00 tomorrow; now that StartNow is set, the job will be executed at 9:00 today), and then continue to execute according to the scheduling rules.

  • Run job: Call the Start method to start the scheduler, so that the job runs according to the specified trigger:

    await scheduler. Start();

    Note: This is just to start the scheduler, and the job will not be executed immediately, it needs a trigger to be executed.

  • The local system time adjustment will affect the execution of Quartz, because Quartz’s time corresponds to the system time.
ISchedulerFactory schedulerFactory = new StdSchedulerFactory  ();
 IScheduler scheduler = await  schedulerFactory. GetScheduler();
 IJobDetail job = JobBuilder. Create()
     .WithIdentity("  job1", "  group1")
     .WithMisfireHandlingInstructionFireAndProceed()
     .StartNow()
     .Build();

 await scheduler.ScheduleJob(job, trigger);

WithMisfireHandlingInstructionFireAndProceed is a method of JobBuilder in Quartz.NET, which specifies the misfire policy of the task (Job) as “execute immediately after missing”, that is, when the task misses the scheduled execution time, execute the task immediately instead of waiting next execution time.

In Quartz.NET, when a trigger (Trigger) is triggered, but the task does not start in time due to some reasons, a misfire will occur. The misfire policy is used to specify how Quartz.NET should handle this kind of misfire.

In JobBuilder, the WithMisfireHandlingInstructionFireAndProceed method is used to set the misfire policy of a task. Using this method can avoid a large delay in the program and ensure that the task can be executed in time.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/the-use-of-quartz-in-c/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索