Java Task Scheduler: Quartz Scheduler
In modern Java development, there is often a need to execute code on a schedule: sending email newsletters, clearing cache, generating reports, or synchronizing data. For these purposes, there is a powerful library Quartz Scheduler — the de facto standard for task scheduling in the Java ecosystem.
Quartz Scheduler is an open-source framework that allows you to create, schedule, and manage tasks (Jobs) using a flexible trigger system. It supports both simple delays and complex cron schedules, is fault-tolerant, and can be integrated with Spring Boot, Jakarta EE, and other platforms.
Key Features
- Flexible Scheduling — support for simple triggers (SimpleTrigger) with delay and repetition, as well as cron triggers (CronTrigger) for complex schedules.
- Persistence — jobs and triggers can be stored in a database (JDBC JobStore), ensuring execution even after application restart.
- Clustering — the ability to run multiple scheduler instances with coordination via a database.
- Listeners — listening to job execution events (JobListener, TriggerListener) for logging or additional processing.
- Monitoring — built-in API for viewing current jobs, their status, and execution history.
Installation and Setup
To get started, add the dependency to your pom.xml (Maven):
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.2</version></dependency>For Gradle:
implementation 'org.quartz-scheduler:quartz:2.3.2'Example of Creating a Simple Job
Let's create a class that implements the Job interface. This is our task:
import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import java.time.LocalTime;
public class SimpleJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Job executed at: " + LocalTime.now()); // Your business logic here }}Scheduling a Job with SimpleTrigger
Let's run the job after 5 seconds and repeat it every 10 seconds:
import org.quartz.*;import org.quartz.impl.StdSchedulerFactory;
public class SimpleSchedulerExample { public static void main(String[] args) throws SchedulerException { // 1. Create the scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// 2. Define the job (JobDetail) JobDetail job = JobBuilder.newJob(SimpleJob.class) .withIdentity("myJob", "group1") .build();
// 3. Create the trigger: start after 5 seconds, repeat every 10 seconds Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1") .startAt(DateBuilder.futureDate(5, DateBuilder.IntervalUnit.SECOND)) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .repeatForever()) .build();
// 4. Assign the job to the trigger and start scheduler.scheduleJob(job, trigger); scheduler.start(); }}Using CronTrigger for Complex Schedules
Cron expressions allow you to set a schedule of any complexity. For example, every day at 9:30 AM:
Trigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity("cronTrigger", "group1")