Spring 调度:@Scheduled 与 Quartz

发布于 2024-10-06 06:41:06 字数 526 浏览 6 评论 0原文

我正在阅读 Spring 3.0 doc<关于日程安排。我倾向于 Spring 的 Quartz JobDetailBean。然而,@Scheduled 注释引起了我的注意。看来这是使用 Spring 框架安排任务的另一种方式。根据文档,Spring提供了三种调度方式:

  1. @Scheduled
  2. Via Quartz
  3. Via JDK Timer

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

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

I'm reading the Spring 3.0 docs regarding scheduling. I'm leaning towards Spring's JobDetailBean for Quartz. However, the @Scheduled annotation 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:

  1. @Scheduled
  2. Via Quartz
  3. Via JDK Timer

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).

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

定格我的天空 2024-10-13 06:41:06

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

如果您需要的只是每 X 秒或按照 cron 计划在 bean 上执行一次方法,那么 @Scheduled(或 Spring 的 配置架构< /a>) 可能就足够了

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.

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

北方的巷 2024-10-13 06:41:06

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

调度作业有以下要求:

  • 最终用户应该能够保存和调度(定义执行时间)自己的任务
  • 服务器停机期间的调度作业不应从作业队列中省略

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

  • 使用quartz.properties 文件与Spring 4 MVC 应用程序集成一点也不困难。
  • 我们能够选择第二个数据库来存储主数据库中的作业。
  • 只要服务器启动,在服务器停机期间安排的作业就会开始运行。
  • 作为奖励,我们设法在主数据库中维护一些有关使用自定义 JobListenerTriggerListener 用户定义的计划作业的有用(且更面向用户)信息。
  • Quartz 在具有更复杂调度要求的应用程序中是一个非常有用的库。

I have to state my own experience regarding use of @Scheduled versus Quartz as scheduling implementation in a Spring application.

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:

  • 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 JobListener and TriggerListener.
  • Quartz is a very helpful library in applications with more complex scheduling requirements.
罪#恶を代价 2024-10-13 06:41:06

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

  1. 在 Quartz 中,我们可以将调度程序置于待机模式
    scheduler.standby(); 并使用 scheduler.start(); 重新安排它。
  2. 在作业执行之前或之后关闭调度程序
    scheduler.shutdown(true);scheduler.shutdown(false);
  3. 存储作业供以后使用,当您需要该作业时,您可以
    触发了它。
JobDetail job1 =newJob(MyJobClass.class).
withIdentity("job1","group1").
storeDurably().
build();
  1. 将新作业添加到调度程序,指示它使用给定名称和组(如果有)“替换”现有作业。
JobDetail job1 = newJob(MyJobClass.class).
withIdentity("job1", "group1").
build();

According to Quartz Documentation, we can use some more complex features that don't exist in @Scheduler. For example:

  1. in Quartz we can place a scheduler in stand-by mode with
    scheduler.standby(); and re schedule it with scheduler.start();.
  2. shutting down a scheduler before the execution of the 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.
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).
JobDetail job1 = newJob(MyJobClass.class).
withIdentity("job1", "group1").
build();
故事还在继续 2024-10-13 06:41:06

Spring 提供了一种简单的方法来实现调度作业的 API。在我们部署应用程序的多个实例之前,它会很好地工作。

默认情况下,Spring 无法处理多个实例上的调度程序同步。相反,它在每个节点上同时执行作业。

你可以看看 ShedLock——一个 Java 库,它确保我们的计划任务在同一时间只运行一次,并且是 Quartz 的替代品。

Spring provides an easy way to implement API for scheduling jobs. It works great until we deploy multiple instances of our application.

Spring, by default, cannot handle scheduler synchronization over multiple instances. It executes the jobs simultaneously on every node instead.

You can look at ShedLock — a Java library that makes sure our scheduled tasks run only once at the same time and is an alternative to Quartz.

朕就是辣么酷 2024-10-13 06:41:06

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文