ReconnectErrorStrategy.java


Overview

`ReconnectErrorStrategy.java` defines an error handling strategy for Kafka consumers within the Apache Camel Kafka component. This strategy implements the `PollExceptionStrategy` interface and focuses on recovering from polling exceptions by requesting a consumer reconnect, except in the case of non-recoverable authentication errors.

The primary goal of this strategy is to enhance consumer resilience by allowing automatic recovery from transient network or broker issues through reconnection cycles, while preventing futile retries on unrecoverable authentication failures.


Class: ReconnectErrorStrategy

Package

org.apache.camel.component.kafka.consumer.errorhandler

Dependencies

Purpose

The `ReconnectErrorStrategy` class implements a polling exception strategy that reacts to Kafka consumer poll failures by:

Class Diagram

classDiagram
    class ReconnectErrorStrategy {
        - KafkaFetchRecords recordFetcher
        - boolean retry
        + ReconnectErrorStrategy(KafkaFetchRecords)
        + void reset()
        + boolean canContinue()
        + void handle(long partitionLastOffset, Exception exception)
    }
    ReconnectErrorStrategy ..|> PollExceptionStrategy

Detailed Class Description

Fields

Field

Type

Description

`recordFetcher`

`KafkaFetchRecords`

Reference to the fetching component controlling consumer state and lifecycle.

`retry`

`boolean`

Internal flag indicating if polling can continue immediately after handling an exception.

Constructor

public ReconnectErrorStrategy(KafkaFetchRecords recordFetcher)

Methods

void reset()

ReconnectErrorStrategy strategy = new ReconnectErrorStrategy(fetcher);
// After handling an error and stopping retry, reset to allow retrying again
strategy.reset();

boolean canContinue()

if (!strategy.canContinue()) {
    // Stop current poll loop and trigger reconnect lifecycle
}

void handle(long partitionLastOffset, Exception exception)

try {
    consumer.poll();
} catch (Exception e) {
    strategy.handle(lastOffset, e);
    if (!strategy.canContinue()) {
        // Close consumer and reconnect on next cycle
    }
}

Important Implementation Details


Interaction with Other System Components


Usage in Application Context

When configured as the active poll exception strategy in Apache Camel’s Kafka component, `ReconnectErrorStrategy` ensures that the consumer:


Visual Diagram: Sequence Diagram of Exception Handling Workflow

sequenceDiagram
    participant Fetcher as KafkaFetchRecords
    participant Strategy as ReconnectErrorStrategy
    participant Kafka as Kafka Broker

    Fetcher->>Kafka: poll()
    Kafka-->>Fetcher: throws Exception
    Fetcher->>Strategy: handle(partitionLastOffset, exception)
    alt AuthenticationException (Non-recoverable)
        Strategy->>Fetcher: setReconnect(false)
        Strategy->>Fetcher: setConnected(false)
        Strategy->>Strategy: retry = false
    else Other Exceptions (Recoverable)
        Strategy->>Fetcher: setReconnect(true)
        Strategy->>Fetcher: setConnected(false)
        Strategy->>Strategy: retry = false
    end
    Fetcher->>Fetcher: close consumer
    Fetcher->>Fetcher: on next poll cycle, reconnect if enabled

Summary

`ReconnectErrorStrategy.java` is a key error handling strategy within the Kafka consumer error handling framework of Apache Camel. It enables the Kafka consumer to recover from transient problems by requesting a consumer reconnect, while also identifying and halting on non-recoverable authentication errors.

By manipulating consumer connection flags and signaling poll loop continuation, it helps maintain a resilient message consumption process that can self-heal from many common Kafka consumer disruptions.


End of Documentation for ReconnectErrorStrategy.java