Programming & Development / April 18, 2025

Scheduled Tasks in Spring Boot Using @Scheduled

Spring Boot Scheduled Tasks @Scheduled @EnableScheduling Cron Expression Fixed Rate Fixed Delay Java Scheduler

Spring Boot makes it easy to run scheduled tasks using the @Scheduled annotation. Here’s a complete example to get started with scheduling in a Spring Boot application.

⚙️ Dependencies

Make sure your pom.xml includes at least the following:

xml

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

🚀 Enable Scheduling

Add the @EnableScheduling annotation in your main application class:

java

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

@SpringBootApplication
@EnableScheduling
public class SchedulerExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchedulerExampleApplication.class, args);
    }
}

🕒 Create Scheduled Tasks

You can define scheduled methods using @Scheduled inside a @Component or @Service.

java

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class ScheduledTasks {

    @Scheduled(fixedRate = 5000)
    public void fixedRateTask() {
        System.out.println("Fixed Rate Task :: " + LocalDateTime.now());
    }

    @Scheduled(fixedDelay = 5000)
    public void fixedDelayTask() {
        System.out.println("Fixed Delay Task :: " + LocalDateTime.now());
    }

    @Scheduled(cron = "0 * * * * *")
    public void cronTask() {
        System.out.println("Cron Task :: " + LocalDateTime.now());
    }
}

🧠 Explanation

  • @Scheduled(fixedRate = 5000): Executes the method every 5 seconds after the start of the last execution.
  • @Scheduled(fixedDelay = 5000): Executes the method every 5 seconds after the end of the last execution.
  • @Scheduled(cron = "0 * * * * *"): Executes at the start of every minute (cron format: sec min hour day month weekday).

🔧 Externalize Scheduling Properties

application.properties

properties

app.scheduling.fixed-rate=5000
app.scheduling.fixed-delay=5000
app.scheduling.cron=0 * * * * *

CustomScheduledTasks.java

java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class CustomScheduledTasks {

    @Value("${app.scheduling.fixed-rate}")
    private long fixedRate;

    @Value("${app.scheduling.fixed-delay}")
    private long fixedDelay;

    @Value("${app.scheduling.cron}")
    private String cronExpression;

    @Scheduled(fixedRateString = "${app.scheduling.fixed-rate}")
    public void scheduledRateTask() {
        System.out.println("Externalized Fixed Rate Task: " + LocalDateTime.now());
    }

    @Scheduled(fixedDelayString = "${app.scheduling.fixed-delay}")
    public void scheduledDelayTask() {
        System.out.println("Externalized Fixed Delay Task: " + LocalDateTime.now());
    }

    @Scheduled(cron = "${app.scheduling.cron}")
    public void scheduledCronTask() {
        System.out.println("Externalized Cron Task: " + LocalDateTime.now());
    }
}

✅ Summary

  • Use @EnableScheduling to activate scheduling support.
  • Define methods with @Scheduled to run periodically.
  • Schedule with fixedRate, fixedDelay, or cron expressions.
  • Externalize values with application.properties for flexibility.

This approach allows you to easily implement cron-like jobs within a Spring Boot application using simple annotations.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex