简体   繁体   English

Java Web应用程序中的Quartz与ScheduledExecutorService

[英]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 . 对于监视当前在命令行上运行并使用ScheduledExecutorService Java应用程序的系统,我想编写一个简单的Web应用程序版本,以便在像Apache TomcatEclipse Jetty这样的Servlet容器中运行。

I have read about Quartz as one of the popular job schedulers for web applications. 我已经读过Quartz作为Web应用程序的流行作业调度程序之一。 Would it be better (maybe because of better servlet container integration) to port this application from ScheduledExecutorService to Quartz? 将此应用程序从ScheduledExecutorService移植到Quartz会更好(可能是因为更好的servlet容器集成)?

Adding another library dependency to the application is not a problem, I am interested in technical reasons against usage of ScheduledExecutorService . 向应用程序添加另一个库依赖项不是问题,我对反对使用ScheduledExecutorService技术原因感兴趣。

It depends on what you are using it for. 这取决于你使用它的是什么。

Quartz is useful for programmed times eg every hour on the hour. 石英对于编程时间非常有用,例如每小时一小时。

ScheduledExecutorService is useful for repeating tasks which don't have to occur at a specific time. ScheduledExecutorService对于重复不必在特定时间发生的任务非常有用。 Its simpler and possibly more efficient. 它更简单,可能更有效。 If you have this working it indicates to me that you don't need Quartz. 如果你有这个工作,它告诉我你不需要Quartz。

ScheduledExecutorService operates at a lower level and you'd have to implement all scheduling monitoring/maintenance facilities yourself. ScheduledExecutorService在较低级别运行,您必须自己实施所有调度监视/维护工具。

Quartz has tons of facilities such as Job Persistence, Transactions, Clustering etc. Quartz拥有大量的工具 ,如工作持久性,交易,聚类等。

Java's Executor solution allows you to either: Java的Executor解决方案允许您:

  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. 但是,Quartz在运行任务/工作的时间和频率方面赋予您极大的灵活性。 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. 或者在本月的最后一天,你不必弄清楚某个月的最后一天是在28日,29日,30日还是31日。 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 以下是cron样式调度所适应的灵活性的更多示例 - 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 使用Java的库比较容易,但是对于那些想要开始进入Quartz工作的简单代码库示例的人来说,我已经把这个模板放在一起免费下载使用 - https://github.com/javateer/quartz-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM