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

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 context) throws JobExecutionException {
		System.out.println("This is my first quartz job!!");
	}

}

2. Create Properties File

quartz.properties

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

quartz-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
	xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData

http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"

	version="1.8">

	<schedule>
		<job>
			<name>FirstJob</name>
			<group>DummyGroup</group>
			<description>This is FirstJob</description>
			<job-class>javabeat.net.quartz.FirstJob</job-class>
		</job>
		<trigger>
			<cron>
				<name>dummyTrigger</name>
				<job-name>FirstJob</job-name>
				<job-group>GroupDummy</job-group>
				<!-- It will run every 2 seconds -->
				<cron-expression>0/2 * * * * ?</cron-expression>
			</cron>
		</trigger>
	</schedule>
</job-scheduling-data>

4. Run the Quartz Application

QuartzXmlExample.java

package javabeat.net.quartz;

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(
				"javabeat/net/quartz/quartz.properties");
		Scheduler scheduler = factory.getScheduler();
		// Start scheduler
		scheduler.start();
	}
}

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(Class.java:2451)
	at java.lang.Class.privateGetPublicMethods(Class.java:2571)
	at java.lang.Class.getMethods(Class.java:1429)
	at java.beans.Introspector.getPublicDeclaredMethods(Introspector.java:1261)
	at java.beans.Introspector.getTargetMethodInfo(Introspector.java:1122)
	at java.beans.Introspector.getBeanInfo(Introspector.java:414)
	at java.beans.Introspector.getBeanInfo(Introspector.java:161)
	at org.quartz.impl.StdSchedulerFactory.setBeanProps(StdSchedulerFactory.java:1393)
	at org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1057)
	at org.quartz.impl.StdSchedulerFactory.getScheduler(StdSchedulerFactory.java:1519)
	at javabeat.net.quartz.QuartzXmlExample.main(QuartzXmlExample.java:13)
Caused by: java.lang.ClassNotFoundException: javax.transaction.UserTransaction
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 12 more

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. Ronald Correa says:

    Thank you. Work perfectly

  2. B Pavan Kumar says:

    Getting the following exception, Can you please help me with this?
    Failed to instantiate SLF4J LoggerFactory
    Reported exception:
    java.lang.NoClassDefFoundError: org/apache/log4j/Level
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:107)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:295)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
    at org.quartz.impl.StdSchedulerFactory.(StdSchedulerFactory.java:303)
    at javabeat.net.quartz.QuartzXMLExample.main(QuartzXMLExample.java:11)
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 7 more
    Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/log4j/Level
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:107)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:295)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
    at org.quartz.impl.StdSchedulerFactory.(StdSchedulerFactory.java:303)
    at javabeat.net.quartz.QuartzXMLExample.main(QuartzXMLExample.java:11)
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 7 more

Speak Your Mind

*