Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - @Scheduled Annotation


What is the first thing that comes into your mind when you hear the term Scheduled?


Well! Something that is Scheduled to run at a particular time.


If you guessed that, you are right on track. @Scheduled Annotation is used to run a particular task at a given time.


Not just that, @Scheduled Annotation does a lot more than just running a particular task at a particular time.


How do we use @Scheduled Annotation in Spring Boot?


With @Scheduled Annotation, we are going to run an application that runs at a particular time.


So let us build a Spring Boot application first.

  1. To do that let us create a class and name it 'SpringBootSchedular' and annotate it with @Component annotation.


    @Component
    public class SpringBootSchedular {
    }

    Spring Boot - @Scheduled Annotation


    @Component annotation will make the class SpringBootSchedular available to SpringBoot.

  2. Then let us annotate 'SpringBootSchedular' class with @EnableScheduling Annotation. This will let Spring Boot know that the @Scheduled annotation needs to be used.


    @Component
    @EnableScheduling
    public class SpringBootSchedular {
    
    }

    Spring Boot - @Scheduled Annotation

  3. Next, let us create a method named 'printHelloWorld()' in the 'SpringBootSchedular' class and annotate it with @Scheduled annotation.


    @Component
    @EnableScheduling
    public class SpringBootSchedular {
    	@Scheduled(cron = "0 * 15 * * ?")
    	public void printHelloWorld() {
    		System.out.println("Hello World");
    	}
    }

    Spring Boot - @Scheduled Annotation


    In the above code, we have made the @Scheduled annotation to run everyday at 3:00 PM. And that is mentioned in the cron expression of the @Scheduled annotation.

    @Scheduled(cron = "0 * 15 * * ?")


    And what happens is, Hello World gets printed every day at 3:00 PM (Since 15:00 stands for 3:00 PM).

Other than running at a particular time, we also said that the @Scheduled annotation does a lot more than that.


Let us see them below :


FixedDelay using @Scheduled annotation


@Component
@EnableScheduling
public class SpringBootSchedular {

	@Scheduled(fixedDelay = 1000)
	public void printHelloWorld() {

		System.out.println("Hello World");
	}
}

So in the above code, we have used fixedDelay with @Scheduled annotation.


@Scheduled(fixedDelay = 1000)

The application waits 1 second or 1000 milli seconds to execute the next task.


In simple words, Hello World is printed then the control waits for 1 second and again prints Hello World.


This is how, fixedDelay works with @Scheduled annotation.


FixedRate using @Scheduled annotation


@Component
@EnableScheduling
public class SpringBootSchedular {

	@Scheduled(fixedRate = 1000)
	public void printHelloWorld() {

		System.out.println("Hello World");
	}
}

The fixedRate is almost similar to fixedDelay with a minor difference. The fixedRate doesn't wait for the previous task to finish. Where as the fixedDelay waits for the previous task to finish.


InitialDelay using @Scheduled annotation


@EnableScheduling
public class SpringBootSchedular {

	@Scheduled(fixedDelay = 1000, initialDelay = 1000)
	public void printHelloWorld() {

		System.out.println("Hello World");
	}
}

The initialDelay is used with fixedDelay property, when you want to start the execution with an initial delay of 1 second.


In simple words, the Hello World would be printed after 1 second initially due to the initialDelay property. Then continue printing every 1 second as we are using fixedDelay property.