Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between fixed rate and fixed delay in Spring Scheduled annotation?

I am implementing scheduled tasks using Spring, and I see there are two types of config options for time that schedule work again from the last call. What is the difference between these two types?

 @Scheduled(fixedDelay = 5000)  public void doJobDelay() {      // do anything  }   @Scheduled(fixedRate = 5000)  public void doJobRate() {      // do anything  } 
like image 925
Adam Avatar asked Aug 09 '16 05:08

Adam


People also ask

What is fixed delay in spring scheduler?

Schedule a Task at Fixed Delay In this case, the duration between the end of the last execution and the start of the next execution is fixed. The task always waits until the previous one is finished. This option should be used when it's mandatory that the previous execution is completed before running again.

What is @scheduled in spring?

Spring Core. Spring provides excellent support for both task scheduling and asynchronous method execution based on cron expression using @Scheduled annotation. The @Scheduled annotation can be added to a method along with trigger metadata.

What is @scheduled in Java?

The schedule (TimerTask task, Date time) method of Timer class is used to schedule the task for execution at the given time. If the time given is in the past, the task is scheduled at that movement for execution.

What is @scheduled in spring boot?

The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. @SpringBootApplication @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.


1 Answers

  • fixedRate : makes Spring run the task on periodic intervals even if the last invocation may still be running.
  • fixedDelay : specifically controls the next execution time when the last execution finishes.

In code:

@Scheduled(fixedDelay=5000) public void updateEmployeeInventory(){     System.out.println("employee inventory will be updated once only the last updated finished ");     /**      * add your scheduled job logic here      */ }   @Scheduled(fixedRate=5000) public void updateEmployeeInventory(){     System.out.println("employee inventory will be updated every 5 seconds from prior updated has stared, regardless it is finished or not");     /**      * add your scheduled job logic here      */ } 
like image 143
kuhajeyan Avatar answered Sep 28 '22 04:09

kuhajeyan