KafkaConsumerIdempotentGroupIdIT.java

Overview

`KafkaConsumerIdempotentGroupIdIT.java` is an integration test class designed to verify the idempotent consumption of messages from a Kafka topic using Apache Camel's Kafka component. Specifically, it tests that messages with numeric headers can be consumed exactly once when idempotency is enabled via a Kafka-backed idempotent repository.

This test class:

The test ensures that the Kafka consumer's idempotent feature works correctly when using group IDs, thus preventing duplicate message processing in distributed consumption scenarios.


Classes and Methods

Class: KafkaConsumerIdempotentGroupIdIT

This is the main test class that extends `KafkaConsumerIdempotentTestSupport` (not shown here, but presumably provides common Kafka testing utilities).

Fields

Field Name

Type

Description

`TOPIC`

`String` (static final)

Unique Kafka topic name for sending test messages, generated with UUID prefix.

`REPOSITORY_TOPIC`

`String` (static final)

Unique Kafka topic name used by `KafkaIdempotentRepository` to store processed message keys.

`size`

`int`

Number of messages to produce and test (set to 200).

`testIdempotent`

`KafkaIdempotentRepository`

Idempotent repository bound to Camel registry for deduplication persistence.

Static Initializer

Static Methods

Lifecycle Methods

Registry Binding

Method: createRouteBuilder()

Returns a Camel `RouteBuilder` that defines the route under test:

This route tests that messages with the same `"id"` header are consumed only once.

Test Method: kafkaIdempotentMessageIsConsumedByCamel()


Important Implementation Details


Interactions with Other Components


Usage Example

This test runs automatically as part of the integration test suite if the system property `enable.kafka.consumer.idempotency.tests` is not set to `false`. It verifies that messages sent to a Kafka topic are consumed once and only once by a Camel route with idempotent consumer enabled.

To manually run the test:

mvn test -Dtest=KafkaConsumerIdempotentGroupIdIT

To disable the test:

mvn test -Denable.kafka.consumer.idempotency.tests=false

Mermaid Class Diagram

classDiagram
    class KafkaConsumerIdempotentGroupIdIT {
        - static final String TOPIC
        - static final String REPOSITORY_TOPIC
        - final int size = 200
        - KafkaIdempotentRepository testIdempotent
        + static void createRepositoryTopic()
        + static void removeRepositoryTopic()
        + void before()
        + void after()
        + RouteBuilder createRouteBuilder()
        + void kafkaIdempotentMessageIsConsumedByCamel()
    }

    KafkaConsumerIdempotentGroupIdIT --|> KafkaConsumerIdempotentTestSupport
    KafkaConsumerIdempotentGroupIdIT o-- KafkaIdempotentRepository : uses
    KafkaConsumerIdempotentGroupIdIT --> RouteBuilder : creates

Summary

`KafkaConsumerIdempotentGroupIdIT.java` is a focused integration test validating Apache Camel Kafka component's idempotent consumer feature using a Kafka-backed repository. It ensures that messages with numeric IDs in headers are only processed once per consumer group, preventing duplicates in distributed Kafka consumption scenarios. The test dynamically manages Kafka topics, configures Camel routes with idempotent consumers, and uses mock endpoints for verification, serving as a reliable example of idempotency integration in event-driven applications.