Skip to content

Micronaut Scheduled

DodaTech 1 min read

In this tutorial, you'll learn about Fix Micronaut @Scheduled Not Running. We cover key concepts, practical examples, and best practices.

The Problem

@Scheduled tasks in Micronaut never execute at the specified frequency.

Quick Fix

Add @Singleton to task

Wrong:

@Scheduled(fixedDelay = "5s")
public class CleanupTask { // No @Singleton

Output:

Task not registered

Right:

@Singleton
public class CleanupTask {
    @Scheduled(fixedDelay = "5s")
    public void run() { }
}

Output:

Task registered and running

Use correct cron format

Wrong:

@Scheduled(cron = "0 0 * * * *")
// 6 fields as expected

Output:

Correct

Right:

@Scheduled(cron = "*/5 * * * *")
// Every 5 seconds

Output:

Cron expression works

Handle exceptions in tasks

Wrong:

@Scheduled(fixedDelay = "5s")
public void run() {
    throw new RuntimeException();
} // Task stops after error

Output:

No retry

Right:

@Scheduled(fixedDelay = "5s")
public void run() {
    try {
        // risky operation
    } catch (Exception e) {
        log.error("Task failed", e);
    }
}

Output:

Task continues after error

Prevention

  • Annotate task class with @Singleton
  • Use proper cron or fixedDelay/fixedRate expressions
  • Catch exceptions within scheduled methods to prevent cancellation

Common Mistakes with scheduled

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world MICRONAUT 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 the task stop after an exception?

Uncaught exceptions in scheduled tasks 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