Quartz Scheduler Tutorial

Schedulers are important for running the batch process in your application. There are plenty of Java scheduler frameworks available in the market. Among them, Quartz Scheduler is the most popular and most widely used by Java projects. This tutorial provides very simple example to write Quartz scheduler. When you complete reading this tutorial, you will be able to write your won quartz scheduler. If you have any questions, please write it in the comments section.

1. SetUp Quartz Scheduler

Get the quartz JSR files from here. Also you would need slf4j-api-1.7.5.jar to run the quartz examples. Quartz is internally using this API for the logging purposes. Add these files to the classpath and imprt to the eclipse environment. I assume that you have already created a Java project in eclipse.

2. Create A Quartz Job

As a first step, implement org.quartz.Job interface and write your own Job. You must override the method execute which will be invoked for the batch process. The below code snippet is the simple example for writing a Job.


package javabeat.net.quartz;

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

public class FirstJob implements Job{

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("This is my first quartz job!!");


3. Register Quartz Scheduler

This is the final step where we initialize the Quartz scheduler. The following are the steps to initialize this scheduler.

  1. Create scheduler factory which is required for getting the scheduler instance.
  2. Get the scheduler instance from the factory.
  3. Create the JobDetailImpl instance and register our FirstJob to that object.
  4. Create and set the values for trigger. Trigger is very important for setting the details about the frequecny of batch process.
  5. Start the scheduler
  6. Schedule the Job by linking our Job and trigger.
  7. Your Quartz scheduler is ready and running.


package javabeat.net.quartz;

import java.util.Date;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;

public class QuartzExample {
	public static void main(String args[]) throws SchedulerException{
		//Creating scheduler factory and scheduler
		SchedulerFactory factory = new StdSchedulerFactory();
		Scheduler scheduler = factory.getScheduler();

		//Creating Job and link to our Job class
		JobDetailImpl jobDetail = new JobDetailImpl();
		jobDetail.setName("First Job");

		//Creating schedule time with trigger
		SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
		simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));

		//Start scheduler

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!