Spring Boot provides the ability to schedule tasks for execution at a given time period with the help of @Scheduled annotation. This article provides a step by step guideline on how we can schedule tasks to run in a spring boot application
Implementation:
It is depicted below stepwise as follows:
Step 1: Creating a spring boot application using Spring Initializer for which one can refer to the basics of creating a Spring class.
Step 2: Specifying @EnableScheduling annotation in the Spring Boot application class.
Java
// Java Program to Illustrate Specifying
// @EnableScheduling annotation
package com.Scheduler;
// Importing required classes
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
// Annotation
@SpringBootApplication
@EnableScheduling
// Class
public class SchedulerApplication {
// Main driver method
public static void main(String[] args)
{
SpringApplication.run(SchedulerApplication.class,
args);
}
}
@EnableScheduling annotation facilitates Spring Boot with scheduled task execution capability.
Step 3: Creating a @Component class Scheduler which defines the method scheduleTask() for scheduling a task using the @Scheduled annotation.
The method scheduleTask() in Scheduler class simply prints the date and time at which the task is running.
Scheduling tasks using a cron expression
Java
// Java Program to Illustrate Scheduling Task
// using a cron expression
package com.Scheduler;
// Importing required classes
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
// Annotation
@Component
// Class
public class Scheduler {
// Method
// To trigger the scheduler every one minute
// between 19:00 PM to 19:59 PM
@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);
}
}
The cron element specified in the @Scheduled annotation allows defining cron-like expressions to include triggers on the second, minute, hour, day of the month, month, and day of the week. The expression specified here in the cron element directs spring boot to trigger the scheduler every one minute between 19:00.00 to 19:59.00.
On Running the Spring Boot Application, we can see the output in the console as follows:

Scheduling tasks at Fixed Rate
Java
// Java Program to Illustrate Scheduling Task
// At a Fixed Rate
package com.Scheduler;
// Importing required classes
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
// Annotation
@Component
// Class
public class Scheduler {
// Method
// To trigger the scheduler to run every two seconds
@Scheduled(fixedRate = 2000) public void scheduleTask()
{
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss.SSS");
String strDate = dateFormat.format(new Date());
System.out.println(
"Fixed rate Scheduler: Task running at - "
+ strDate);
}
}
The fixedRate element specified in the @Scheduled annotation executes the annotated method at a fixed time period between invocations. It does not wait for the previous task to be complete. The time value specified for this element is in milliseconds.
Here a fixed rate scheduler is defined which runs every 2 seconds starting at 19:11:58.
On Running the Spring Boot Application, we can see the output in the console as follows:
Scheduling tasks to run at Fixed Delay
Java
// Java Program to Illustrate Scheduling Task
// at a Fixed Delay
package com.Scheduler;
// Importing required classes
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
// Annotation
@Component
// Class
public class Scheduler {
// Method
// To trigger the scheduler every 3 seconds with
// an initial delay of 5 seconds.
@Scheduled(fixedDelay = 3000, initialDelay = 5000)
public void scheduleTask()
{
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);
}
}
The fixedDelay element specified in the @Scheduled annotation executes the annotated method at a fixed time period between the end of the previous invocation and the start of the next invocation. It basically waits for the previous task to be complete. The time value specified for this element is in milliseconds.
The initialDelay element specified here allows mentioning the amount of time it waits before the invocation of the first task. The time value specified for this element is in milliseconds.
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.
On Running the Spring Boot Application, we can see the output in the console as follows:

Similar Reads
Spring Boot Actuator
Developing and managing an application are the two most important aspects of the applicationâs life cycle. It is very important to know what is going on beneath the application. Also, when we push the application into production, managing it gradually becomes critically important. Therefore, it is a
5 min read
Spring Boot - Introduction to RESTful Web Services
RESTful Web Services REST stands for REpresentational State Transfer. It was developed by Roy Thomas Fielding, one of the principal authors of the web protocol HTTP. Consequently, REST was an architectural approach designed to make the optimum use of the HTTP protocol. It uses the concepts and verbs
5 min read
How to create a basic application in Java Spring Boot
Spring Boot is the most popular Java framework that is used for developing RESTful web applications. In this article, we will see how to create a basic Spring Boot application.Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also p
3 min read
How to Create a REST API using Java Spring Boot?
Representational State Transfer (REST) is a software architectural style that defines a set of constraints for creating web services. RESTful web services allow systems to access and manipulate web resources through a uniform and predefined set of stateless operations. Unlike SOAP, which exposes its
4 min read
Easiest Way to Create REST API using Spring Boot
Spring Boot is a powerful framework that makes it easy to create RESTful APIs. Creating a REST API using Spring Boot is one of the fastest and simplest ways to develop scalable and production-ready web services. Spring Boot simplifies REST API development by providing built-in features such as autom
11 min read
Java Spring Boot Microservices Sample Project
Microservices are more popular nowadays. They can be written in any language. In this article, let us see Spring Boot Microservices. in this article let us see a base project "currency-exchange-sample-service" which has a business logic and which can be invoked in another project "currency-conversio
9 min read
Difference between Spring MVC and Spring Boot
1. Spring MVC : Spring is widely used for creating scalable applications. For web applications Spring provides Spring MVC framework which is a widely used module of spring which is used to create scalable web applications. Spring MVC framework enables the separation of modules namely Model View, Con
3 min read
Spring Boot - Spring JDBC vs Spring Data JDBC
Understanding the difference between Spring JDBC and Spring Data JDBC is important for choosing the right approach to interact with relational databases in Spring Boot applications. Both frameworks serve the same purpose but differ significantly in terms of abstraction, ease of use, and developer pr
5 min read
Best Practices For Structuring Spring Boot Application
Spring Boot is built on top of the conventional spring framework. So, it provides all the features of spring and is yet easier to use than spring. In this article, we are going to see how one should start and structure his Spring Boot application. Prerequisites: Good knowledge of Java.Basic knowledg
3 min read
Spring Boot - Start/Stop a Kafka Listener Dynamically
In a Spring Boot application, Kafka Listeners start automatically once the application launches and they listen for messages from Kafka topics. But there are many scenarios where we might need to dynamically start or stop a Kafka listener based on certain conditions. This can be achieved using Kafka
7 min read