ResumeRebalanceListener.java


Overview

`ResumeRebalanceListener` is a Java class implementing Kafka's `ConsumerRebalanceListener` interface. It is part of the Apache Camel Kafka component, specifically supporting the **resume strategies** mechanism that handles offset management for Kafka consumers.

The primary responsibility of this class is to listen to partition rebalance events on Kafka consumers and integrate with the resume offset management system. It ensures that offsets are committed appropriately when partitions are revoked and that the consumer resumes from the correct offsets when partitions are assigned.

This class acts as a bridge between Kafka's rebalance lifecycle and the resume strategy logic, coordinating commit operations and resume actions to maintain fault-tolerant and consistent message processing.


Package

package org.apache.camel.component.kafka.consumer.support.resume;

Dependencies


Class: ResumeRebalanceListener

public class ResumeRebalanceListener implements ConsumerRebalanceListener

Description

This class listens to Kafka consumer partition rebalance events. It handles:

Fields

Field Name

Type

Description

`LOG`

Logger

Logger instance for debugging and info messages.

`threadId`

String

Identifier for the consumer thread using this listener.

`configuration`

KafkaConfiguration

Kafka consumer configuration, used to determine commit behavior.

`commitManager`

CommitManager

Manages offset commit operations outside Kafka's auto-commit.

`resumeAdapter`

KafkaResumeAdapter

Adapter responsible for handling resume offset state and consumer.

Constructor

public ResumeRebalanceListener(String threadId,
                               KafkaConfiguration configuration,
                               CommitManager commitManager,
                               Consumer<?, ?> consumer,
                               ResumeStrategy resumeStrategy)

Parameters

Behavior


Methods

onPartitionsRevoked

@Override
public void onPartitionsRevoked(Collection<TopicPartition> partitions)

onPartitionsAssigned

@Override
public void onPartitionsAssigned(Collection<TopicPartition> partitions)

Important Implementation Details


Interaction with Other Components


Usage Example

// Assume these components are created and configured elsewhere
String threadId = "consumer-thread-1";
KafkaConfiguration kafkaConfig = new KafkaConfiguration();
CommitManager commitManager = new CommitManager();
Consumer<String, String> kafkaConsumer = createKafkaConsumer();
ResumeStrategy resumeStrategy = getResumeStrategyInstance();

// Instantiate the rebalance listener
ResumeRebalanceListener rebalanceListener = new ResumeRebalanceListener(
    threadId,
    kafkaConfig,
    commitManager,
    kafkaConsumer,
    resumeStrategy
);

// Attach listener to Kafka consumer
kafkaConsumer.subscribe(
    topics,
    rebalanceListener
);

In this example, the listener will commit offsets when partitions are revoked and trigger resume logic when partitions are assigned.


Visual Diagram: Class Structure

classDiagram
    class ResumeRebalanceListener {
        - static final Logger LOG
        - String threadId
        - KafkaConfiguration configuration
        - CommitManager commitManager
        - KafkaResumeAdapter resumeAdapter
        + ResumeRebalanceListener(String, KafkaConfiguration, CommitManager, Consumer<?,?>, ResumeStrategy)
        + void onPartitionsRevoked(Collection~TopicPartition~)
        + void onPartitionsAssigned(Collection~TopicPartition~)
    }
    ResumeRebalanceListener ..> KafkaConfiguration : uses
    ResumeRebalanceListener ..> CommitManager : uses
    ResumeRebalanceListener ..> KafkaResumeAdapter : uses
    ResumeRebalanceListener ..> Consumer : uses
    ResumeRebalanceListener ..> ResumeStrategy : uses

Summary

`ResumeRebalanceListener` is a specialized Kafka consumer rebalance listener tailored for the Apache Camel Kafka component's resume offset strategies. It:

This class plays a crucial role in enabling the fault-tolerant behavior of Kafka consumers by ensuring offsets are safely managed across rebalances, contributing to reliable message processing in distributed systems.