0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · บทเรียน

กลไกการลองใหม่ด้วย Spring Retry

ผนวก Spring Retry เพื่อพยายามประมวลผลข้อความใหม่โดยอัตโนมัติเมื่อเกิดความล้มเหลวชั่วคราว ซึ่งช่วยเพิ่มความทนทานของแอปพลิเคชัน

กลไกการลองใหม่ด้วย Spring Retry เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Do We Need Retries?

In distributed systems like those using Kafka, operations can sometimes fail due to temporary issues. These are called transient failures.

  • Network glitches
  • Temporary service unavailability
  • Database connection timeouts

Retries help overcome these by automatically re-attempting failed operations, improving system resilience and reducing manual intervention.

Meet Spring Retry

Spring Retry is a powerful framework that simplifies implementing retry logic in your applications. It provides both declarative (using annotations) and programmatic (using RetryTemplate) ways to handle transient errors.

It integrates seamlessly with Spring Boot to make your applications more robust, especially when interacting with external services like Kafka.

RetryTemplate Basics

The RetryTemplate is Spring Retry's programmatic core. It allows you to wrap any code that might fail and define how it should be retried. Let's see a basic example:

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.support.RetryTemplate;

public class BasicRetryDemo {
    private static int attemptCount = 0;

    public static void main(String[] args) {
        RetryTemplate retryTemplate = new RetryTemplate();

        try {
            String result = retryTemplate.execute(
                new RetryCallback<String, RuntimeException>() {
                    @Override
                    public String doWithRetry(RetryContext context) {
                        System.out.println("Executing task. Attempt: " + (++attemptCount));
                        if (attemptCount < 3) {
                            throw new RuntimeException("Simulated service failure!");
                        }
                        return "Task completed successfully!";
                    }
                });
            System.out.println(result);
        } catch (RuntimeException e) {
            System.out.println("Final failure: " + e.getMessage());
        }
    }
}

Limiting Retries: Max Attempts

By default, RetryTemplate retries 3 times (1 initial attempt + 2 retries). You can configure this using a SimpleRetryPolicy. Let's set it to 4 attempts:

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.retry.policy.SimpleRetryPolicy;

import java.util.Collections;

public class MaxAttemptsDemo {
    private static int attemptCount = 0;

    public static void main(String[] args) {
        RetryTemplate retryTemplate = new RetryTemplate();
        
        // Configure max attempts (1 initial + 3 retries)
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(
            4, Collections.singletonMap(RuntimeException.class, true));
        retryTemplate.setRetryPolicy(retryPolicy);

        try {
            String result = retryTemplate.execute(
                new RetryCallback<String, RuntimeException>() {
                    @Override
                    public String doWithRetry(RetryContext context) {
                        System.out.println("Executing task. Attempt: " + (++attemptCount));
                        if (attemptCount < 4) { // Fails first 3 times
                            throw new RuntimeException("Transient error!");
                        }
                        return "Task completed!";
                    }
                });
            System.out.println(result);
        } catch (RuntimeException e) {
            System.out.println("Final failure: " + e.getMessage());
        }
    }
}

Smart Delays: Fixed Backoff

Retrying immediately might overwhelm a temporarily struggling service. A backoff policy introduces a delay between retries. FixedBackOffPolicy waits a set amount of time.

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.backoff.FixedBackOffPolicy;

import java.util.Collections;

public class FixedBackoffDemo {
    private static int attemptCount = 0;

    public static void main(String[] args) {
        RetryTemplate retryTemplate = new RetryTemplate();
        retryTemplate.setRetryPolicy(
            new SimpleRetryPolicy(3, Collections.singletonMap(RuntimeException.class, true)));
        
        // Configure fixed delay of 1000ms (1 second)
        retryTemplate.setBackOffPolicy(new FixedBackOffPolicy(1000L));

        try {
            String result = retryTemplate.execute(
                new RetryCallback<String, RuntimeException>() {
                    @Override
                    public String doWithRetry(RetryContext context) {
                        System.out.println("Attempt: " + (++attemptCount));
                        if (attemptCount < 3) {
                            throw new RuntimeException("Service busy!");
                        }
                        return "Success after retries!";
                    }
                });
            System.out.println(result);
        } catch (RuntimeException e) {
            System.out.println("Final failure: " + e.getMessage());
        }
    }
}

Exponential Backoff

For services that need more time to recover, exponential backoff increases the delay after each retry. This is often more effective than a fixed delay.

ExponentialBackOffPolicy lets you set initial delay, multiplier, and max delay.

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;

import java.util.Collections;

public class ExponentialBackoffDemo {
    private static int attemptCount = 0;

    public static void main(String[] args) {
        RetryTemplate retryTemplate = new RetryTemplate();
        retryTemplate.setRetryPolicy(
            new SimpleRetryPolicy(4, Collections.singletonMap(RuntimeException.class, true)));
        
        // Configure exponential backoff
        ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
        backOffPolicy.setInitialInterval(100L); // 100ms
        backOffPolicy.setMultiplier(2.0);      // Doubles each time
        backOffPolicy.setMaxInterval(2000L);   // Max 2 seconds
        retryTemplate.setBackOffPolicy(backOffPolicy);

        try {
            String result = retryTemplate.execute(
                new RetryCallback<String, RuntimeException>() {
                    @Override
                    public String doWithRetry(RetryContext context) {
                        System.out.println("Attempt: " + (++attemptCount));
                        if (attemptCount < 4) {
                            throw new RuntimeException("Resource contention!");
                        }
                        return "Success after exponential backoff!";
                    }
                });
            System.out.println(result);
        } catch (RuntimeException e) {
            System.out.println("Final failure: " + e.getMessage());
        }
    }
}

Retry on Specific Errors

You might only want to retry on certain types of exceptions, not all. SimpleRetryPolicy allows you to specify which exceptions should trigger a retry.

Exceptions not in the list will cause immediate failure.

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.retry.policy.SimpleRetryPolicy;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SpecificExceptionDemo {
    private static int attemptCount = 0;

    public static void main(String[] args) {
        RetryTemplate retryTemplate = new RetryTemplate();
        
        Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>();
        retryableExceptions.put(IOException.class, true); // Only retry IOException
        retryableExceptions.put(MyCustomTransientException.class, true); 

        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(3, retryableExceptions);
        retryTemplate.setRetryPolicy(retryPolicy);

        try {
            String result = retryTemplate.execute(
                new RetryCallback<String, Exception>() { // Note: now throws Exception
                    @Override
                    public String doWithRetry(RetryContext context) throws Exception {
                        System.out.println("Attempt: " + (++attemptCount));
                        if (attemptCount == 1) {
                            throw new IOException("Network issue!"); // Will retry
                        } else if (attemptCount == 2) {
                            throw new MyCustomTransientException("DB lock!"); // Will retry
                        } else if (attemptCount == 3) {
                            throw new IllegalArgumentException("Bad data!"); // Will NOT retry
                        }
                        return "Success!";
                    }
                });
            System.out.println(result);
        } catch (Exception e) {
            System.out.println("Final failure: " + e.getClass().getSimpleName() + " - " + e.getMessage());
        }
    }
    
    static class MyCustomTransientException extends RuntimeException {
        public MyCustomTransientException(String message) { super(message); }
    }
}

Handling Final Failures: @Recover

What happens if all retries are exhausted and the operation still fails? You need a fallback!

In a Spring context, the @Recover annotation marks a method to be called when a @Retryable method permanently fails. It lets you provide alternative logic or gracefully log the error.

For RetryTemplate, you can provide a RecoveryCallback to achieve similar fallback behavior.

Spring Kafka Listener Retries

For Spring Boot Kafka consumers, you can apply the @Retryable annotation directly to your @KafkaListener methods. This ensures that if message processing fails, the listener will retry before the message is potentially sent to a Dead Letter Topic (DLT).

Remember to enable Spring Retry with @EnableRetry on your application class!

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import org.springframework.retry.backoff.Backoff;

// This is a conceptual example for a Kafka Listener.
// It requires a running Kafka broker and Spring Boot app.
@Component
public class MyKafkaListener {
    private int processAttempts = 0;

    @KafkaListener(topics = "myTopic", groupId = "myGroup")
    @Retryable(
        value = {RuntimeException.class}, // Retry on RuntimeException
        maxAttempts = 5,
        backoff = @Backoff(delay = 1000) // Initial 1s delay
    )
    public void listen(String message) {
        processAttempts++;
        System.out.println("Processing message: '" + message + "' (Attempt " + processAttempts + ")");
        if (processAttempts < 3) { // Simulate failure for first 2 processing attempts
            throw new RuntimeException("Failed to process: " + message);
        }
        processAttempts = 0; // Reset for next message
        System.out.println("Successfully processed: " + message);
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("This code demonstrates @Retryable on a @KafkaListener method.");
        System.out.println("It would run within a Spring Boot application connected to Kafka.");
    }
}

Retry Configuration Check

You are building a Kafka consumer that processes orders. Sometimes, the external payment service is temporarily unavailable. You want to retry processing an order up to 5 times, with an initial delay of 500ms, doubling each time, but not exceeding 5 seconds. Which configuration for @Retryable is correct?

Recap: Spring Retry

In this lesson, you learned how to make your Kafka consumers more resilient using Spring Retry:

  • Understood the need for retries for transient failures in distributed systems.
  • Explored RetryTemplate for programmatic retry logic, demonstrating its core features.
  • Learned how to configure maxAttempts and different BackOffPolicy strategies (fixed and exponential).
  • Understood the role of @Retryable and @Recover annotations for declarative retries in a Spring context, particularly for @KafkaListener methods.

Next, we'll explore Dead Letter Topics (DLT) for handling messages that permanently fail after all retries, ensuring no data is lost.

คำถามที่พบบ่อย

บทเรียน “กลไกการลองใหม่ด้วย Spring Retry” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กลไกการลองใหม่ด้วย Spring Retry” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กลไกการลองใหม่ด้วย Spring Retry”

ผนวก Spring Retry เพื่อพยายามประมวลผลข้อความใหม่โดยอัตโนมัติเมื่อเกิดความล้มเหลวชั่วคราว ซึ่งช่วยเพิ่มความทนทานของแอปพลิเคชัน คุณปฏิบัติ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “กลไกการลองใหม่ด้วย Spring Retry” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม

ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การจัดการข้อยกเว้นของผู้บริโภค
  2. กลไกการลองใหม่ด้วย Spring Retry
  3. การใช้งานหัวข้อจดหมายตีกลับ (DLT)
  4. การลองใหม่แบบไม่บล็อกด้วยหัวข้อการลองใหม่
← กลับไปที่ Advanced Spring Boot 4: Event-Driven Architecture (Kafka)