Skip to content

Spring Boot Scheduling Cron

DodaTech 1 min read

In this tutorial, you'll learn about Fix Spring Boot @Scheduled Cron Not Firing. We cover key concepts, practical examples, and best practices.

The Problem

Scheduled tasks with @Scheduled never execute at the configured cron interval.

Quick Fix

Enable scheduling

Wrong:

// No @EnableScheduling

Output:

Scheduled tasks not executed

Right:

@SpringBootApplication
@EnableScheduling
public class Application { }

Output:

Scheduling enabled

Use correct cron expression

Wrong:

@Scheduled(cron = "0 0 * * * *")
// 7 fields instead of 6

Output:

Cron parsing error

Right:

@Scheduled(cron = "0 0 * * * *")
// Standard: sec min hour day month dow

Output:

Correct 6-field cron

Bean must be a Spring bean

Wrong:

@Scheduled(cron = "0 * * * * *")
public class NotAService { } // Not a bean

Output:

Task never scheduled

Right:

@Component
public class ScheduledTask {
    @Scheduled(cron = "0 * * * * *")
    public void run() { }
}

Output:

Task scheduled correctly

Prevention

  • Add @EnableScheduling to configuration
  • Use standard 6-field cron expressions
  • Ensure the class is a Spring @Component

Common Mistakes with boot scheduling cron

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world SPRING code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does @Scheduled only run once?

The scheduled method may be throwing an exception. Uncaught exceptions stop future executions.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro