1024programmer Asp.Net Explain in detail the application of some advanced functions of Quartz.NET through examples. How much have you used?

Explain in detail the application of some advanced functions of Quartz.NET through examples. How much have you used?

Explain in detail the application of some advanced functions of Quartz.NET through examples. How much have you used?

Quartz.NET is a powerful open source job scheduling library that provides many advanced features. The following are common advanced features of Quartz.NET:

  1. Cron expression trigger: Use Cron expressions to define flexible scheduling rules and implement complex time scheduling strategies.
  2. Job dependencies:Allows you to define dependencies between jobs, ensuring that they are executed in a specific order.
  3. Data transfer during job execution: When scheduling a job, parameters and data can be passed so that the job can obtain execution context information as needed.
  4. Global job listener: Add a global listener to monitor job execution life cycle events, such as before and after job execution, etc.
  5. Global trigger listener: Add a global listener to listen for trigger life cycle events, such as trigger firing, trigger completion, etc.
  6. Custom calendar:Customized calendar logic can be implemented, such as excluding specific dates or time periods, to meet business needs.
  7. Cluster mode: Allows Quartz.NET instances to be configured as clusters to implement distributed job scheduling and ensure high availability and load balancing.
  8. Persistent jobs: Quartz.NET provides job persistence support, which can store jobs and triggers in the database to ensure that jobs will not be lost.
  9. Job status record: Quartz.NET can record the execution status of jobs, including success, failure, rejection, etc., to facilitate monitoring and troubleshooting.
  10. Parallel execution: Allow multiple jobs to be executed at the same time to improve the concurrency performance of the system.

These features make Quartz.NET a flexible and feature-rich job scheduling framework suitable for various complex tasks. Scheduling requirements.

The following are examples of advanced features of Quartz.NET, including Chinese comments:

1.Cron expression trigger

using Quartz;
 using Quartz.Impl;
 using System;

 class Program
 {
     static void Main(string[] args)
     {
         try
         {
             //Create a scheduler factory
             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

             // Get the scheduler instance
             IScheduler scheduler = schedulerFactory.GetScheduler().Result;

             //Create job
             IJobDetail job = JobBuilder.Create()
                 .WithIdentity("networkStatusCheckJob", "group1")
                 .Build();

             // Create a trigger, use Cron expression, trigger once every day at 10:30
             ITrigger trigger = TriggerBuilder.Create()
                 .WithIdentity("networkStatusCheckTrigger", "group1")
                 .StartNow()
                 .WithCronSchedule("0 30 10 ? * *")
                 .Build();

             //Add jobs and triggers to the scheduler
             scheduler.ScheduleJob(job, trigger).Wait();

             //Start the scheduler
             scheduler.Start().Wait();

             Console.WriteLine("Quartz.NET has been started, press any key to exit...");
             Console.ReadKey();

             // Close the scheduler
             scheduler.Shutdown().Wait();
         }
         catch (SchedulerException se)
         {
             Console.WriteLine(se);
         }
     }
 }

2. Dependencies between jobs

using Quartz;
 using Quartz.Impl;
 using System;

 class Program
 {
     static void Main(string[] args)
     {
         try
         {
             //Create a scheduler factory
             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

             // Get the scheduler instance
             IScheduler scheduler = schedulerFactory.GetScheduler().Result;

             //Create job 1
             IJobDetail job1 = JobBuilder.Create()
                 .WithIdentity("job1", "group1")
                 .Build();

             //Create job 2
             IJobDetail job2 = JobBuilder.Create()
                 .WithIdentity("job2", "group1")
                 .Build();

             //Create trigger 1
             ITrigger trigger1 = TriggerBuilder.Create()
                 .WithIdentity("trigger1", "group1")
                 .StartNow()
                 .Build();

             //Create trigger 2 and set it to execute after trigger 1
             ITrigger trigger2 = TriggerBuilder.Create()
                 .WithIdentity("trigger2", "group1")
                 .StartNow()
                 .Build();

             //Add jobs and triggers to the scheduler
             scheduler.ScheduleJob(job1, trigger1).Wait();
             scheduler.ScheduleJob(job2, trigger2.ForJob(job1).Build()).Wait();

             //Start the scheduler
             scheduler.Start().Wait();

             Console.WriteLine("Quartz.NET has been started, press any key to exit...");
             Console.ReadKey();

             // Close the scheduler
             scheduler.Shutdown().Wait();
         }
         catch (SchedulerException se)
         {
             Console.WriteLine(se);
         }
     }
 }

 public class Job1 : IJob
 {
     public void Execute(IJobExecutionContext context)
     {
         Console.WriteLine("Job1 execution");
     }
 }

 public class Job2 : IJob
 {
     public void Execute(IJobExecutionContext context)
     {
         Console.WriteLine("Job2 Execution");
     }
 }

3. Data transfer during job execution

using Quartz;
 using Quartz.Impl;
 using System;

 class Program
 {
     static void Main(string[] args)
     {
         try
         {
             //Create a scheduler factory
             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

             // Get the scheduler instance
             IScheduler scheduler = schedulerFactory.GetScheduler().Result;

             //Create a job with data
             IJobDetail job = JobBuilder.Create()
                 .WithIdentity("dataPassingJob", "group1")
                 .UsingJobData("key1", "value1")
                 .UsingJobData("key2", 123)
                 .Build();

             //Create trigger
             ITrigger trigger = TriggerBuilder.Create()
                 .WithIdentity("dataPassingTrigger", "group1")
                 .StartNow()
                 .WithSimpleSchedule(x => x
                     .WithIntervalInSeconds(20)
                     .RepeatForever())
                 .Build();

             //Add jobs and triggers to the scheduler
             scheduler.ScheduleJob(job, trigger).Wait();

             //Start the scheduler
             scheduler.Start().Wait();

             Console.WriteLine("Quartz.NET has been started, press any key to exit...");
             Console.ReadKey();

             // Close the scheduler
             scheduler.Shutdown().Wait();
         }
         catch (SchedulerException se)
         {
             Console.WriteLine(se);
         }
     }
 }

 public class DataPassingJob : IJob
 {
     public void Execute(IJobExecutionContext context)
     {
         // Get the passed data from the job execution context
         JobDataMap dataMap = context.JobDetail.JobDataMap;
         string value1 = dataMap.GetString("key1");
         int value2 = dataMap.GetInt("key2");

         Console.WriteLine($"Data in job execution: key1={value1}, key2={value2}");
     }
 }

4. Global job listener

using Quartz;
 using Quartz.Impl;
 using System;

 public class GlobalJobListener : IJobListener
 {
     public string Name => "GlobalJobListener";

     public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default)
     {
         Console.WriteLine($"Job rejected: {context.JobDetail.Key}");
         return Task.CompletedTask;
     }

     public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default)
     {
         Console.WriteLine($"Preparing to execute the job: {context.JobDetail.Key}");
         return Task.CompletedTask;
     }

     public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
     {
         Console.WriteLine($"Job execution completed: {context.JobDetail.Key}");
         return Task.CompletedTask;
     }
 }

 class Program
 {
     static void Main(string[] args)
     {
         try
         {
             //Create a scheduler factory
             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

             // Get the scheduler instance
             IScheduler scheduler = schedulerFactory.GetScheduler().Result;

             //Add global job listener
             scheduler.ListenerManager.AddJobListener(new GlobalJobListener());

             //Create job
             IJobDetail job = JobBuilder.Create()
                 .WithIdentity("simpleJob", "group1")
                 .Build();

             //Create trigger
             ITrigger trigger = TriggerBuilder.Create()
                 .WithIdentity("simpleTrigger", "group1")
                 .StartNow()


                 .WithSimpleSchedule(x => x
                     .WithIntervalInSeconds(20)
                     .RepeatForever())
                 .Build();

             //Add jobs and triggers to the scheduler
             scheduler.ScheduleJob(job, trigger).Wait();

             //Start the scheduler
             scheduler.Start().Wait();

             Console.WriteLine("Quartz.NET has been started, press any key to exit...");
             Console.ReadKey();

             // Close the scheduler
             scheduler.Shutdown().Wait();
         }
         catch (SchedulerException se)
         {
             Console.WriteLine(se);
         }
     }
 }

 public class SimpleJob : IJob
 {
     public void Execute(IJobExecutionContext context)
     {
         Console.WriteLine("Simple job is executing...");
     }
 }

5. Global trigger listener

using Quartz;
 using Quartz.Impl;
 using System;

 public class GlobalTriggerListener : ITriggerListener
 {
     public string Name => "GlobalTriggerListener";

     public Task TriggerComplete(ITrigger trigger, IJobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode, CancellationToken cancellationToken = default)
     {
         Console.WriteLine($"Trigger completed: {trigger.Key}");
         return Task.CompletedTask;
     }

     public Task TriggerFired(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default)
     {
         Console.WriteLine($"Trigger fired: {trigger.Key}");
         return Task.CompletedTask;
     }

     public Task TriggerMisfired(ITrigger trigger, CancellationToken cancellationToken = default)
     {
         Console.WriteLine($"Trigger missed firing: {trigger.Key}");
         return Task.CompletedTask;
     }

     public Task VetoJobExecution(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default)
     {
         Console.WriteLine($"Deny job execution: {trigger.Key}");
         return Task.FromResult(false);
     }
 }

 class Program
 {
     static void Main(string[] args)
     {
         try
         {
             //Create a scheduler factory
             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

             // Get the scheduler instance
             IScheduler scheduler = schedulerFactory.GetScheduler().Result;

             //Add global trigger listener
             scheduler.ListenerManager.AddTriggerListener(new GlobalTriggerListener());

             //Create job
             IJobDetail job = JobBuilder.Create()
                 .WithIdentity("simpleJob", "group1")
                 .Build();

             //Create trigger
             ITrigger trigger = TriggerBuilder.Create()
                 .WithIdentity("simpleTrigger", "group1")
                 .StartNow()
                 .WithSimpleSchedule(x => x
                     .WithIntervalInSeconds(20)
                     .RepeatForever())
                 .Build();

             //Add jobs and triggers to the scheduler
             scheduler.ScheduleJob(job, trigger).Wait();

             //Start the scheduler
             scheduler.Start().Wait();

             Console.WriteLine("Quartz.NET has been started, press any key to exit...");
             Console.ReadKey();

             // Close the scheduler
             scheduler.Shutdown().Wait();
         }
         catch (SchedulerException se)
         {
             Console.WriteLine(se);
         }
     }
 }

 public class SimpleJob : IJob
 {
     public void Execute(IJobExecutionContext context)
     {
         Console.WriteLine("Simple job is executing...");
     }
 }

6. Custom calendar

using Quartz;
 using Quartz.Impl;
 using System;

 public class CustomCalendar : BaseCalendar
 {
     // Implement custom calendar logic. Here is a simple example of excluding weekends.
     public override bool IsTimeIncluded(DateTimeOffsIndustry
             IJobDetail job = JobBuilder.Create()
                 .WithIdentity("simpleJob", "group1")
                 .StoreDurably() // Set as a persistent job
                 .Build();

             //Create trigger
             ITrigger trigger = TriggerBuilder.Create()
                 .WithIdentity("simpleTrigger", "group1")
                 .StartNow()
                 .WithSimpleSchedule(x => x
                     .WithIntervalInSeconds(20)
                     .RepeatForever())
                 .Build();

             //Add jobs and triggers to the scheduler
             scheduler.AddJob(job, true).Wait();
             scheduler.ScheduleJob(trigger).Wait();

             //Start the scheduler
             scheduler.Start().Wait();

             Console.WriteLine("Quartz.NET has been started, press any key to exit...");
             Console.ReadKey();

             // Close the scheduler
             scheduler.Shutdown().Wait();
         }
         catch (SchedulerException se)
         {
             Console.WriteLine(se);
         }
     }
 }

 public class SimpleJob : IJob
 {
     public void Execute(IJobExecutionContext context)
     {
         Console.WriteLine("Simple job is executing...");
     }
 }

9. Job status record

using Quartz;
 using Quartz.Impl;
 using System;

 class Program
 {
     static void Main(string[] args)
     {
         try
         {
             //Create a scheduler factory
             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

             // Get the scheduler instance
             IScheduler scheduler = schedulerFactory.GetScheduler().Result;

             //Create job
             IJobDetail job = JobBuilder.Create()
                 .WithIdentity("statusRecordingJob", "group1")
                 .Build();

             //Create trigger
             ITrigger trigger = TriggerBuilder.Create()
                 .WithIdentity("statusRecordingTrigger", "group1")
                 .StartNow()
                 .WithSimpleSchedule(x => x
                     .WithIntervalInSeconds(20)
                     .RepeatForever())
                 .Build();

             //Add jobs and triggers to the scheduler
             scheduler.ScheduleJob(job, trigger).Wait();

             //Start the scheduler
             scheduler.Start().Wait();

             Console.WriteLine("Quartz.NET has been started, press any key to exit...");
             Console.ReadKey();

             // Close the scheduler
             scheduler.Shutdown().Wait();
         }
         catch (SchedulerException se)
         {
             Console.WriteLine(se);
         }
     }
 }

 public class StatusRecordingJob : IJob
 {
     public void Execute(IJobExecutionContext context)
     {
         //Record job status
         Console.WriteLine($"Job execution... Execution status: {context.Result}");
     }
 }

10. Parallel execution

using Quartz;
 using Quartz.Impl;
 using System;
 using System.Threading.Tasks;

 class Program
 {
     static async Task Main(string[] args)
     {
         try


         {
             //Create a scheduler factory
             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

             // Get the scheduler instance
             IScheduler scheduler = await schedulerFactory.GetScheduler();

             //Create job 1
             IJobDetail job1 = JobBuilder.Create()
                 .WithIdentity("parallelJob1", "group1")
                 .Build();

             //Create job 2
             IJobDetail job2 = JobBuilder.Create()
                 .WithIdentity("parallelJob2", "group1")
                 .Build();

             //Create trigger 1
             ITrigger trigger1 = TriggerBuilder.Create()
                 .WithIdentity("trigger1", "group1")
                 .StartNow()
                 .Build();

             //Create trigger 2
             ITrigger trigger2 = TriggerBuilder.Create()
                 .WithIdentity("trigger2", "group1")
                 .StartNow()
                 .Build();

             //Add jobs and triggers to the scheduler
             await scheduler.ScheduleJob(job1, trigger1);
             await scheduler.ScheduleJob(job2, trigger2);

             //Start the scheduler
             await scheduler.Start();

             Console.WriteLine("Quartz.NET has been started, press any key to exit...");
             Console.ReadKey();

             // Close the scheduler
             await scheduler.Shutdown();
         }
         catch (SchedulerException se)
         {
             Console.WriteLine(se);
         }
     }
 }

 public class ParallelJob : IJob
 {
     public async Task Execute(IJobExecutionContext context)
     {
         // Simulate job execution time
         await Task.Delay(TimeSpan.FromSeconds(10));

         Console.WriteLine($"Parallel job execution... Execution time: {DateTime.Now}");
     }
 }

These examples cover some advanced features of Quartz.NET, including Cron expression triggers and inter-job dependencies performance, data passing in job execution, global job listeners, global trigger listeners, custom calendars, cluster mode, persistent jobs, job status logging, and parallel execution. I hope it will be helpful for you to understand the advanced functions of Quartz.NET.

}
}

These examples cover some advanced features of Quartz.NET, including Cron expression triggers and inter-job dependencies performance, data passing in job execution, global job listeners, global trigger listeners, custom calendars, cluster mode, persistent jobs, job status logging, and parallel execution. I hope it will be helpful for you to understand the advanced functions of Quartz.NET.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/explain-in-detail-the-application-of-some-advanced-functions-of-quartz-net-through-examples-how-much-have-you-used/

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
首页
微信
电话
搜索