Language: Java
Scheduler / Job Management
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.
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.
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>implementation 'org.quartz-scheduler:quartz:2.3.2'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.
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.
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.
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.
// Configure quartz.properties to use JDBCJobStore for persistence
// This ensures jobs survive application restartsQuartz can persist jobs and triggers in a relational database for reliability in production.
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.
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.