In this blog, we will walk through the process of creating a scheduler job using Quartz.NET in a .NET Framework Console Application. Quartz.NET is a powerful, open-source job scheduling library that allows you to schedule and run tasks with high flexibility and precision. We’ll cover every step in detail to make it beginner friendly.
Prerequisites:
- Visual Studio Code installed on your machine.
- .NET installed
- Basic knowledge of C# programming.
Step 1: Create a Console Application Project in .NET Framework
- Open Visual Studio Code and create a new console app name
QuartzSchedulerApp - Click on Create.
Now, you have an empty Console Application to start with.
Step 2: Install Quartz.NET NuGet Package
The next step is to install the Quartz.NET package, which will allow us to schedule jobs.
You can use the Package Manager Console to install the package by running this command:
Install-Package Quartz -Version 3.13.1
Step 3: Create a Class for the Job
In this step, we will create a class that implements the IJob interface. This class will contain the logic that you want to execute periodically.
- Right-click on the project and select Add > Class.
- Name the class
MyJob.csand click Add.
Now to implement the IJob interface. This interface has a single method Execute that you need to define.
Here’s the code for MyJob.cs:
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QuartzSchedulerApp
{
public class MyJob:IJob
{
public Task Execute(IJobExecutionContext context)
{
// Logic to be executed when the job runs
Console.WriteLine($"Job executed at: {DateTime.Now}");
return Task.CompletedTask;
}
}
}
Explanation:
1. The Execute method contains the logic that gets executed when the job is triggered.
2. The Console.WriteLine outputs the current date and time every time the job is triggered.
Step 4: Write the Main Program to Schedule the Job
Now we’ll configure the scheduler to run the job periodically. This code will go in the Program.cs file.
- Open
Program.cs(this is the main entry point of your application). - Add the following code to configure and start the scheduler:
using Quartz;
using Quartz.Impl;
using QuartzSchedulerApp;
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Create a scheduler factory
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Get a scheduler
IScheduler scheduler = await schedulerFactory.GetScheduler();
// Start the scheduler
await scheduler.Start();
// Define the job and tie it to our MyJob class
IJobDetail job = JobBuilder.Create<MyJob>()
.WithIdentity("myJob", "group1") // Job name and group
.Build();
// Trigger the job to run every 2 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1") // Trigger name and group
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(2) // Set the interval to 2 seconds
.RepeatForever()) // The job will repeat forever
.Build();
// Schedule the job with the trigger
await scheduler.ScheduleJob(job, trigger);
// Keep the console app running to allow jobs to execute
Console.WriteLine("Scheduler started. Press any key to exit...");
Console.ReadKey();
// Shutdown the scheduler when done
await scheduler.Shutdown();
}
}
Explanation of the Code:
- Scheduler Factory and Scheduler: First, we create an instance of
StdSchedulerFactoryand get theISchedulerinstance. - Job Creation: We define the job using
JobBuilder.Create<MyJob>()whereMyJobis the class we created earlier. - Trigger: We set up a trigger that starts immediately (
StartNow()) and repeats every 2 seconds. - Job Scheduling: We schedule the job with the defined trigger using
scheduler.ScheduleJob(job, trigger). - Console Output: We keep the console application running until the user presses a key.
Step 5: Run the Application
Once you’ve added all the code, you can now run your application.
- after running the app, You should see the message:
Scheduler started. Press any key to exit.... - Every 2 seconds, you’ll see a log in the console showing when the job is executed. It should look like this:
The job will continue to run every 2 seconds until you press any key to stop the application.
Conclusion
In this blog, we’ve created a simple console application that uses Quartz.NET to run a job at regular intervals. We walked through every step in detail, from creating the project to scheduling a job to executing it every 2 seconds.
By following these steps, you can build more complex scheduler jobs using Quartz.NET, such as running database tasks, sending emails, or triggering other actions at specified intervals.