How does the quartz scheduler work?

Abhishek Jain picture Abhishek Jain · Oct 16, 2012 · Viewed 9.3k times · Source

My question is : How does the quartz scheduler work and how is it different from a normal class implementing the Runnable interface (basically a thread) which wakes up according to the specified time interval and performs the required job?

Is it just a convenient way of doing things (using the quartz scheduler) like specifying the job configuration through an XML files and easy addition of new jobs to an existing scheduler or is there something more to it? By more, I mean does it do any sort of optimizations such as it does not hang on to the thread for the entire duration and releases it? Is it a polling kind of mechanism where the thread keeps polling the system time and sees whether the specified time interval has elapsed or does it do some kind of registration with the system clock where the clock itself notifies the quartz scheduler?

Please let me know if any further clarification is required on the question above.

Answer

Abhishek Jain picture Abhishek Jain · Oct 16, 2012

After posting the question, I browsed through some content on the web and found some useful insights into the same. Sorry for posting a question and answering it myself, but it would be useful for anybody else who may like to understand the same.

Here are the benefits of Quartz and its comparison with the usual Java timer interface:

  • Quartz is quite flexible, and contains multiple usage paradigms that can be used separately or together, in order to achieve your desired behavior, and enable you to write your code in the manner that seems most 'natural' to your project.
  • Quartz is very light-weight, and requires very little setup/configuration - it can actually be used 'out-of-the-box' if your needs are relatively basic.
  • Quartz is fault-tolerant, and can persist ('remember') your scheduled jobs between system restarts.

On the other hand, it overcomes the following problems in the Timer interface:

  • Timers have no persistence mechanism.
  • Timers have inflexible scheduling (only able to set start-time & repeat interval, nothing based on dates, time of day, etc.)
  • Timers don't utilize a thread-pool (one thread per timer)
  • Timers have no real management schemes - you'd have to write your own mechanism for being able to remember, organize and retrieve your tasks by name, etc.

If anybody would like to add any info to the above, please feel free to do so.