DEV Community

Cover image for What is Quartz.Net and its simple implementation
Emre Kocadere
Emre Kocadere

Posted on

2 1

What is Quartz.Net and its simple implementation

quartz.net is a c# library that allows specific actions to be run at certain times and periods.

Job:it is action that to be run
Trigger: it controls when a job runs
Scheduler:responsible for coordinating jobs and triggers

Example

For example, you have a service about currency rates. Your service needs to go to another service every 5 minutes and get the rates. You will agree that you cannot call the function every 5 minutes.

At this point, Quartz.NET comes into play.

First of all, after get the library from NuGet, you need a class that implements the IJob interface to create a background job.

The IJob interface includes a method called Execute. You will write the job you want to run within this method.

public class CurrencyRatesFetcherJob: IJob
{

    public async Task Execute(IJobExecutionContext context)
    {

      // background job..
    }
}
Enter fullscreen mode Exit fullscreen mode
builder.Services.AddQuartz(configure =>
{

    var jobKey = new JobKey("GetCurrencyRates");
    configure
        .AddJob<CurrencyRatesFetcherJob>(jobKey)
        .AddTrigger(
            trigger => trigger.ForJob(jobKey).WithSimpleSchedule(
                schedule => schedule.WithIntervalInMinutes(5).RepeatForever()));

});

Enter fullscreen mode Exit fullscreen mode

This configuration ensures that the code we wrote in Execute runs every 5 minutes.

By default, Quartz configures all jobs using the RAMJobStore. The locations where Quartz.NET jobs are stored are managed by the Quartz job store.These job stores are used to store the status, schedules, and other information of jobs and triggers.

In my next article, I will write about using cron for more complex schedules and how to store jobs and triggers somewhere other than RAM.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay