Job Scheduling with Quartz

September 1, 2006 · Print This Article

I’ve recently become enamored with the Quartz job scheduling framework from OpenSymphony. While Quartz can be used in many types of applications, I ran across it looking for a solution to fire scheduled events in web applications. I wanted a Cron replacement that allowed me to instantiate and run any Java class in my application.

Before Quartz, I relied primarily on Caucho Resin’s runat tag to fire events. By adding a runat tag to the application’s web.xml file, you can call a servlet extending GenericServlet. Using Timers or application server-specific solutions is another option, but for the current project, I really wanted to start out with Tomcat, and I wanted the option to easily migrate to an application server if the need arose.

Quartz just works, and integrating it with an existing project was a snap. I approach most new tools and frameworks cautiously. The feature rich, easy to configure, and mature trifecta doesn’t come along as often as one would like, and I’ve been continually impressed with how carefully executed Quartz is.

There are three pieces that make a Quartz application: jobs, triggers, and the scheduler.

  • Jobs: contain the logic or processing that you want to perform.
  • Triggers: determine when jobs are run.
  • Scheduler: the conductor that coordinates jobs and triggers.

Since I’ve started using Quartz, I’ve offloaded much of the on-demand processing into jobs of all sorts. Some jobs are used to perform routine maintenance, in a Ronco set it and forget it fashion, e.g. backups. Some jobs are used to generate pseudo-dynamic content, by running frequently in the background and generating static pages. This takes load off of the server, and conveniently aids in search engine spidering. Some jobs are traditional batch processing. For example, in the current project, a lot of geocoding is performed. Batching these requests, and running them every hour is much more efficient than firing them off on demand.

Adding to the flexibility of Quartz is the ability to schedule jobs both programmatically and declaratively, the ability to use listeners, the ability to persist jobs in a JobStore, and very versatile triggers.

Quartz is most definitely worth a look-see. The latest stable release is 1.5.2, and 1.6.0 alpha was recently released. Both can be download via OpenSymphony.

Comments

Got something to say?