Skip to content

Quartz

Developer UtilitiesScheduler / Job ManagementJava

What it is

Quartz is a powerful and widely used Java library for scheduling and executing jobs. It allows developers to define tasks, schedule them with cron-like expressions or fixed intervals, and manage job execution in enterprise applications.

Quartz provides an API to define jobs by implementing the `Job` interface, schedule them using `Trigger`s, and execute them through a `Scheduler`. Jobs can be scheduled at specific times, repeated at intervals, or defined using cron expressions. Quartz also supports persistent storage and clustering for distributed applications.

Installation

<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.2</version> </dependency>

Getting started

The smallest useful thing you can do with it, and what each part means.

Defining and executing a simple job
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class HelloJob implements Job {
    public void execute(JobExecutionContext context) {
        System.out.println("Hello, Quartz!");
    }
}

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
JobDetail job = JobBuilder.newJob(HelloJob.class)
    .withIdentity("myJob", "group1")
    .build();
Trigger trigger = TriggerBuilder.newTrigger()
    .withIdentity("myTrigger", "group1")
    .startNow()
    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever())
    .build();
scheduler.scheduleJob(job, trigger);
Defines a simple job that prints a message and schedules it to run every 10 seconds indefinitely.

Advanced usage

Where the library earns its place over a simpler alternative.

Using a CronTrigger
Trigger cronTrigger = TriggerBuilder.newTrigger()
    .withIdentity("cronTrigger", "group1")
    .withSchedule(CronScheduleBuilder.cronSchedule("0 0/5 * * * ?")) // every 5 minutes
    .build();
scheduler.scheduleJob(job, cronTrigger);
Schedules a job using a cron expression to execute every 5 minutes.
JobDataMap for passing parameters
JobDetail job = JobBuilder.newJob(HelloJob.class)
    .usingJobData("name", "Quartz User")
    .build();
// In job execute method: String name = context.getJobDetail().getJobDataMap().getString("name");
Passes data to a job at execution time using a JobDataMap.
Persistent job store
// Configure quartz.properties to use JDBCJobStore for persistence
// This ensures jobs survive application restarts
Quartz can persist jobs and triggers in a relational database for reliability in production.
Listeners for monitoring jobs
scheduler.getListenerManager().addJobListener(new JobListener() {
    public String getName() { return "MyListener"; }
    public void jobToBeExecuted(JobExecutionContext context) {}
    public void jobExecutionVetoed(JobExecutionContext context) {}
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        System.out.println("Job executed: " + context.getJobDetail().getKey());
    }
});
Adds a job listener to monitor job execution events.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

SchedulerException
Occurs if the scheduler fails to start, schedule a job, or shut down. Check configuration and job definitions.
JobExecutionException
Thrown within a job when execution fails. Can be used to trigger retries or rescheduling.
ObjectAlreadyExistsException
Thrown when trying to schedule a job or trigger with an existing identity. Use unique names or groups.

Best practices

  • Use meaningful job and trigger names and groups for better management.
  • Consider persistent job stores for critical tasks that must survive restarts.
  • Handle exceptions within jobs to prevent scheduler disruption.
  • Use cron triggers carefully to avoid overlapping executions.
  • Monitor scheduler performance and consider clustering for high availability.

Background

Why it exists, and what it was reacting to.

Quartz was created to provide a robust, feature-rich scheduling library for Java applications. It supports simple schedules, cron schedules, persistent job stores, clustering, and job listeners. Quartz is used in applications ranging from batch processing to workflow automation, where timed or repeated task execution is required.