Skip to content

Micronaut Retry

DodaTech 1 min read

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

The Problem

@Retryable annotated methods do not retry after failure.

Quick Fix

Add @Retryable annotation

Wrong:

public Product fetch(Long id) { } // No @Retryable

Output:

No retry

Right:

@Retryable(attempts = "3", delay = "1s")
public Product fetch(Long id) { }

Output:

Retries 3 times

Configure retry for specific exceptions

Wrong:

@Retryable(attempts = "3")
public Product fetch(Long id) { throw new RuntimeException(); }

Output:

Retries all exceptions

Right:

@Retryable(attempts = "3", includes = TimeoutException.class)
public Product fetch(Long id) { }

Output:

Retries only TimeoutException

Use @CircuitBreaker

Wrong:

@Retryable(attempts = "10")
public Product fetch(Long id) { } // Always retries

Output:

Exhaustion

Right:

@CircuitBreaker(reset = "30s", attempts = "5")
public Product fetch(Long id) { }

Output:

Opens circuit after 5 failures

Prevention

  • Annotate methods with @Retryable
  • Specify includes/excludes for exception filtering
  • Use @CircuitBreaker for resilience patterns

Common Mistakes with retry

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

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

### Difference between @Retryable and @CircuitBreaker?

@Retryable always retries. @CircuitBreaker opens the circuit after max attempts and stops calling the method until the reset period.

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