As a developer, you probably have tasks you’d like to automate — things like sending emails, generating reports, or cleaning up data on a schedule.
So, how do you make that happen?
That’s where services come in. They’re a great way to handle automation efficiently and reliably.
Let’s break down how you can set this up using services.
Quartz is a popular open-source tool for scheduling and automation.
At the heart of it is the Quartz Trigger, a core component of the Quartz Scheduler, a powerful job scheduling library available in both C# and Java.
Official Documentation: [https://www.quartz-scheduler.org/documentation/]()
A Trigger defines when and how often a job should run. \n Think of it as the schedule attached to a job.
When you schedule a job in Quartz, you provide:
\
A Cron Expression is a string with 6 or 7 fields that defines a schedule, specifying exactly when a job should run (down to seconds). \n It’s like a compact language for time-based scheduling.
Detailed expression: you can read it in their official documents.
The requirement is to read the file every 30 seconds.
Let’s implement a Cron trigger in .NET 8.
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <RootNamespace>CronImplementation</RootNamespace> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Quartz" Version="3.15.1" /> <PackageReference Include="Quartz.Plugins" Version="3.15.1" /> </ItemGroup> <ItemGroup> <Content Include="jobs.xml" CopyToOutputDirectory="Always" /> </ItemGroup> </Project>
using Quartz; using Quartz.Impl; using Quartz.Xml; using Quartz.Simpl; // <-- needed for SimpleTypeLoadHelper namespace CronImplementation { class Program { static async Task Main(string[] args) { //Create a scheduler ISchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = await factory.GetScheduler(); //Create a type load helper (required by the new API) var typeLoadHelper = new SimpleTypeLoadHelper(); typeLoadHelper.Initialize(); //Use the new XMLSchedulingDataProcessor constructor var xmlProcessor = new XMLSchedulingDataProcessor(typeLoadHelper); //Ensure file path is an absolute string xmlPath = Path.Combine(AppContext.BaseDirectory, "jobs.xml"); xmlProcessor.ProcessFileAndScheduleJobs(xmlPath, scheduler); Console.WriteLine(File.Exists(xmlPath)); //Start the scheduler await scheduler.Start(); Console.WriteLine("Quartz Scheduler started using XML configuration. Press any key to stop..."); Console.ReadKey(); await scheduler.Shutdown(); Console.WriteLine("Scheduler stopped."); } } }
using Quartz; namespace CronImplementation { public class FileReadingJob : IJob { public Task Execute(IJobExecutionContext context) { string filePath = Path.Combine(AppContext.BaseDirectory, "file.txt"); if (!File.Exists(filePath)) { Console.WriteLine($"File not found: {filePath}"); return Task.CompletedTask; } //Read all lines string[] lines = File.ReadAllLines(filePath); foreach (var line in lines) { Console.WriteLine(line); } Console.WriteLine($"FileReadingJob executed at: {DateTime.Now}"); return Task.CompletedTask; } } }
<?xml version="1.0" encoding="UTF-8"?> <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> <!-- List of scheduled jobs --> <schedule> <!-- Job definition --> <job> <name>FileReadingJob</name> <group>group1</group> <description>Job that reads content from file</description> <job-type>CronImplementation.FileReadingJob, CronImplementation</job-type> <!-- Namespace.ClassName, Assembly --> <durable>true</durable> <recover>false</recover> </job> <!-- Trigger definition (Cron-based) --> <trigger> <cron> <name>FileReadingJobTrigger</name> <group>group1</group> <job-name>FileReadingJob</job-name> <job-group>group1</job-group> <cron-expression>0/10 * * * * ?</cron-expression> <!-- Runs every 10 seconds --> </cron> </trigger> </schedule> </job-scheduling-data>
“This is the console app. This project explains the implementation of the Cron trigger in NET 8.”
The Quartz trigger is platform-independent, meaning you can deploy your apps on both Windows and Linux servers without any issues.
Another great feature is that it lets you assign different trigger schedules to multiple jobs, giving you more flexibility in how tasks are executed.
You can also update or delete triggers by simply editing the XML file—no need to change the code. Just keep in mind that the service needs to be restarted for the changes to take effect.
\


