Şuayb's BlogŞuayb's Blog
Home
Categories
Games
MediumAboutContact
Language
Theme
    1. Blog
    2. Programming
    3. Spring Boot Scheduled Tasks

Spring Boot Scheduled Tasks

PublishedFebruary 20, 2025
UpdatedFebruary 21, 2025
Reading time3 min read
JavaKotlinSpring BootScheduled Tasks
XLinkedInFacebook
Spring Boot Scheduled Tasks

Loading likes...

Spring Boot allows developers to easily schedule and execute background tasks using the @Scheduled annotation. This is useful for running periodic jobs, automation tasks, and batch processing without manual intervention.


Last updatedFebruary 21, 2025

Total viewsLoading hits...

Previous articleSpring Boot Reactive ProgrammingNext articleSpring Boot Async Tasks
Şuayb Şimşek

Written by

Şuayb Şimşek

Backend-focused fullstack developer sharing practical notes on Spring Boot, security, microservices, and cloud-native architecture.

Expertise

  • Spring Boot
  • Go
  • Microservices
  • Next.js
  • Cloud Native

Connect

GitHubLinkedInMedium

Related posts

Spring Boot Configuration Properties
Programming

Spring Boot Configuration Properties

Learn how to use @ConfigurationProperties for type-safe configuration, validate settings with @Validated, and manage environment-specific values with profile-specific application-{profile}.yml files.

February 4, 20263 min read
JavaKotlinSpring BootConfiguration
Spring Boot GraphQL JWE Authentication
Programming

Spring Boot GraphQL JWE Authentication

Learn how to secure your Spring Boot GraphQL APIs with stateless encrypted JWTs (JWE) while persisting user identities and roles in a JPA-backed database.

May 17, 20256 min read
JavaKotlinSpring BootSecurityJWTJWEGraphQL
Spring Boot JWE Authentication with JPA
Programming

Spring Boot JWE Authentication with JPA

Learn how to use stateless encrypted JWTs (JWE) to secure your Spring Boot APIs while persisting user identities and roles in a JPA-backed database.

May 11, 20254 min read
JavaKotlinSpring BootSecurityJWTJWEJPA

About

Articles on Spring Boot, microservices, security, and more.

ContactStart here

Latest posts

  • Captain Tsubasa 2: World Fighters
  • Captain Tsubasa: Rise of New Champions
  • Spring Boot Configuration Properties
  • Spring Boot GraphQL JWE Authentication
  • Spring Boot JWE Authentication with JPA

Top topics

JavaKotlinSpring BootJWEJWTMicroservice

Subscribe

Get practical backend + fullstack notes when new articles are published.

Social

© 2024-2026 Şuayb's Blog. All rights reserved.

🌟 Why Use @Scheduled in Spring Boot?

In this section, we clarify Why Use @Scheduled in Spring Boot? and summarize the key points you will apply in implementation.

  • Automate Tasks: Run jobs periodically without human interaction.
  • Efficient Resource Usage: Schedule tasks without blocking main threads.
  • Supports Fixed Rate, Fixed Delay, and Cron Expressions.
  • Seamless Integration: Works with Spring Boot’s dependency injection and lifecycle management.

📋 Prerequisites

Ensure you have the following:

  • ☕ Java Development Kit (JDK) 17+
  • 📦 Maven or Gradle installed
  • 🔤 A Java IDE (e.g., IntelliJ IDEA, Eclipse)

🛠️ Step 1: Add Dependencies

To enable scheduling, you need to include spring-boot-starter-web in your pom.xml or build.gradle file.

Maven:

XMLpom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Gradle:

GROOVYbuild.gradle
implementation 'org.springframework.boot:spring-boot-starter-web'

Why spring-boot-starter-web? Spring Boot applications using @Scheduled need a running Spring Context. Including spring-boot-starter-web ensures that the application lifecycle is properly managed.


🛠️ Step 2: Enable Scheduling in Your Application

To enable scheduling, annotate your main application class with @EnableScheduling.


🛠️ Step 3: Create a Scheduled Task

Define a scheduled task using @Scheduled.


▶️ Running the Application

Run the Spring Boot application:

BASH
./mvnw spring-boot:run

Or using Gradle:

BASH
gradle bootRun

The scheduled task will execute every 5 seconds and print a timestamp.


🧪 Testing the Scheduled Task

In this section, we clarify Testing the Scheduled Task and summarize the key points you will apply in implementation.

Expected Console Output:

Compare your console logs with this output to quickly confirm the behavior is correct.

PLAINTEXTsnippet.txt
Scheduled task executed at: 12:00:01
Scheduled task executed at: 12:00:06
Scheduled task executed at: 12:00:11

🏁 Conclusion

You now have a practical Spring Boot Scheduled Tasks implementation with a clear, production-friendly Spring Boot structure. As a next step, adapt configuration and tests to your own domain, then validate behavior under realistic traffic and failure scenarios.

JAVAScheduledApplication.java
package com.example.scheduled;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ScheduledApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScheduledApplication.class, args);
    }
}
KOTLINScheduledApplication.kt
package com.example.scheduled

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling

@SpringBootApplication
@EnableScheduling
class ScheduledApplication

fun main(args: Array<String>) {
    runApplication<ScheduledApplication>(*args)
}
JAVAScheduledTask.java
package com.example.scheduled;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalTime;

@Service
public class ScheduledTask {

    @Scheduled(fixedRate = 5000)
    public void runTask() {
        System.out.println("Scheduled task executed at: " + LocalTime.now());
    }
}
KOTLINScheduledTask.kt
package com.example.scheduled

import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import java.time.LocalTime

@Service
class ScheduledTask {

    @Scheduled(fixedRate = 5000)
    fun runTask() {
        println("Scheduled task executed at: ${LocalTime.now()}")
    }
}