Module 3: Quartz.NET (The Alarm Clock)
📚 Module 3: Quartz.NET
Course ID: DOTNET-703
Subject: The Alarm Clock
In enterprise apps, you often need tasks to run at a specific time.
- “Send invoices on the 1st of every month.”
- “Delete temporary files every Sunday at midnight.”
We use Quartz.NET for this.
🏗️ Step 1: The “Always On” Problem
You could write a while(true) loop that checks the time every second.
- The Problem: It’s inefficient, it doesn’t handle Daylight Savings Time, and if your app restarts, you might miss a job.
🏗️ Step 2: Jobs & Triggers (The “What” and “When”)
Quartz.NET splits a task into two parts:
- The Job: The C# code you want to run. (The “What”).
- The Trigger: The schedule. (The “When”).
🧩 The Analogy: The Alarm Clock
- The Job: Waking up and making coffee.
- The Trigger: An alarm set for 7:00 AM every weekday.
🏗️ Step 3: Cron Expressions (The “Secret Code”)
Quartz uses Cron Expressions to define complex schedules.
0 0 12 * * ?-> Run every day at 12:00 PM.0 0/5 * * * ?-> Run every 5 minutes.
🧩 The Analogy: The Calendar
It’s like telling your assistant: “Only call me on the third Tuesday of every month if it’s not a holiday.”
🥅 Module 3 Review
- Job: The task itself.
- Trigger: The timer that starts the job.
- Cron: The language for defining schedules.
- Persistence: Quartz can save your schedule to a database so it survives a server restart.