Spring 调度:@Scheduled 与 Quartz
- @Scheduled
- Via Quartz
- 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:
- @Scheduled
- Via Quartz
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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我必须阐述一下我自己在 Spring 应用程序中使用
@Scheduled
与Quartz
作为调度实现的经验。调度作业有以下要求:
因此,我们必须尝试使用 Quartz 实现(版本 2.2) .3) 为了支持数据库中作业的持久性。一些基本结论如下:
JobListener
和TriggerListener
用户定义的计划作业的有用(且更面向用户)信息。I have to state my own experience regarding use of
@Scheduled
versusQuartz
as scheduling implementation in a Spring application.Scheduling jobs had the following requirements:
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:
JobListener
andTriggerListener
.根据Quartz Documentation,我们可以使用@Scheduler 中不存在一些更复杂的功能。例如:
scheduler.standby();
并使用scheduler.start();
重新安排它。scheduler.shutdown(true);
和scheduler.shutdown(false);
触发了它。
According to Quartz Documentation, we can use some more complex features that don't exist in @Scheduler. For example:
scheduler.standby();
and re schedule it withscheduler.start();
.scheduler.shutdown(true);
andscheduler.shutdown(false);
triggered it.
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.
在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.