61

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
chris
  • 3,820
  • 6
  • 36
  • 36

5 Answers5

44

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 7
    "It's [Quartz] a bit of a pig" - it's 2 years later and I'm doing the same comparison as @chris. Is Quartz still difficult to use via Spring? – Edward Feb 21 '13 at 16:13
  • 7
    Does @Scheduled support CLUSTER? If so then how can I achieve it? – Rushabh Patel Jul 17 '15 at 08:24
  • 7
    No, @Scheduled does not support cluster or multi node environments, which seems to be its only major limitation and the main reason a lot of people still use Quartz. – BrianC Dec 09 '16 at 16:38
  • 2
    I have use quartz with Spring's wrapper and have developed a scheduler module for our company. I find it a good choice. Quartz with spring is not complex. And it works perfectly in a clustered environment. We didn't found any issue working with it. – learner Feb 06 '17 at 11:17
  • 4
    Nice article by Khalid Saleem - http://khalidsaleem.blogspot.in/2015/03/quartz-scheduler-vs-spring-scheduler.html. Basically, Spring Scheduler (Spring 3.0+) is a truly light weight implementation that will suffice for simple scheduling needs and, it provides annotation support for both Task Scheduling and Asynchronous method execution. It provides support for fixed rate & delay and cron based scheduling. But, Quartz provides support for enterprise level features like JTA and clustering; it comes with JobPersistence (JDBC & RAM stores) which can be used for Fail-safe & Load Balancing purposes. – Mahesh Jun 26 '17 at 18:02
21

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.
lzagkaretos
  • 2,842
  • 2
  • 16
  • 26
4

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();
Saikat
  • 14,222
  • 20
  • 104
  • 125
Saman Salehi
  • 1,004
  • 1
  • 12
  • 19
0

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.

Cyril Sojan
  • 149
  • 7
0

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.