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.

FirstJob.java

package javabeat.net.quartz;

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

public class FirstJob implements Job{

	@Override
	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.

QuartzExample.java

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");
		jobDetail.setJobClass(FirstJob.class);

		//Creating schedule time with trigger
		SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
		simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
		simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
		simpleTrigger.setRepeatInterval(2000);
		simpleTrigger.setName("FirstTrigger");

		//Start scheduler
		scheduler.start();
		scheduler.scheduleJob(jobDetail,simpleTrigger);
	}
}

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Hi,
    I just want to point readers to QuartzDesk (http://www.quartzdesk.com). QuartzDesk is a advanced graphical frontend for the management and monitoring of Quartz schedulers, jobs and triggers running in all sorts of Java applications.
    Lite (free) license available as well as 30-day trial licenses for all non-free editions.

    Best regards,
    Jan Moravec
    QuartzDesk Project Founder

Speak Your Mind

*