Contents | ‹ Prev | Next ›

Example 3 - Cron-based Triggers


This example is designed to demonstrate how you can use Cron Triggers to schedule jobs. This example will fire off several simple jobs that say “Hello World” and display the date and time that the job was executed.

The program will perform the following actions:

Note: Refer to the Quartz javadoc for a thorough explanation of CronTrigger.

Running the Example

This example can be executed from the examples/example3 directory. There are two out-of-the-box methods for running this example

The Code

The code for this example resides in the package org.quartz.examples.example3.

The code in this example is made up of the following classes:

Class Name Description
CronTriggerExample The main program
SimpleJob A simple job that says Hello World and displays the date/time


SimpleJob is a simple job that implements the Job interface and logs a nice message to the log (by default, this will simply go to the screen). The current date and time is printed in the job so that you can see exactly when the job is run.

public void execute(JobExecutionContext context) throws JobExecutionException {
    String jobName = context.getJobDetail().getFullName();
    _log.info("SimpleJob says: " + jobName + " executing at " + new Date());


The program starts by getting an instance of the Scheduler. This is done by creating a StdSchedulerFactory and then using it to create a scheduler. This will create a simple, RAM-based scheduler.

SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();

Job #1 is scheduled to run every 20 seconds

JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1", "0/20 * * * * ?");
sched.addJob(job, true);

Job #2 is scheduled to run every other minute, starting at 15 seconds past the minute.

job = new JobDetail("job2", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger2", "group1", "job2", "group1", "15 0/2 * * * ?");
sched.addJob(job, true);

Job #3 is scheduled to every other minute, between 8am and 5pm (17 o’clock).

job = new JobDetail("job3", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger3", "group1", "job3", "group1", "0 0/2 8-17 * * ?");
sched.addJob(job, true);

Job #4 is scheduled to run every 20 seconds

job = new JobDetail("job4", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger4", "group1", "job4", "group1", "0 0/3 17-23 * * ?");
sched.addJob(job, true);

Job #5 is scheduled to run at 10am on the 1st and 15th days of the month

job = new JobDetail("job5", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger5", "group1", "job5", "group1", "0 0 10am 1,15 * ?");
sched.addJob(job, true);

Job #6 is scheduled to run every 30 seconds on Weekdays (Monday through Friday)

job = new JobDetail("job6", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger6", "group1", "job6", "group1", "0,30 * * ? * MON-FRI");
sched.addJob(job, true);

Job #7 is scheduled to run every 30 seconds on Weekends (Saturday and Sunday)

job = new JobDetail("job7", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger7", "group1", "job7", "group1", "0,30 * * ? * SAT,SUN");
sched.addJob(job, true);

The scheduler is then started.


To let the program have an opportunity to run the job, we then sleep for five minutes (300 seconds). The scheduler is running in the background and should fire off several jobs during that time.

Note: Because many of the jobs have hourly and daily restrictions on them, not all of the jobs will run in this example. For example: Job #6 only runs on Weekdays while Job #7 only runs on Weekends.

Thread.sleep(300L * 1000L);

Finally, we will gracefully shutdown the scheduler:


Note: passing true into the shutdown message tells the Quartz Scheduler to wait until all jobs have completed running before returning from the method call.