Java 春季调度:@Scheduled vs Quartz

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4385719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:47:16  来源:igfitidea点击:

Spring Scheduling: @Scheduled vs Quartz

javaspringschedulingquartz-scheduler

提问by chris

I'm reading the Spring 3.0 docs regarding scheduling. I'm leaning towards Spring's JobDetailBean for Quartz. However, the @Scheduledannotation has captured my eye. It appears this is another way of scheduling task using the Spring Framework. Based on the docs, Spring provides three way of scheduling:

我正在阅读有关调度的Spring 3.0 文档。我倾向于 Spring 的 JobDetailBean for Quartz。然而,@ Scheduled注释吸引了我的眼球。看来这是使用 Spring Framework 调度任务的另一种方式。根据文档,Spring 提供了三种调度方式:

  1. @Scheduled
  2. Via Quartz
  3. Via JDK Timer
  1. @预定
  2. 通过石英
  3. 通过 JDK 定时器

I have no interest in the JDK Timer. Why should I choose @Scheduled over Quartz? (When I mention Quartz I mean using Spring's bean wrapper for Quartz).

我对 JDK 计时器不感兴趣。为什么我应该选择 @Scheduled 而不是 Quartz?(当我提到 Quartz 时,我的意思是对 Quartz 使用 Spring 的 bean 包装器)。

Let's say my use case is complex enough that I will be communicating to a third-party web service to import and export data at specified intervals.

假设我的用例足够复杂,我将与第三方 Web 服务通信以在指定的时间间隔导入和导出数据。

回答by skaffman

Quartz is an order of magnitude more complex than Spring's built in scheduler, including support for persistent, transactional and distributed jobs. It's a bit of a pig, though, even with Spring's API support.

Quartz 比 Spring 的内置调度程序复杂一个数量级,包括对持久性、事务性和分布式作业的支持。但是,即使有 Spring 的 API 支持,它也有点像猪。

If all you need to is to execute methods on a bean every X seconds, or on a cron schedule, then @Scheduled(or the various options in Spring's <task>config schema) is probably enough

如果您只需要每 X 秒或按 cron 计划在 bean 上执行一次方法,那么@Scheduled(或 Spring<task>配置模式中的各种选项)可能就足够了

回答by lzagkaretos

I have to state my own experience regarding use of @Scheduledversus Quartzas scheduling implementation in a Spring application.

我必须陈述我自己在 Spring 应用程序中使用@ScheduledvsQuartz作为调度实现的经验。

Scheduling jobs had the following requirements:

调度作业具有以下要求:

  • End users should have the ability to save and schedule (define execution time) their own tasks
  • Scheduled jobs during server downtime should not get omitted from jobs queue
  • 最终用户应该能够保存和安排(定义执行时间)他们自己的任务
  • 服务器停机期间的计划作业不应从作业队列中省略

Hence, we have to try and use Quartz implementation (version 2.2.3) in order to support persistence of jobs in a database. Some basic conclusions are the following:

因此,我们必须尝试使用​​ Quartz 实现(版本 2.2.3)来支持数据库中作业的持久性。一些基本结论如下:

  • Integration with a Spring 4 MVC application is not difficult at all using quartz.properties file.
  • We had the ability to choose a second database for storing the jobs from the main database.
  • Jobs scheduled during server downtime begin running as long as server comes up.
  • As a bonus we managed to maintain in main database some useful (and more user-oriented) information about user defined scheduled jobs using custom JobListenerand TriggerListener.
  • Quartz is a very helpful library in applications with more complex scheduling requirements.
  • 使用quartz.properties 文件与Spring 4 MVC 应用程序集成并不困难。
  • 我们可以选择第二个数据库来存储主数据库中的作业。
  • 只要服务器启动,在服务器停机期间安排的作业就会开始运行。
  • 作为奖励,我们设法在主数据库中使用自定义JobListenerTriggerListener.
  • Quartz 在具有更复杂调度需求的应用程序中是一个非常有用的库。

回答by Cyril Sojan

In Spring you could schedule task by using FixedRate,FixedDelay and cron. But most of the scheduled job requires dynamic handling of execution time. So in this scenario it is better to use Quartz as it provide the option to store scheduled jobs in DBJobstore as well as RAMJobstore.

在 Spring 中,您可以使用 FixedRate、FixedDelay 和 cron 来安排任务。但大部分预定作业需要动态处理执行时间。因此,在这种情况下,最好使用 Quartz,因为它提供了在 DBJobstore 和 RAMJobstore 中存储计划作业的选项。

回答by Saman Salehi

According to Quartz Documentation

根据Quartz 文档

We can use some more and complex feature that it doesn't exist in @Scheduler. for example:

我们可以使用@Scheduler 中不存在的一些更复杂的功能。例如:

  1. in Quartz we can placing a scheduler in stand-by mode with scheduler.standby();and re schedule it with scheduler.start();.
  2. shutting down a scheduler before execution of job or after that with scheduler.shutdown(true);and scheduler.shutdown(false);
  3. storing a job for later use and when you need the job you can triggered it.
  1. 在 Quartz 中,我们可以使用 将调度器置于待机模式 scheduler.standby();并使用 重新调度它scheduler.start();
  2. 在执行作业之前或之后使用scheduler.shutdown(true);和关闭调度程序 scheduler.shutdown(false);
  3. 存储作业供以后使用,当您需要该作业时,您可以触发它。

JobDetail job1 =newJob(MyJobClass.class). withIdentity("job1","group1"). storeDurably(). build();

JobDetail job1 =newJob(MyJobClass.class). withIdentity("job1","group1"). storeDurably(). build();

  1. Add the new job to the scheduler, instructing it to "replace" the existing job with the given name and group (if any).
  1. 将新作业添加到调度程序,指示它使用给定的名称和组(如果有)“替换”现有作业。

JobDetail job1 = newJob(MyJobClass.class). withIdentity("job1", "group1"). build();

JobDetail job1 = newJob(MyJobClass.class). withIdentity("job1", "group1"). build();