mjn
mjn

Reputation: 36644

Quartz vs. ScheduledExecutorService in Java web application

For a system monitoring Java application which currently runs on the command line and uses ScheduledExecutorService, I would like to write a simple web application version, to be run in a Servlet container like Apache Tomcat or Eclipse Jetty.

I have read about Quartz as one of the popular job schedulers for web applications. Would it be better (maybe because of better servlet container integration) to port this application from ScheduledExecutorService to Quartz?

Adding another library dependency to the application is not a problem, I am interested in technical reasons against usage of ScheduledExecutorService.

Upvotes: 19

Views: 8853

Answers (3)

Matthew Kuraja
Matthew Kuraja

Reputation: 401

Java's Executor solution allows you to either:

  1. immediately run a task
  2. start a task after an initial delay (and optionally rerun the task after subsequent delay cycles).

But Quartz empowers you with incredible flexibility on when and how often to run a task/job. For example, one schedule during the Mon-Fri work week and something else (or not at all) during the weekends. Or on the last day of the month and you don't have to figure out if a given month's last day is on the 28th, 29th, 30th, or 31st. Here's some more examples of the flexibility the cron style scheduling accommodates - http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html#examples

Using Java's library is easier but for anyone that wants a jump start into a bare-bones codebase example of Quartz working, I've put this template together for free download usage - https://github.com/javateer/quartz-example

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533492

It depends on what you are using it for.

Quartz is useful for programmed times e.g. every hour on the hour.

ScheduledExecutorService is useful for repeating tasks which don't have to occur at a specific time. Its simpler and possibly more efficient. If you have this working it indicates to me that you don't need Quartz.

Upvotes: 14

cherouvim
cherouvim

Reputation: 31903

ScheduledExecutorService operates at a lower level and you'd have to implement all scheduling monitoring/maintenance facilities yourself.

Quartz has tons of facilities such as Job Persistence, Transactions, Clustering etc.

Upvotes: 10

Related Questions