KafkaResumable.java

Overview

`KafkaResumable.java` defines the `KafkaResumable` class, a utility within the Apache Camel Kafka component designed to support resumable consumption of Kafka messages. Its primary purpose is to encapsulate the Kafka consumer state—specifically the topic partition and offset—into a standardized "resumable" object. This allows Camel routes to track and resume message consumption reliably from the last processed offset, ensuring no message loss or duplication in the event of failure or restarts.

The class implements the `Resumable` interface from the Camel resume framework, providing methods to retrieve a unique offset key and the last consumed offset. It also includes a factory method to create instances directly from a Camel `Exchange` with Kafka-specific headers.


Class: KafkaResumable

public final class KafkaResumable implements Resumable

Description

`KafkaResumable` is an immutable class representing the state of a Kafka consumer at a particular topic partition and offset. It implements `Resumable`, which is a contract for objects that can provide an offset key and last offset to support resumption logic in Camel.

Fields

Field

Type

Description

`addressable`

`String`

A unique identifier combining topic and partition (formatted as `"topic/partition"`).

`offset`

`Long`

The last consumed offset in the Kafka partition.

Constructor

private KafkaResumable(String addressable, Long offset)

Methods

OffsetKey<?> getOffsetKey()

KafkaResumable resumable = KafkaResumable.of(exchange);
OffsetKey<?> key = resumable.getOffsetKey();
// key can be used to retrieve or store offset state

Offset<?> getLastOffset()

KafkaResumable resumable = KafkaResumable.of(exchange);
Offset<?> lastOffset = resumable.getLastOffset();
// lastOffset holds the numeric offset to resume from

static KafkaResumable of(Exchange exchange)

KafkaResumable resumable = KafkaResumable.of(exchange);

Implementation Details


Integration and Interaction


Visual Diagram

classDiagram
    class KafkaResumable {
        -String addressable
        -Long offset
        +static KafkaResumable of(Exchange exchange)
        +OffsetKey<?> getOffsetKey()
        +Offset<?> getLastOffset()
    }

    KafkaResumable ..|> Resumable

    class Resumable {
        <<interface>>
        +OffsetKey<?> getOffsetKey()
        +Offset<?> getLastOffset()
    }

    class Exchange {
        +Message getMessage()
    }

    class Message {
        +<T> T getHeader(String name, Class<T> type)
    }

    KafkaResumable --> Exchange : Uses for creation

Summary

`KafkaResumable.java` provides a clean, immutable representation of Kafka consumer state tailored to Apache Camel’s resume framework. It extracts necessary Kafka metadata from exchanges and packages it into a form that is easily tracked and persisted. This enables reliable resumption of Kafka consumers within Camel routes, supporting fault tolerance and exactly-once processing semantics.

This class is a key building block in the Kafka component’s offset management, bridging Kafka-specific details with Camel’s generalized resume mechanism.