Quick Quartz 2.0 Tutorial

This is a quick Quartz 2.0 tutorial that uses the DSL/builder-based API. You will need Eclipse and the Eclipse m2e Plugin to follow the steps below.

1. Go to Eclipse and create a new Maven project.

Create a Maven Project

2. Click “Create a simple project” and hit Next. On the next page, enter Group Id as “com.teamextension”, Artifact Id as “quickquartz” and Name as “quickquartz”.

Configure Maven Project and hit Finish.

3. Right click on the project and go to Maven -> Add Dependency. Enter “quartz-scheduler” in the search box and pick “org.quartz-scheduler” version 2.0.2.

Add Dependency

We will also add SLF4J as a dependency so you can see the Quartz logs. Enter “slf4j-simple” in the search box and pick “org.slf4j slf4j-simple” version 1.6.1.

SLF4J

4. Create 2 classes com.teamextension.QuickQuartz and com.teamextension.QuickQuartzJob.

package com.teamextension;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.ScheduleBuilder;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuickQuartz {
	public static void main(String[] args) throws SchedulerException, InterruptedException {
		// start the Scheduler
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		scheduler.start();

		// create the Job
		JobDetail job = JobBuilder.newJob(QuickQuartzJob.class).
				withIdentity("QuickQuartzJob").build();

		// create the Schedule, run every 5 seconds
		ScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.
				simpleSchedule().
				withIntervalInSeconds(5).
				repeatForever();

		// create the Trigger
		Trigger trigger = TriggerBuilder.
				newTrigger().
				withIdentity("QuickQuartzTrigger").
				withSchedule(scheduleBuilder).
				startNow().build();

		// schedule the Job
		scheduler.scheduleJob(job, trigger);

		// run for 30 seconds and exit
		Thread.sleep(1000 * 30);
		scheduler.shutdown();
	}
}

The main class starts the Scheduler, and creates a job and trigger. The schedule in the example is set to every 5 seconds. We also sleep for 30 seconds to give time for the job to run.

package com.teamextension;

import java.util.Date;

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

public class QuickQuartzJob implements Job {
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println(new Date());
	}
}

The job simply prints the current time to System.out.

Run the application and you should see the current date printed out a few times before the program exits.

For more in-depth information, read what’s new with Quartz 2.0, Quartz Cookbook and the official Quartz Tutorial.


2 Comments

  1. subesh says:

    My Quartz configuration was successfull,
    config : jbdcstore

    When i execute below code

    CronTrigger ct = new CronTrigger(“cronTrigger”,”group2″,”0 0/5 * * * ?”);
    scheduler.scheduleJob(jd,ct);

    But I am not able to see details in Quartz tables.

  2. arun says:

    when i schedule the job for particular cron schedule it prints like this

    batch acquisition of 0 triggers

    it means what pls explain


Leave a Reply

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