- Spring Boot - Home
- Spring Boot - Introduction
- Spring Boot - Quick Start using CLI
- Spring Boot - Bootstrapping
- Spring Tool Suite
- Spring Boot - Tomcat Deployment
- Spring Boot - Build Systems
- Spring Boot - Code Structure
- Spring Boot - Beans & Dependency Injection
- Spring Boot - Runners
- Spring Boot - Starters
- Spring Boot - Application Properties
- Spring Boot - Configuration
- Spring Boot - Annotations
- Spring Boot - Logging
- Building RESTful Web Services
- Spring Boot - Exception Handling
- Spring Boot - Interceptor
- Spring Boot - Servlet Filter
- Spring Boot - Tomcat Port Number
- Spring Boot - Rest Template
- Spring Boot - File Handling
- Spring Boot - Service Components
- Spring Boot - Thymeleaf
- Consuming RESTful Web Services
- Spring Boot - CORS Support
- Spring Boot - Internationalization
- Spring Boot - Scheduling
- Spring Boot - Enabling HTTPS
- Spring Boot - Eureka Server
- Service Registration with Eureka
- Gateway Proxy Server and Routing
- Spring Cloud Configuration Server
- Spring Cloud Configuration Client
- Spring Boot - Actuator
- Spring Boot - Admin Server
- Spring Boot - Admin Client
- Spring Boot - Enabling Swagger2
- Spring Boot - Using SpringDoc OpenAPI
- Spring Boot - Creating Docker Image
- Tracing Micro Service Logs
- Spring Boot - Flyway Database
- Spring Boot - Sending Email
- Spring Boot - Hystrix
- Spring Boot - Web Socket
- Spring Boot - Batch Service
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - Unit Testing
- Rest Controller Unit Testing
- Spring Boot - Database Handling
- Securing Web Applications
- Spring Boot - OAuth2 with JWT
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 Sign-In
Spring Boot Resources
Spring Boot - Scheduling
Scheduling is a process of executing the tasks for the specific time period. Spring Boot provides a good support to write a scheduler on the Spring applications.
Java Cron Expression
Java Cron expressions are used to configure the instances of CronTrigger, a subclass of org.quartz.Trigger. For more information about Java cron expression you can refer to this link −
https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm
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.
DemoApplication.java
package com.tutorialspoint.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tutorialspoint</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Example - Scheduing a Task
The @Scheduled annotation is used to trigger the scheduler for a specific time period.
@Scheduled(cron = "0 0-5 13 * * ?")
public void cronJobSch() throws Exception {
}
The following is a sample code that shows how to execute the task every minute starting at 1:00 PM for next five minutes.
Scheduler.java
package com.tutorialspoint.demo.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Scheduler {
@Scheduled(cron = "0 0-5 13 * * ?")
public void cronJobSch() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String strDate = sdf.format(now);
System.out.println("Java cron job expression:: " + strDate);
}
}
Output
The following screenshot shows how the application has started at 13:03:00 and for every one minute from that time the cron job scheduler task has executed and it will execute for next five minutes as per the cron job expression.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.5.6) [2025-09-29T13:03:20Z] [org.springframework.boot.StartupInfoLogger] [main] [53] [INFO ] Starting DemoApplication using Java 21.0.6 with PID 21384 (D:\Projects\demo\target\classes started by mahes in D:\Projects\demo) [2025-09-29T13:03:20Z] [org.springframework.boot.SpringApplication] [main] [652] [INFO ] No active profile set, falling back to 1 default profile: "default" [2025-09-29T13:03:20Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [111] [INFO ] Tomcat initialized with port 8080 (http) [2025-09-29T13:03:20Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Initializing ProtocolHandler ["http-nio-8080"] [2025-09-29T13:03:20Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Starting service [Tomcat] [2025-09-29T13:03:20Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Starting Servlet engine: [Apache Tomcat/10.1.46] [2025-09-29T13:03:20Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Initializing Spring embedded WebApplicationContext [2025-09-29T13:03:20Z] [org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext] [main] [301] [INFO ] Root WebApplicationContext: initialization completed in 1127 ms [2025-09-29T13:03:20Z] [org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping] [main] [59] [INFO ] Adding welcome page template: index [2025-09-29T13:03:20Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Starting ProtocolHandler ["http-nio-8080"] [2025-09-29T13:03:20Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [243] [INFO ] Tomcat started on port 8080 (http) with context path '/' [2025-09-29T13:03:20Z] [org.springframework.boot.StartupInfoLogger] [main] [59] [INFO ] Started DemoApplication in 2.13 seconds (process running for 2.549) Java cron job expression:: 2025-09-29 13:03:00.091 Java cron job expression:: 2025-09-29 13:04:00.013 Java cron job expression:: 2025-09-29 13:05:00.015
Example - Scheduling at Fixed Rate
Fixed Rate scheduler is used to execute the tasks at the specific time. It does not wait for the completion of previous task. The values should be in milliseconds. The sample code is shown here −
@Scheduled(fixedRate = 1000)
public void fixedRateSch() {
}
A sample code for executing a task on every second from the application startup is shown here −
Scheduler.java
package com.tutorialspoint.demo.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Scheduler {
@Scheduled(fixedRate = 1000)
public void fixedRateSch() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String strDate = sdf.format(now);
System.out.println("Fixed Rate scheduler:: " + strDate);
}
}
Output
Observe the following screenshot that shows the application that has started at 13:06:45 and after that every second fixed rate scheduler task has executed.
Fixed Rate scheduler:: 2025-09-29 13:06:45.832 Fixed Rate scheduler:: 2025-09-29 13:06:46.813 Fixed Rate scheduler:: 2025-09-29 13:06:47.809
Example - Scheduling with Fixed Delay
Fixed Delay scheduler is used to execute the tasks at a specific time. It should wait for the previous task completion. The values should be in milliseconds. A sample code is shown here −
@Scheduled(fixedDelay = 1000, initialDelay = 1000)
public void fixedDelaySch() {
}
Here, the initialDelay is the time after which the task will be executed the first time after the initial delay value.
An example to execute the task for every second after 3 seconds from the application startup has been completed is shown below −
Scheduler.java
package com.tutorialspoint.demo.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Scheduler {
@Scheduled(fixedDelay = 1000, initialDelay = 3000)
public void fixedDelaySch() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String strDate = sdf.format(now);
System.out.println("Fixed Delay scheduler:: " + strDate);
}
}
Output
Observe the following screenshot which shows the application that has started at 13:11:42 and after every 3 seconds, the fixed delay scheduler task has executed on every second.
Fixed Delay scheduler:: 2025-09-29 13:11:46.061 Fixed Delay scheduler:: 2025-09-29 13:11:47.063 Fixed Delay scheduler:: 2025-09-29 13:11:48.065