Bridge Error Strategy

Purpose

The Bridge Error Strategy addresses the need for flexible and customizable error handling during Kafka consumer polling within Apache Camel routes. Unlike fixed behaviors such as discarding messages or stopping the consumer, this strategy delegates exception handling to Camel's built-in error handler bridge. This allows route developers to integrate Kafka polling exceptions seamlessly into Camel's rich error handling framework, supporting retries, dead-letter channels, or other customized error processing.

Functionality

When a polling exception occurs, the Bridge Error Strategy:

This approach combines Kafka offset management with Camel’s error handling capabilities, allowing for sophisticated recovery flows managed by Camel routes.

Key Methods and Workflow

Code Snippet Illustrating Core Interaction

@Override
public void handle(long partitionLastOffset, Exception exception) {
    LOG.warn("Deferring processing to the exception handler based on polling exception strategy");

    // Delegate exception to Camel's bridge error handler
    recordFetcher.getBridge().handleException(exception);

    // Skip the poison message by seeking to next offset
    SeekUtil.seekToNextOffset(consumer, partitionLastOffset);

    // Stop polling on authentication or authorization failures
    if (exception instanceof AuthenticationException || exception instanceof AuthorizationException) {
        continueFlag = false;
    }
}

Integration with Parent Topic and Other Subtopics

The Bridge Error Strategy is one of several poll exception handling strategies under the "Kafka Consumer Error Handling" parent topic. It complements other strategies such as:

Unlike these, the Bridge Error Strategy uniquely integrates with Camel's native error handling mechanisms, enabling:

This strategy interacts closely with `KafkaFetchRecords` for managing message offsets and relies on the consumer instance for seeking offsets. It complements manual commit and resume strategies by ensuring that error handling is consistent and offsets progress despite exceptions.

Diagram

sequenceDiagram
    participant KafkaConsumer as Kafka Consumer
    participant BridgeStrategy as Bridge Error Strategy
    participant KafkaFetchRecords as Record Fetcher
    participant CamelErrorHandler as Camel Error Handler Bridge

    KafkaConsumer->>BridgeStrategy: Poll Exception Occurs (exception, partitionOffset)
    BridgeStrategy->>KafkaFetchRecords: getBridge().handleException(exception)
    KafkaFetchRecords->>CamelErrorHandler: handleException(exception)
    BridgeStrategy->>KafkaConsumer: Seek to next offset (partitionOffset + 1)
    alt Authentication/Authorization Exception
        BridgeStrategy-->>KafkaConsumer: Signal stop polling (canContinue = false)
    else Other Exception
        BridgeStrategy-->>KafkaConsumer: Continue polling (canContinue = true)
    end

This sequence illustrates how the Bridge Error Strategy defers exception processing to Camel’s error handler, advances the Kafka consumer offset to avoid stuck processing, and controls whether polling should continue based on exception type.