There are multiple ways to schedule a job:
- Spring in built- Task scheduler
- Quartz [Third party open source frame work ]
- Timer that comes as integrated wih JDK 1.3
Spring + Quartz integration
- Quartz
JobDetail
objects contain all information needed to run a job. Spring provides aJobDetailFactoryBean
- The timeout is specified in the job data map. The job data map is available through the
JobExecutionContext
(passed to you at execution time), but theJobDetail
also gets its properties from the job data mapped to properties of the job instance. - Often you just need to invoke a method on a specific object. Using the
MethodInvokingJobDetailFactoryBean
you can do exactly this: - By default, Quartz Jobs are stateless, resulting in the possibility of jobs interfering with each other. If you specify two triggers for the same
JobDetail
, it might be possible that before the first job has finished, the second one will start. IfJobDetail
classes implement theStateful
interface, this won’t happen. The second job will not start before the first one has finished. To make jobs resulting from theMethodInvokingJobDetailFactoryBean
non-concurrent, set theconcurrent
flag tofalse
. - Finally we wireup jobs & triggers using SchedulerFactoryBean
References:
______________________________________________________
https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-quartz
____________________________________________________________________
TaskScheduler
public interface TaskScheduler {
ScheduledFuture schedule(Runnable task, Trigger trigger);
ScheduledFuture schedule(Runnable task, Date startTime);
ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period);
ScheduledFuture scheduleAtFixedRate(Runnable task, long period);
ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay);
}
Spri
@EnableScheduling
. This annotation enables Spring’s scheduled task execution capability, similar to functionality found in Spring’s task
namespace we saw in previous tutorial. Thanks to @EnableScheduling, all the bean methods annotated with @Scheduler will be registered for scheduling
Above
@Scheduled
annotated method will be called by Scheduler every 5 seconds. Note that the method which is annotated with @Scheduler must return void and must not have any parameters. Of course you can inject another bean in above bean to get some external functionality called in printMessage.
@Scheduled annotation have several attributes to specify different scheduling timelines.
Attribute
initialDelay
attribute specifies the number of milliseconds to wait before the first execution of the method. fixedRate
specifies the number of milliseconds between each method start , regardless of how long method takes to complete. fixedDelay
specifies the number of milliseconds between completion of previous run, and start of next run.Attribute
cron
provides more fine-grained control on task execution scheduling. For example, @Scheduled(cron=*/5 * * * * MON-FRI")
configures this method to be executed every 5 seconds but only on weekdays.
Spring has its own scheduler then why quartz is still relevant ?
- http://www.baeldung.com/spring-quartz-schedule
- http://khalidsaleem.blogspot.com/2015/03/quartz-scheduler-vs-spring-scheduler.html
- https://stackoverflow.com/questions/4385719/spring-scheduling-scheduled-vs-quartz
_______________________________________________________________________
No comments:
Post a Comment