Java Task Scheduler: Quartz Scheduler — Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Java Task Scheduler: Quartz Scheduler



In modern Java development, there is often a need to perform certain actions on a schedule: sending email newsletters, archiving logs, synchronizing data, or generating reports. To solve these tasks, there is a powerful and flexible library Quartz Scheduler. This is an open-source framework that allows you to create, schedule, and manage tasks (Jobs) using triggers and cron expressions. Quartz easily integrates with Spring Boot, Java EE, and any standalone applications.



Key Quartz Features

  • Flexible scheduling — support for simple intervals (every N seconds/minutes) and complex cron expressions.
  • Persistence — storing task state in a database (JDBC JobStore) for recovery after application restart.
  • Clustering — distributing tasks across multiple nodes for fault tolerance.
  • Parallel execution — the ability to run multiple tasks simultaneously.
  • Missed firings — configuring behavior during downtime (misfire instructions).


Installation and Setup

To use Quartz, add the dependency to 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'


Creating a Simple Task

Each task in Quartz is a class that implements the Job interface. Inside the execute() method, the business logic is written.

import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;

public class HelloJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello! Task executed at " + new java.util.Date()); }}


Configuring the Scheduler and Trigger

The Scheduler is created through the StdSchedulerFactory. Then a trigger is created — either a simple trigger (SimpleTrigger) or a cron-based one (CronTrigger).

import org.quartz.*;import org.quartz.impl.StdSchedulerFactory;

public class QuartzExample { public static void main(String[] args) throws SchedulerException { // 1. Get the scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

// 2. Define the job JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity("myJob", "group1") .build();

// 3. Create a trigger — run every 10 seconds Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .repeatForever()) .build();

// 4. Bind the job to the trigger and start the scheduler scheduler.scheduleJob(job, trigger); scheduler.start(); }}


Using Cron Expressions

Cron expressions allow you to set complex schedules. Format: seconds minutes hours day-of-month month day-of-week.

// Run every day at 18:30CronScheduleBuilder cronSchedule = CronScheduleBuilder        .dailyAtHourAndMinute(18, 30);

// Or manually: "0 30 18 * * ?" (every day at 18:30)Trigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity("cronTrigger", "group1") .withSchedule(CronScheduleBuilder.cronSchedu

Recommendations