18

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
mjn
  • 36,362
  • 28
  • 176
  • 378
  • Note that for periodic metrics/monitoring tasks, creating ["passage of time" events](http://verraes.net/2019/05/patterns-for-decoupling-distsys-passage-of-time-event/) in distributed systems, and the like you should be really careful to not interpret the ordinal number of the task run or the event as the time when it happened. Otherwise [clock drift](https://en.wikipedia.org/wiki/Clock_drift) problem could bite you. See [this blog post](https://medium.com/@leventov/cronscheduler-a-reliable-java-scheduler-for-external-interactions-cb7ce4a4f2cd) for detailed discussion in the context of Java. – leventov Jan 29 '20 at 15:07

3 Answers3

13

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 4
    I think java has the APIs to perform what Quartz does. I still did not understand the need for Quartz framework. – Newbie Jan 23 '13 at 10:18
  • 3
    Quartz allows you to say you want a task run on the hour. e.g. 12:00, 13:00 etc. The built in APIs allow you to run every hour (starting at some point) but this drifts over time. (As much as 10 seconds a day) – Peter Lawrey Jan 23 '13 at 10:29
  • 2
    @Newbie No. You might use modern Java APIs in Java such as the Executors and *java.time* to implement a tool like *Quartz*, but that would be much work on your part. Quartz is [richly-featured](http://www.quartz-scheduler.org/overview/features.html) with so many extras beyond those of the Executors: variety of time periods, triggers and listeners, persisting jobs to databases or other stores, transactions, fail-over & load-balancing, and more. – Basil Bourque Mar 14 '18 at 22:42
  • @PeterLawrey are you sure Quartz doesn't drift over time? There are [no mentions for 'drift' in the project repository](https://github.com/quartz-scheduler/quartz/search?q=drift&unscoped_q=drift). Preventing clock drift is not trivial and is a valuable feature so I think if it was handled, it would be mentioned either somewhere in the code or in the docs. – leventov Jan 29 '20 at 15:00
  • @leventov AFAIK, it uses System.currentTimeMills() – Peter Lawrey Feb 13 '20 at 18:44
10

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.

cherouvim
  • 31,725
  • 15
  • 104
  • 153
4

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

Matthew Kuraja
  • 401
  • 4
  • 7
  • Thanks, good description. Then link of Quartz is not working, here is the latest one (2.4.0) http://www.quartz-scheduler.org/documentation/2.4.0-SNAPSHOT/tutorials/index.html – Junaed Nov 23 '20 at 15:13