• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Configure Quartz Scheduler Jobs using XML File

November 8, 2013 //  by Krishna Srinivasan//  Leave a Comment

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

Category: JavaTag: Quartz Scheduler

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.

Previous Post: « How to Write Quartz JobListener?
Next Post: Circular Dependency in Spring »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact