SeekPolicyPartitionAssignmentAdapter.java


Overview

The `SeekPolicyPartitionAssignmentAdapter` class provides a partition assignment strategy for Kafka consumers within the Apache Camel Kafka component. This adapter implements a resume strategy based on Camel's `SeekPolicy` configuration, allowing a Kafka consumer to seek to either the beginning or the end of assigned partitions upon partition assignment.

In Kafka consumer processing, when partitions are assigned to a consumer (such as during a rebalance), the consumer can control where to start consuming messages. This class encapsulates the logic for seeking based on a configured policy (`BEGINNING` or `END`), enabling flexible and controlled resumption of consumption.


Class: SeekPolicyPartitionAssignmentAdapter

Package

`org.apache.camel.component.kafka.consumer.support.classic`

Implements

Purpose

Handles Kafka partition assignment events by seeking the consumer's position according to a `SeekPolicy`. This allows starting consumption from the beginning or end of the assigned partitions, based on the configured policy.

Properties

Property

Type

Description

`seekPolicy`

`SeekPolicy`

The configured seek policy (BEGINNING or END).

`consumer`

`Consumer`

The Kafka consumer instance to control.

`LOG`

`Logger`

Logger instance for debug messages.

Constructor

public SeekPolicyPartitionAssignmentAdapter(SeekPolicy seekPolicy)

Methods

void setConsumer(Consumer<?, ?> consumer)

void handlePartitionAssignment()


Important Implementation Details


Interaction with Other System Components


Usage Scenario Example

Suppose you have a Kafka consumer integrated with Apache Camel, and you want to ensure that whenever partitions are assigned (e.g., after a rebalance), the consumer starts reading messages from the beginning of the partitions:

SeekPolicyPartitionAssignmentAdapter adapter = new SeekPolicyPartitionAssignmentAdapter(SeekPolicy.BEGINNING);
adapter.setConsumer(kafkaConsumer);
adapter.handlePartitionAssignment();

This ensures the consumer processes all messages from the start of the assigned partitions.


Mermaid Class Diagram

classDiagram
    class SeekPolicyPartitionAssignmentAdapter {
        -seekPolicy: SeekPolicy
        -consumer: Consumer<?, ?>
        +SeekPolicyPartitionAssignmentAdapter(seekPolicy: SeekPolicy)
        +setConsumer(consumer: Consumer<?, ?>): void
        +handlePartitionAssignment(): void
    }
    SeekPolicyPartitionAssignmentAdapter ..|> PartitionAssignmentAdapter

    class SeekPolicy {
        <<enumeration>>
        +BEGINNING
        +END
    }

    SeekPolicyPartitionAssignmentAdapter --> "1" SeekPolicy
    SeekPolicyPartitionAssignmentAdapter --> "1" Consumer

Summary

`SeekPolicyPartitionAssignmentAdapter.java` provides a focused implementation of a partition assignment strategy based on Camel's `SeekPolicy`. By controlling Kafka consumer offsets at partition assignment time, it ensures flexible resumption behavior in message consumption, contributing to the robustness and configurability of Apache Camel's Kafka integration.