KafkaConsumerLastRecordHeaderIT.java

Overview

`KafkaConsumerLastRecordHeaderIT.java` is an integration test class within the Apache Camel Kafka component suite. Its primary purpose is to verify that Kafka consumer message headers related to record commit boundaries are correctly set when consuming messages with auto-commit disabled (`autoCommitEnable=false`). Specifically, it tests that the header `KafkaConstants.LAST_RECORD_BEFORE_COMMIT` is present and correctly indicates the last record before a commit operation, and that `KafkaConstants.LAST_POLL_RECORD` is properly marked.

This test ensures that when consuming messages from a Kafka topic, the Camel Kafka consumer correctly annotates the last record in each batch of polled records, which is crucial for consumers that manage offset commits manually. The test produces a series of messages to a Kafka topic and verifies that the consumer processes them with the expected headers.


Detailed Explanation

Class: KafkaConsumerLastRecordHeaderIT

Description

An integration test class that extends `BaseKafkaTestSupport` (presumably a base class that sets up the Kafka and Camel test infrastructure). It tests Kafka consumer behavior regarding message headers indicating the last record before commit and the last record in a poll batch for manual offset commit scenarios.

Properties

Property Name

Type

Description

`LOG`

`Logger`

Logger instance for logging test execution details.

`TOPIC`

`String`

Kafka topic name used for the test (`"last-record"`).

`producer`

`KafkaProducer`

Kafka producer instance used to send test messages.

Methods


@BeforeEach public void before()


@AfterEach public void after()


@Test public void shouldStartFromBeginningWithEmptyOffsetRepository() throws InterruptedException


protected RouteBuilder createRouteBuilder()


Important Implementation Details


Interaction with Other System Components


Usage Summary

This test class is primarily used by Apache Camel Kafka developers or maintainers to ensure that Kafka consumers correctly mark the last message before offset commits when manual commit is enabled. It is a part of the integration test suite executed during builds or testing cycles.


Example Workflow

  1. Setup: Kafka producer is initialized.

  2. Message Production: Five messages are sent to the Kafka topic "last-record".

  3. Route Consumption: Camel route consumes messages with manual offset commit.

  4. Mock Endpoint Validation: Messages are intercepted by mock:result.

  5. Header Verification: For each message, headers indicating commit boundaries are validated.

  6. Cleanup: Producer is closed and Kafka topic deleted.


Mermaid Class Diagram

classDiagram
    class KafkaConsumerLastRecordHeaderIT {
        - static final Logger LOG
        - static final String TOPIC
        - KafkaProducer<String, String> producer
        + void before()
        + void after()
        + void shouldStartFromBeginningWithEmptyOffsetRepository() throws InterruptedException
        + RouteBuilder createRouteBuilder()
    }
    KafkaConsumerLastRecordHeaderIT --|> BaseKafkaTestSupport

Summary

`KafkaConsumerLastRecordHeaderIT.java` is a focused integration test validating Kafka consumer header correctness in manual commit scenarios within Apache Camel's Kafka component. It ensures that consumers correctly mark the last record before committing offsets, which is essential for reliable offset management and exactly-once processing semantics. The class leverages Camel's testing utilities and Kafka produce/consume APIs to simulate real-world usage and assert expected behaviors.