Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as support for JTA transactions and clustering.
Quartz is freely usable, licensed under the Apache 2.0 license.
Please read our overview for additional quick information.
Quartz let's you add scheduling to your Java application with ease. Learning just a few concepts will get you started.
Your project will need (at least) the Quartz core library, named quartz-x.y.z.jar (where x.y.z is a version number), in its classpath.
Quartz has few dependencies, but always requires at least the sl4j-api jar file. If you want to store your scheduling data in a database then you will also need the c3p0 library. If needed, these libraries can be found in the Quartz distribution.
You may also want the quartz-jobs jar file on your classpath - if you want to use any of the jobs "built into" quartz.
You can download the full distribution (with examples, source, dependencies, doc, etc.).
Or if you are a Maven user you can add the dependencies (only the first below is required):
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
Schedulers are created by factories.
Schedulers can be immediately used to schedule jobs, but they will not start executing any until the .start() method has been called.
// Grab the Scheduler instance from the Factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// and start it off
scheduler.start();
The Quartz API is designed to be used as fluent domain-specific language (DSL). This can be done most cleanly if you import the API components statically:
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
Once you have done that, you can implement Jobs - which have an .execute(..) method.
public class MyJob implements org.quartz.Job {
public MyJob() {
}
public void execute(JobExecutionContext context) throws JobExecutionException {
System.err.println("Hello World! MyJob is executing.");
}
}
And then schedule those jobs with triggers that define at what time(s) the job should run.
// define the job and tie it to our MyJob class
JobDetail job = newJob(MyJob.class)
.withIdentity("job1", "group1")
.build();
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
Read the user documentation to learn more about Quartz concepts.