Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Getting Started with Quartz.NET for Periodic Tasks

Tech May 14 14

To implement periodic tasks in C#, you first need to reference the Quartz NuGet package in your project.

Next, define a scheduler initialization class that manages three core components: the scheduler itself, the job detail (specifying what to execute), and the trigger (defining when and how often too run).

public static async Task InitializeScheduler()
{
    // Initialize scheduler instance
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = await schedulerFactory.GetScheduler();
    await scheduler.Start();

    // Create job definition
    var dailyAlertJob = JobBuilder.Create<DailyAlertTask>()
        .WithIdentity("dailyHealthCheck", "notificationGroup")
        .WithDescription("System health check job running at regular intervals")
        .Build();

    // Configure execution trigger
    var intervalTrigger = TriggerBuilder.Create()
        .WithIdentity("dailyHealthCheckTrigger", "notificationGroup")
        // Uncomment to run immediately on startup
        //.StartNow()
        .WithSimpleSchedule(config => config
            .WithIntervalInSeconds(5)
            .WithRepeatCount(3)) // Runs 4 times total (initial + 3 repeats); use RepeatForever() for endless execution
        .Build();

    // Associate job and trigger with the scheduler
    await scheduler.ScheduleJob(dailyAlertJob, intervalTrigger);
}

The DailyAlertTask class is your custom implementation of the periodic behavior, inehriting from Quartz's IJob interface.

public class DailyAlertTask : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        await Task.Run(() =>
        {
            Console.WriteLine("=========================================");
            Console.WriteLine($"Daily system health check executed at {DateTime.Now}");
            Console.WriteLine("=========================================");
            Console.WriteLine();
        });
    }
}

To start the scheduler and begin executing tasks, call the initialization method from your application entry point.

SchedulerBootstrap.InitializeScheduler().GetAwaiter().GetResult();
Tags: Quartz.NETC#

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.