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..
}
}
builder.Services.AddQuartz(configure =>
{
var jobKey = new JobKey("GetCurrencyRates");
configure
.AddJob<CurrencyRatesFetcherJob>(jobKey)
.AddTrigger(
trigger => trigger.ForJob(jobKey).WithSimpleSchedule(
schedule => schedule.WithIntervalInMinutes(5).RepeatForever()));
});
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.
Top comments (0)