Stop Strategy

Purpose

The Stop Strategy provides a fail-safe mechanism for Kafka consumers by immediately stopping consumption when a polling exception occurs. This approach prevents the consumer from continuously encountering errors that could lead to resource exhaustion or inconsistent state. It is particularly useful in scenarios where continued consumption after certain exceptions would be harmful or when manual intervention is preferred before resuming.

Functionality

Upon a polling exception detected during the Kafka consumer's fetch cycle, the Stop Strategy intervenes to halt further polling and processing. Its core behaviors include:

This ensures that once an error triggers the Stop Strategy, the consumer ceases operation until explicitly reset or restarted, avoiding further error propagation.

Key Methods and Workflow

Critical Interaction Snippet

@Override
public void handle(long partitionLastOffset, Exception exception) {
    LOG.warn("Requesting the consumer to stop based on polling exception strategy");
    retry = false;
    recordFetcher.setConnected(false); // Stops the consumer fetch loop
}

@Override
public boolean canContinue() {
    return retry; // False after an error triggers stop
}

Integration

The Stop Strategy integrates tightly with the Kafka consumer's fetch mechanism (`KafkaFetchRecords`) through the `PollExceptionStrategy` interface. It complements other error handling strategies by providing a conservative, defensive approach:

By halting on error, this strategy prevents repeated error cycles that could interfere with offset management, message processing guarantees, or resource stability. It is ideal for critical systems where failing fast and requiring operator intervention is preferable to risking inconsistent consumption.

Diagram

flowchart TD
    A[Start Kafka Consumer Fetch Loop] --> B[Poll Kafka for Records]
    B --> C{Polling Exception Occurs?}
    C -- No --> D[Process Records]
    D --> B
    C -- Yes --> E[Invoke StopErrorStrategy.handle()]
    E --> F[Log Warning and Set retry = false]
    F --> G[Set recordFetcher.connected = false]
    G --> H[Stop Polling Loop]

This flowchart illustrates the consumer's fetch cycle interruption triggered by the Stop Strategy upon encountering a polling exception.


By stopping the consumer decisively on errors, the Stop Strategy ensures controlled shutdown and prevents compounding failures, supporting robust Kafka consumption workflows within Apache Camel.