Introduction
Scheduling is used when you want to call the function every certain time. @Schedule annotation used for schedule tasks for execution at a given time period in spring boot. By fixed delay you called the function.
Here in this blog, you learn about how to schedule tasks in springboot.
Prerequisites
- JDK 11 or later
- Maven or Gradle
- IDE (here Intellij)
Why schedule tasks ?
Schedule task provides functionality to the application that did some operation for a certain time without manual interaction. Here some other advantage of schedule task:
- Automatic backups.
- Sending schedule notification.
- Data synchronization with external systems.
- Generating periodic analytics or reports.
Set up Springboot project
Step 1 : Add maven dependencies.
Add the following dependencies to your application.
org.springframework.boot
spring-boot-starter-web
Step 2 : Implement method for schedule task.
Use @Schedule annotation to the method for executing that method periodically. We can schedule tasks in multiple ways. Here you can see that,
Scheduling task using fixed rate
The fixed rate element in the Scheduled annotation refers to the scheduling method that runs between fixed time periods. It does not wait for the completed previous one. It is executed after mention time.
Here in example, the method runs every 2 seconds after application starts.
package com.example.SchedulingDemo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
// To trigger the scheduler to run every two seconds
@Scheduled(fixedRate = 2000)
public void performTask() {
System.out.println("Fixed rate Scheduler: Task running at - " + System.currentTimeMillis());
}
}

Scheduling task using fixed delay
The fixed delay element in the Scheduled annotation refers to the scheduling method that runs between fixed time periods between end of previous innovation and start of the next invocation.It is to wait for the completed previous one.
Here the initial delay shows that the method runs after the mentioned first delay time.
Here the scheduler defined starts with an initial delay of 5 seconds and goes on to execute the task at a fixed delay of 3 seconds.
package com.example.SchedulingDemo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
@Scheduled(fixedDelay = 3000, initialDelay = 5000)
public void scheduleTaskByFixedDelay() {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
String strDate = dateFormat.format(new Date());
System.out.println("Fixed Delay Scheduler: Task running at - " + strDate);
}
}

Scheduling task using a cron expression
The cron element in the Scheduled annotation refers to the method that allows defining cron-like expressions. That includes triggers on the minute, hour, second, minute, hour, day of the month, month, and day of the week.
Here the expression specified the cron element trigger every one minute between 19:00.00 to 19:59.00.
package com.example.SchedulingDemo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
@Scheduled(cron = "0 * 19 * * ?")
public void scheduleTask() {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
String strDate = dateFormat.format(new Date());
System.out.println("Cron job Scheduler: Job running at - " + strDate);
}

Step 3 : Enable scheduling into your application.
By @EnableScheduling annotation we can enable scheduling into the application.
package com.example.SchedulingDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SchedulingDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulingDemoApplication.class, args);
}
}
Conclusion
Scheduling provides an easy set up to execute some method on some time. By your need you can set the repeat task time or also set delay time. In scheduling there are also advanced features available. In advance scheduling you did Asynchronous Scheduling. For your needs and requirements you can choose an appropriate scheduling method and by proper maintenance make your application efficient.