0Pricing
Spring Boot 4 Complete Guide · Lesson

Scheduling Tasks with @Scheduled

Run background jobs on fixed intervals or cron schedules using Spring's built-in task scheduling support.

Scheduling Tasks with @Scheduled is a free Spring Boot 4 Complete Guide lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Complete Guide learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Scheduled Tasks?

Many applications need work to run periodically: cleaning up data, sending digests, refreshing caches. Spring provides scheduling without an external job framework.

Enabling Scheduling

Add @EnableScheduling to a configuration class to activate Spring's scheduler.

@EnableScheduling
@Configuration
public class SchedulingConfig {
}

Fixed Rate Execution

The fixedRate option runs a method every N milliseconds, measured from the start of each run.

@Scheduled(fixedRate = 5000)
public void poll() {
    System.out.println("Polling");
}

Fixed Delay Execution

fixedDelay waits N milliseconds after the previous run finishes, preventing overlap when a task takes a while.

@Scheduled(fixedDelay = 5000)
public void process() {
}

Rate vs Delay

Use fixedRate for steady cadence and fixedDelay when each run must fully complete before the next begins. Choosing wrong can cause overlapping executions.

Cron Expressions

For calendar-based timing use cron. The expression has six fields: second, minute, hour, day, month, weekday.

@Scheduled(cron = "0 0 2 * * *")
public void nightlyJob() {
}

Reading the Cron Above

0 0 2 * * * means at second 0, minute 0, hour 2, every day, every month, any weekday, that is, 2 AM daily.

Initial Delay

The initialDelay attribute postpones the first execution, useful for letting the app warm up before background work begins.

@Scheduled(fixedRate = 5000, initialDelay = 10000)
public void start() {
}

Avoiding Blocking the Scheduler

By default a single thread runs all scheduled tasks. A long task can delay others, so configure a thread pool or pair scheduling with @Async for heavy work.

Scheduling in Clusters

If you run multiple instances, each will fire the same schedule. Use a distributed lock (for example ShedLock) to ensure a job runs once across the cluster.

Externalizing the Schedule

Put the cron expression in properties so you can change timing per environment without recompiling.

@Scheduled(cron = "${cleanup.cron}")
public void cleanup() {
}

Quick Check

Test your understanding of scheduled tasks.

Recap

You enabled scheduling and used fixedRate, fixedDelay, cron expressions, and initial delays. You also learned to externalize schedules and to coordinate jobs safely across a cluster.

Frequently asked questions

Is the “Scheduling Tasks with @Scheduled” lesson free?

Yes — the full text of “Scheduling Tasks with @Scheduled” is free to read here on the web, and the Spring Boot 4 Complete Guide course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Complete Guide course, upgrade to CoddyKit PRO.

What will I learn in “Scheduling Tasks with @Scheduled”?

Run background jobs on fixed intervals or cron schedules using Spring's built-in task scheduling support. You practise Spring Boot 4 Complete Guide with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Complete Guide?

No prior experience is required. Spring Boot 4 Complete Guide on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Scheduling Tasks with @Scheduled” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Complete Guide lesson?

Yes. Every Spring Boot 4 Complete Guide lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Asynchronous Methods with @Async
  2. Introduction to Message Queues
  3. Integrating RabbitMQ/Kafka
  4. Scheduling Tasks with @Scheduled
← Back to Spring Boot 4 Complete Guide