Home » Enterprise Java » Quartz » Quartz cron schedule Example

About Theodora Fragkouli

Theodora Fragkouli
Theodora has graduated from Computer Engineering and Informatics Department in the University of Patras. She also holds a Master degree in Economics from the National and Technical University of Athens. During her studies she has been involved with a large number of projects ranging from programming and software engineering to telecommunications, hardware design and analysis. She works as a junior Software Engineer in the telecommunications sector where she is mainly involved with projects based on Java and Big Data technologies.

Quartz cron schedule Example

This is an example of how to create and run scheduled jobs using cron expressions and Quartz Scheduler, the open source, Java library for job scheduling. The Quartz Scheduler can create schedules for executing jobs in an application and may also include features like support for JTA transactions and clustering. It is used for system maintenance needs, or it can drive the workflow of a procedure.

Its basic components are the Job, that represents a job to be scheduled, the Scheduler of the job and the Trigger, that defines the schedule upon which a given job will be executed.

Cron is a UNIX tool with well known scheduling capabilities. Cron expressions provide the ability to specify complex time combinations such as “At 8:00am every Monday through Friday”. In order to schedule a job using a cron expression, we can make use of the CronTrigger class, which is used to fire a job at given moments in time, defined with Unix ‘cron-like’ schedule definitions. Below, we will create and fire some simple custom jobs, making use of a CronTrigger and some cron expressions.

Tip
You may skip project creation and jump directly to the beginning of the example below.

Our preferred development environment is Eclipse. We are using Eclipse Juno (4.2) version, along with Maven Integration plugin version 3.1.0. You can download Eclipse from here and Maven Plugin for Eclipse from here. The installation of Maven plugin for Eclipse is out of the scope of this tutorial and will not be discussed. We are also using JDK 7_u_21.

Let’s begin,

1. Create a new Maven project

Go to File -> Project ->Maven -> Maven Project.

New Maven Project

New Maven Project – step 1

In the “Select project name and location” page of the wizard, make sure that “Create a simple project (skip archetype selection)” option is checked, hit “Next” to continue with default values.

Maven-Project-Name-Location

New Maven project- step 2

In the “Enter an artifact id” page of the wizard, you can define the name and main package of your project. We will set the “Group Id” variable to "com.javacodegeeks.snippets.enterprise" and the “Artifact Id” variable to "quartzexample". The aforementioned selections compose the main project package as "com.javacodegeeks.snippets.enterprise.quartzexample" and the project name as "quartzexample". Hit “Finish” to exit the wizard and to create your project.

quartz project

quartz project

The Maven project structure is shown below:

quartz project structure

quartz project structure

    It consists of the following folders:

  • /src/main/java folder, that contains source files for the dynamic content of the application,
  • /src/test/java folder contains all source files for unit tests,
  • /src/main/resources folder contains configurations files,
  • /target folder contains the compiled and packaged deliverables,
  • the pom.xml is the project object model (POM) file. The single file that contains all project related configuration.

2. Add quartz dependency

Add the quartz dependency in Maven’s pom.xml file, by editing it at the “Pom.xml” page of the POM editor, as shown below:
 
pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javacodegeeks.snippets.enterprise</groupId>
	<artifactId>quartzexample</artifactId>
	<version>0.0.1-SNAPSHOT</version>

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

</project>

As you can see Maven manages library dependencies declaratively. A local repository is created (by default under {user_home}/.m2 folder) and all required libraries are downloaded and placed there from public repositories. Furthermore intra – library dependencies are automatically resolved and manipulated.

3. Create the job

Every custom job shown below must implement the Job interface. It overrides the execute(JobExecutionContext context) method. When the Job‘s trigger fires, this method is invoked by one of the Scheduler‘s worker threads. The JobExecutionContext object that is passed to this method provides the job instance with a handle to the scheduler that executed it, a handle to the trigger that triggered the execution and the job’s JobDetail object.

All jobs type a hello message with the time.

Job1.java

package com.javacodegeeks.snippets.enterprise.quartzexample.job;

import java.util.Date;

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

public class Job1 implements Job{ 
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("Job1 --->>> Hello geeks! Time is " + new Date());
		} 
}

Job2.java

package com.javacodegeeks.snippets.enterprise.quartzexample.job;

import java.util.Date;

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

public class Job2 implements Job{ 
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("Job2 --->>> Hello geeks! Time is " + new Date());
		} 
}

Job3.java

package com.javacodegeeks.snippets.enterprise.quartzexample.job;

import java.util.Date;

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

public class Job3 implements Job{ 
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("Job3 --->>> Hello geeks! Time is " + new Date());
		} 
}

Job4.java

package com.javacodegeeks.snippets.enterprise.quartzexample.job;

import java.util.Date;

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

public class Job4 implements Job{ 
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("Job4 --->>> Hello geeks! Time is " + new Date());
		} 
}

Job5.java

package com.javacodegeeks.snippets.enterprise.quartzexample.job;

import java.util.Date;

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

public class Job5 implements Job{ 
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("Job5 --->>> Hello geeks! Time is " + new Date());
		} 
}

4. Create the Scheduler and the Trigger

MyApp.java class will be used to execute all 5 jobs. For every job it has a JobDetail object, which is used to create the job instance. The JobBuilder instantiates the job:

  • newJob(Class<? extends Job> jobClass) method sets the java class representing the job which is being built.
  • withIdentity(String name) method sets a JobKey with the given name and default group to identify the JobDetail.
  • build() method builds the job.

Every job also needs a trigger. The CronTrigger class uses the TriggerBuilder.newTrigger() to create the trigger instance, and the withIdentity(String name, String group) method to set a triggerKey with the given name and a group to identify the Trigger.

  • The trigger of the Job1.java class uses the withSchedule(CronScheduleBuilder.cronSchedule(String cronExpression)) method, which creates a CronScheduleBuilder set to repeat every 5 seconds.
  • The trigger of the Job2.java class uses the withSchedule(CronScheduleBuilder.cronSchedule(CronExpression cronExpression)) method, that creates a CronScheduleBuilder with a new CronExpression set to repeat every 7 seconds.
  • The trigger of the Job3.java class uses the withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(int hour, int minute)) method, that sets the schedule to fire every day at the given time (13:46).
  • The trigger of the Job4.java class uses the withSchedule(CronScheduleBuilder.weeklyOnDayAndHourAndMinute(int dayOfWeek, int hour, int minute)) method, that sets the schedule to fire one per week on the given day at the given time (the third day, at 13:46).
  • The trigger of the Job4.java class uses the withSchedule(CronScheduleBuilder.monthlyOnDayAndHourAndMinute(int dayOfMonth, int hour, int minute)) method, that sets the schedule to fire one per month on the given day of month at the given time (the 28th of the month at 13:46).

The build() method is used to build each Trigger.

Finally, a scheduler is also created for every job, using new StdSchedulerFactory().getScheduler(). The scheduler starts the threads that fire the Trigger with the start() method and adds the given JobDetail and associates the given Trigger with it, using scheduleJob(JobDetail jobDetail, Trigger trigger).

MyApp.java

package com.javacodegeeks.snippets.enterprise.quartzexample;

import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.javacodegeeks.snippets.enterprise.quartzexample.job.Job1;
import com.javacodegeeks.snippets.enterprise.quartzexample.job.Job2;
import com.javacodegeeks.snippets.enterprise.quartzexample.job.Job3;
import com.javacodegeeks.snippets.enterprise.quartzexample.job.Job4;
import com.javacodegeeks.snippets.enterprise.quartzexample.job.Job5;

public class MyApp {

	public static void main(String[] args) {
		try {
			JobDetail job1 = JobBuilder.newJob(Job1.class)
					.withIdentity("job1", "group1").build();

			Trigger trigger1 = TriggerBuilder.newTrigger()
					.withIdentity("cronTrigger1", "group1")
					.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
					.build();
			
			Scheduler scheduler1 = new StdSchedulerFactory().getScheduler();
			scheduler1.start();
			scheduler1.scheduleJob(job1, trigger1);

			JobDetail job2 = JobBuilder.newJob(Job2.class)
					.withIdentity("job2", "group2").build();
			
			Trigger trigger2 = TriggerBuilder.newTrigger()
					.withIdentity("cronTrigger2", "group2")
					.withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0/7 * * * * ?")))
					.build();
			
			Scheduler scheduler2 = new StdSchedulerFactory().getScheduler();
			scheduler2.start();
			scheduler2.scheduleJob(job2, trigger2);

			JobDetail job3 = JobBuilder.newJob(Job3.class)
					.withIdentity("job3", "group3").build();
			
			Trigger trigger3 = TriggerBuilder.newTrigger()
					.withIdentity("cronTrigger3", "group3")
					.withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(13, 46))
					.build();
			
			Scheduler scheduler3 = new StdSchedulerFactory().getScheduler();
			scheduler3.start();
			scheduler3.scheduleJob(job3, trigger3);

			JobDetail job4 = JobBuilder.newJob(Job4.class)
					.withIdentity("job4", "group4").build();
			
			Trigger trigger4 = TriggerBuilder.newTrigger()
					.withIdentity("cronTrigger4", "group4")
					.withSchedule(CronScheduleBuilder.weeklyOnDayAndHourAndMinute(3, 13, 46))
					.build();
			
			Scheduler scheduler4 = new StdSchedulerFactory().getScheduler();
			scheduler4.start();
			scheduler4.scheduleJob(job4, trigger4);

			JobDetail job5 = JobBuilder.newJob(Job5.class)
					.withIdentity("job5", "group5").build();
			
			Trigger trigger5 = TriggerBuilder
					.newTrigger().withIdentity("cronTrigger5", "group5")
					.withSchedule(CronScheduleBuilder.monthlyOnDayAndHourAndMinute(28, 13, 46))
					.build();	
			
			Scheduler scheduler5 = new StdSchedulerFactory().getScheduler();
			scheduler5.start();
			scheduler5.scheduleJob(job5, trigger5);
			
			Thread.sleep(100000);
			
			scheduler1.shutdown();
			scheduler2.shutdown();
			scheduler3.shutdown();
			scheduler4.shutdown();
			scheduler5.shutdown();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

5. Run the application

If you run MyApp.java class, the result will be something like the output below:

As you can see, the Job1.java is executed every 5 seconds, Job2.java every 7 seconds and the rest of the jobs are only executed at the specified time.

Note that job4 does not get executed, since it will be triggered on the third day of the week, which is Tuesday (Today it’s Wednesday!)

Output

Job1 --->>> Hello geeks! Time is Wed Jan 28 13:45:55 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:45:56 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:00 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:00 EET 2015
Job3 --->>> Hello geeks! Time is Wed Jan 28 13:46:00 EET 2015
Job5 --->>> Hello geeks! Time is Wed Jan 28 13:46:00 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:05 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:07 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:10 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:14 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:15 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:20 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:21 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:25 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:28 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:30 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:35 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:35 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:40 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:42 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:45 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:49 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:50 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:46:55 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:46:56 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:47:00 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:47:00 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:47:05 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:47:07 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:47:10 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:47:14 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:47:15 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:47:20 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:47:21 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:47:25 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:47:28 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:47:30 EET 2015
Job1 --->>> Hello geeks! Time is Wed Jan 28 13:47:35 EET 2015
Job2 --->>> Hello geeks! Time is Wed Jan 28 13:47:35 EET 2015

6. Download the Eclipse Project

This was an example of how to schedule jobs with the cron scheduler and trigger of Quartz.

Download
You can download the full source code of this example here: QuartzCronScheduleExample.zip
(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Nemo Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Nemo
Guest
Nemo

Thanks!