KafkaResumeAdapter.java

Overview

`KafkaResumeAdapter.java` is a key implementation class within the Apache Camel Kafka component ecosystem, specifically designed to support resuming Kafka consumer processing from previously stored offsets. It acts as an adapter that bridges Kafka’s consumer offset management with Apache Camel’s resumable consumption framework.

This class implements the `ResumeAdapter`, `Deserializable`, and `Cacheable` interfaces, enabling it to:

Effectively, `KafkaResumeAdapter` facilitates fault-tolerant and exactly-once or at-least-once message processing by allowing Kafka consumers to restart from the last processed message offset tracked externally via a resume cache.


Class and Interface Details

Class: KafkaResumeAdapter

public class KafkaResumeAdapter implements ResumeAdapter, Deserializable, Cacheable

Properties

Property

Type

Description

`consumer`

`Consumer`

Kafka consumer instance used to seek to offsets.

`resumeCache`

`ResumeCache`

Cache storing topic partitions and their offsets.

`LOG`

`Logger`

Logger for diagnostic messages and warnings.


Methods

private boolean resume(TopicPartition topicPartition, Object value)


public void resume()


public boolean deserialize(ByteBuffer keyBuffer, ByteBuffer valueBuffer)


public boolean add(OffsetKey<?> key, Offset<?> offset)


public void setCache(ResumeCache<?> cache)


public ResumeCache<?> getCache()


public void setConsumer(Consumer<?, ?> consumer)


Important Implementation Details


Interaction with the System


Usage Scenario

  1. At startup, the adapter reads offset data from a persistent resume cache via deserialize().

  2. The resume() method is called to seek the Kafka consumer to the stored offsets.

  3. During runtime, as messages are processed, updated offsets are stored via add() into the resume cache.

  4. On failure/restart, the adapter again deserializes offsets and resumes consumption, ensuring minimal message reprocessing.


Mermaid Class Diagram

classDiagram
    class KafkaResumeAdapter {
        - Consumer<?, ?> consumer
        - ResumeCache<TopicPartition> resumeCache
        - static Logger LOG
        + void resume()
        + boolean deserialize(ByteBuffer keyBuffer, ByteBuffer valueBuffer)
        + boolean add(OffsetKey<?> key, Offset<?> offset)
        + void setCache(ResumeCache<?> cache)
        + ResumeCache<?> getCache()
        + void setConsumer(Consumer<?, ?> consumer)
        - boolean resume(TopicPartition topicPartition, Object value)
    }

    KafkaResumeAdapter ..|> ResumeAdapter
    KafkaResumeAdapter ..|> Deserializable
    KafkaResumeAdapter ..|> Cacheable

    class ResumeAdapter
    class Deserializable
    class Cacheable

    KafkaResumeAdapter --> Consumer : controls
    KafkaResumeAdapter --> ResumeCache : stores offsets

Summary

`KafkaResumeAdapter.java` is an adapter class enabling Kafka consumers in Apache Camel to resume message consumption from externally stored offsets. By implementing key interfaces (`ResumeAdapter`, `Deserializable`, and `Cacheable`), it supports serialization/deserialization of offset state, cache management, and consumer offset seeking. This class is essential in providing fault tolerance and reliable message processing guarantees in Kafka-based integration scenarios within Apache Camel.