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

Adam picture Adam · Aug 9, 2016 · Viewed 55.3k times · Source

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
 }

Answer

kuhajeyan picture kuhajeyan · Aug 9, 2016
  • 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
     */
}