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开发者_StackOverflow中文版 (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
.
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.
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.
Java's Executor solution allows you to either:
- immediately run a task
- 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
精彩评论