KafkaConsumerIdempotentIT.java


Overview

`KafkaConsumerIdempotentIT.java` is an integration test class for the Apache Camel Kafka component, specifically validating the idempotent consumption feature when using Kafka consumers. The primary goal of this test is to ensure that messages are consumed exactly once by the Camel route when Kafka's idempotent consumer pattern is applied, even when message headers are numeric types.

This test class:


Class: KafkaConsumerIdempotentIT

Inheritance

This base test support class likely provides common setup and utility methods for Kafka consumer idempotency testing but is not included in this file.


Constants

Name

Type

Description

`TOPIC`

String

Kafka topic name for producing test messages, unique per test run.

`REPOSITORY_TOPIC`

String

Kafka topic used as the backing store for the idempotent repository.

These topics are dynamically generated with a UUID suffix to avoid conflicts between test runs.


Fields

Name

Type

Description

`size`

int

Number of test messages to send (default 200).

`testIdempotent`

KafkaIdempotentRepository

Kafka-backed idempotent repository bound to the Camel registry with name `"kafkaIdempotentRepository"`.


Lifecycle Methods

Method

Description

`@BeforeAll createRepositoryTopic()`

Creates the Kafka topic used for the idempotent repository before any tests run.

`@AfterAll removeRepositoryTopic()`

Deletes the repository topic after all tests finish.

`@BeforeEach before()`

Sends `size` messages to the `TOPIC` before each test method.

`@AfterEach after()`

Deletes the main test topic (`TOPIC`) after each test to clean up.


Route Definition

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        @Override
        public void configure() {
            from("kafka:" + TOPIC
                 + "?groupId=KafkaConsumerIdempotentIT&autoOffsetReset=earliest"
                 + "&keyDeserializer=org.apache.kafka.common.serialization.StringDeserializer"
                 + "&valueDeserializer=org.apache.kafka.common.serialization.StringDeserializer"
                 + "&autoCommitIntervalMs=1000&pollTimeoutMs=1000&autoCommitEnable=true"
                 + "&interceptorClasses=org.apache.camel.component.kafka.MockConsumerInterceptor")
            .routeId("foo")
            .idempotentConsumer(numericHeader("id"))
            .idempotentRepository("kafkaIdempotentRepository")
            .to(KafkaTestUtil.MOCK_RESULT);
        }
    };
}

**Explanation:**


Test Method

@Test
@DisplayName("Numeric headers is consumable when using idempotent (CAMEL-16914)")
void kafkaIdempotentMessageIsConsumedByCamel() {
    MockEndpoint to = contextExtension.getMockEndpoint(KafkaTestUtil.MOCK_RESULT);
    doRun(to, size);
}

Important Implementation Details


Interaction with Other System Components


Usage Example

This class itself is a test and not intended for direct production use. However, it serves as an example for:


Mermaid Class Diagram

classDiagram
    class KafkaConsumerIdempotentIT {
        - static final String TOPIC
        - static final String REPOSITORY_TOPIC
        - final int size = 200
        - final KafkaIdempotentRepository testIdempotent
        + static void createRepositoryTopic()
        + static void removeRepositoryTopic()
        + void before()
        + void after()
        + RouteBuilder createRouteBuilder()
        + void kafkaIdempotentMessageIsConsumedByCamel()
    }
    KafkaConsumerIdempotentIT --|> KafkaConsumerIdempotentTestSupport
    KafkaConsumerIdempotentIT ..> KafkaIdempotentRepository : uses
    KafkaConsumerIdempotentIT ..> RouteBuilder : defines route
    KafkaConsumerIdempotentIT ..> MockEndpoint : verifies consumption

Summary

`KafkaConsumerIdempotentIT.java` is a focused integration test validating Apache Camel's Kafka consumer idempotency feature using Kafka as the repository backend. It ensures that messages with numeric headers are processed exactly once, demonstrating robust idempotent consumption in distributed Kafka environments. This test also provides a practical example of configuring Kafka consumer properties and idempotent repositories in Camel routes.