ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

What Is Quartz
Pages: 1, 2, 3, 4, 5

Getting Your Hands on Quartz

The Quartz project is hosted on OpenSymphony's site. On the Quartz site, you will find all of the usual suspects: JavaDocs, documentation including tutorials, CVS access, links to the user and developer forums, and, of course, the downloads.

Grab the distribution from the download link and unzip it to a local directory. The download includes a prebuilt Quartz binary that you can drop into your application. The framework requires very few third-party libraries, and those that are required, you are probably already using anyway.

From the distribution, you'll want to add the third-party libraries from the <quartz- install>/lib/core and <quartz-install>/lib/optional directories to your project. Most of these libraries are the standard Jakarta Commons libraries that we all know and love like Commons Logging, Commons BeantUtils, and so on.

The quartz.properties File

Quartz includes a configuration file called quartz.properties, which allows you to modify the runtime environment of the framework. By default, the version of this file contained within the Quartz binary is used. You should create a copy of this file and put it in your classes directory so that it can be found by the class loader. A sample quartz.propeties file is shown in Example 1.

Example 1. The quartz.properties file allows you modify the Quartz runtime:

# Configure Main Scheduler Properties  

org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO

# Configure ThreadPool  

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount =  5
org.quartz.threadPool.threadPriority = 5

# Configure JobStore  

org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

Once you have the Quartz binary and the third-party libraries added to your project, and the quartz.properties in the classes directory, it's time to create some Jobs. Before we do, however, let's take a short detour and talk briefly about the Quartz architecture.

Inside the Quartz Architecture

In terms of size, Quartz is comparable to most other open source frameworks. It contains approximately 300 Java Classes and Interfaces organized into roughly 12 packages. This compares to approximately 325 Classes and Interfaces and 11 packages in Apache Struts. Although size is rarely a characteristic used to determine the quality of a framework, the point here is that there's a lot of functionality included within Quartz, and functionality and feature set is, and should be, one factor used to judge the quality of a framework, open source or not.

The Quartz Scheduler

At the heart of the Quartz framework is the Scheduler. The Scheduler is responsible for managing the runtime environment for your Quartz application. It doesn't do all of the work by itself, but depends on some very important components within the framework. Quartz is very much about threads and thread management. To ensure scalability, Quartz is based on a multithreaded architecture. When started, the framework initializes a set of "worker threads" that are used by the Scheduler to execute the scheduled Jobs. This is how Quartz is able to run many Jobs concurrently. Quartz relies on a loosely coupled set of ThreadPool management components to manage the thread environment. We'll mention it several times throughout the article, but just about everything in Quartz is either configurable or customizable. So, for example, if you wanted to "plugin" your own ThreadPool management facilities, guess what, you can!

Jobs, Jobs, and More Jobs

In Quartz lingo, a Job is simply a Java class that performs a task. This task can be anything that you can code in Java. The only requirement is that you implement the org.quartz.Job interface and throw a JobExecutionException in the case of a serious error. You saw in the beginning that the Job interface contains a single method, execute().

Once you implement the Job interface and implement the execute() method, Quartz will invoke your Job when it determines it's time for the Job to run. What you do inside the execute() method is entirely up to you. Here are a few examples of things you might want to do inside a Job:

  • Use JavaMail (or another Mail framework like Commons Net) to send emails

  • Create a remote interface and invoke a method on an EJB

  • Get a Hibernate Session, and query and update data in a relational database

  • Use OSWorkflow and invoke a workflow from the Job

  • Use FTP and move files around

  • Call an Ant build script to kick off scheduled builds

The possibilities are endless and that's what makes the framework so powerful. Quartz provides you with the mechanism to establish a very granular, repeatable schedule, and then you just create Java classes that get called to execute.

Pages: 1, 2, 3, 4, 5

Next Pagearrow