Spring Boot @Scheduled Tutorial: Building Scheduled Tasks with TaskScheduler

    Spring Boot @Scheduled Tutorial: Building Scheduled Tasks with TaskScheduler

    Learn how to schedule tasks in Spring Boot using the @Scheduled annotation and TaskScheduler. This guide covers fixedRate, fixedDelay, cron expressions, async scheduling with @Async, custom thread pools, and best practices for building efficient scheduled tasks.

    default profile

    Munaf Badarpura

    September 08, 2025

    3 min read

    Task scheduling is the process of executing methods or tasks at specified times or intervals, either once or repeatedly, without manual intervention. Spring Boot simplifies this with the @Scheduled annotation and the TaskScheduler.

    This enabling developers to automate tasks like logging, notifications, and data processing. This blog explores how to implement scheduled tasks in Spring Boot, including configuration, use cases, and best practices.

    What is Task Scheduling in Spring Boot?#

    Spring Boot provides built-in support for scheduling through the @Scheduled annotation, which integrates with the TaskScheduler interface. By default, it uses a ScheduledExecutorService under the hood, leveraging a thread pool for task execution. This allows for efficient management of recurring or delayed tasks without manual thread handling.

    Common Use Cases#

    • Logging user activity in a separate thread.
    • Sending emails or SMS notifications asynchronously after user actions.
    • Processing multiple image uploads, database queries, or API calls in parallel.
    • Cleaning up expired database records.
    • Archiving logs.
    • Fetching data from external APIs regularly.
    • Performing regular health checks on external systems or services.

    Setting Up Scheduled Tasks#

    Steps to Implement#

    1. Enable Scheduling: Add @EnableScheduling to the main application class to activate Spring’s scheduling support.
    2. Annotate the Method: Use @Scheduled on the method you want to schedule.
    3. Method Requirements: The method must be void and take no arguments.

    Example Configuration#

    import org.springframework.boot.SpringApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Component; @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } @Component class ScheduledTasks { @Scheduled(fixedRate = 5000) // Runs every 5 seconds public void reportCurrentTime() { System.out.println("Current time: " + new java.util.Date()); } }

    How @Scheduled Works#

    When a method is annotated with @Scheduled, Spring:

    1. Registers it as a scheduled task.
    2. Delegates execution to a TaskScheduler implementation.
    3. Uses a thread pool (defaulting to ScheduledExecutorService) for concurrent task execution.
    4. By default, employs a single-threaded executor, running tasks sequentially.

    Customizing for Concurrency#

    By default, tasks run sequentially. To enable parallel execution, configure a custom TaskScheduler with a thread pool:

    import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; @Configuration public class TaskSchedulingConfig { @Bean public TaskScheduler taskScheduler() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.setPoolSize(5); // Allows 5 tasks to run concurrently scheduler.setThreadNamePrefix("CustomTaskScheduler-"); // Optional: Custom thread name scheduler.initialize(); // Initialize the scheduler return scheduler; } }

    Asynchronous Task Scheduling#

    Blocking Behavior#

    By default, @Scheduled tasks are blocking. For example, if a task takes 5 seconds and is scheduled every 2 seconds, it will only rerun after the previous execution completes. This can lead to delays.

    Enabling Async Execution#

    To avoid blocking, use the @Async annotation:

    1. Add @EnableAsync to the main application class.
    2. Annotate the scheduled method with @Async.

    Example:

    import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @EnableAsync @Component class ScheduledTasks { @Scheduled(fixedRate = 2000) // Every 2 seconds @Async public void asyncTask() { try { Thread.sleep(5000); // Simulates a 5-second task System.out.println("Async task completed at: " + new java.util.Date()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }

    Note: @Async is ignored if the method is called within the same class due to Spring’s proxy mechanism. Move the method to a separate class or use a proxy instance.

    Benefits of @Async#

    • Frees the main thread for other operations by offloading time-consuming tasks to a new thread.
    • Improves responsiveness in applications with heavy scheduled workloads.

    Best Practices#

    • Configure Thread Pool Size: Adjust poolSize based on the number of concurrent tasks to optimize performance.
    • Handle Exceptions: Wrap task logic in try-catch blocks to prevent crashes.
    • Use Appropriate Scheduling Types: Choose fixedRate, fixedDelay, or cron based on needs (e.g., fixedRate for regular intervals, cron for complex schedules).
    • Test Scheduling: Verify task timing and concurrency in a non-production environment.

    Conclusion#

    Spring Boot’s @Scheduled annotation, combined with TaskScheduler and @Async, provides a flexible and powerful way to implement scheduled tasks. Whether you need to log activities, send notifications, or perform health checks, this framework simplifies the process.

    Want to Master Spring Boot and Land Your Dream Job?

    Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease

    Learn more
    Spring Boot Scheduled Tasks
    Spring Boot @Scheduled
    Spring Boot TaskScheduler Configuration

    Subscribe to our newsletter

    Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.

    More articles