Configure Quartz Scheduler Jobs using XML File

In this tutorial I would explain how to configure quartz scheduler using properties file and xml file. Properties file will be used for configuring the JobStore details and XML configuration path. XML will store the list of Job details and cron triggers. In my previous post I have explained how to setup quartz scheduler pro-grammatically. However, in web application or any huge applications having multiple jobs to be configured, correct approach is to use the XML configuration file. Lets look at the below example and post your questions in the comments section.

Required JAR Files

Add the following JSR files to the classpath before start running the below example application.

  • quartz-2.2.1.jar
  • quartz-jobs-2.2.1.jar
  • slf4j-api-1.7.5.jar
  • jta-1.1.jar

1. Create A Job


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

public class FirstJob implements Job{

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


2. Create Properties File

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = javabeat/net/quartz/quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true

3. Create XML File with Quartz Job and Cron Trigger


<?xml version="1.0" encoding="UTF-8"?>

			<description>This is FirstJob</description>
				<!-- It will run every 2 seconds -->
				<cron-expression>0/2 * * * * ?</cron-expression>

4. Run the Quartz Application


import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

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

If you run the above example, it would work perfectly and job is invoked for every 2 seconds. Hope this helps you to understand how to configure quartz scheduler using XML file.

5. Why JTA JAR file?

You may get the following exception if you are not adding the jta.jar file to the classpath when running the above application. You can get these jar file from here. It looks like quartz libraries are internally using this JSr file for managing the transactions.

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
	at java.lang.Class.getDeclaredMethods0(Native Method)
	at java.lang.Class.privateGetDeclaredMethods(
	at java.lang.Class.privateGetPublicMethods(
	at java.lang.Class.getMethods(
	at java.beans.Introspector.getPublicDeclaredMethods(
	at java.beans.Introspector.getTargetMethodInfo(
	at java.beans.Introspector.getBeanInfo(
	at java.beans.Introspector.getBeanInfo(
	at org.quartz.impl.StdSchedulerFactory.setBeanProps(
	at org.quartz.impl.StdSchedulerFactory.instantiate(
	at org.quartz.impl.StdSchedulerFactory.getScheduler(
Caused by: java.lang.ClassNotFoundException: javax.transaction.UserTransaction
	at Method)
	at java.lang.ClassLoader.loadClass(
	at sun.misc.Launcher$AppClassLoader.loadClass(
	at java.lang.ClassLoader.loadClass(
	... 12 more

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!