KafkaIdempotentRepositoryNonEagerIT.java

Overview

`KafkaIdempotentRepositoryNonEagerIT` is an integration test class designed to validate the behavior of the **non-eager** mode of the Apache Camel Kafka-based idempotent repository (`KafkaIdempotentRepository`). This class ensures that duplicate messages are correctly filtered out when processed through Camel routes using the Kafka idempotent repository but with lazy (non-eager) idempotent checking.

The tests in this file verify two main aspects:

The class extends a base test class `SimpleIdempotentTest` (not included here), which presumably provides foundational Camel test infrastructure.


Detailed Explanation

Package and Dependencies


Class: KafkaIdempotentRepositoryNonEagerIT

Description

This class defines integration tests for the `KafkaIdempotentRepository` configured with eager mode disabled (`eager(false)`). It verifies that the idempotent consumer correctly filters duplicates lazily during message consumption in a Camel route.

Annotations

Fields

Field

Description

`REPOSITORY_TOPIC`

Kafka topic name for storing idempotent keys, unique per test run using UUID.

`kafkaIdempotentRepository`

Instance of `KafkaIdempotentRepository` bound to Camel registry, uses the topic above and Kafka bootstrap servers.


Methods

createRepositoryTopic()

configureKafka(CamelContext context)

createRouteBuilder()

testRemovesDuplicates()

ProducerTemplate template = contextExtension.getProducerTemplate();
for (int i = 0; i < 10; i++) {
    template.sendBodyAndHeader("direct:in", "Test message", "id", i % 5);
}

testRollsBackOnException()

mockOut.whenAnyExchangeReceived(exchange -> {
    int id = exchange.getIn().getHeader("id", Integer.class);
    if (id == 0) {
        throw new IllegalArgumentException("Boom!");
    }
});

Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class KafkaIdempotentRepositoryNonEagerIT {
        - static final String REPOSITORY_TOPIC
        - KafkaIdempotentRepository kafkaIdempotentRepository
        + static void createRepositoryTopic()
        + void configureKafka(CamelContext context)
        + RouteBuilder createRouteBuilder()
        + void testRemovesDuplicates()
        + void testRollsBackOnException()
    }
    KafkaIdempotentRepositoryNonEagerIT --|> SimpleIdempotentTest

Summary

`KafkaIdempotentRepositoryNonEagerIT` is a focused integration test class that validates the lazy (non-eager) mode of Apache Camel's Kafka-backed idempotent repository. It ensures duplicate messages are filtered and that processing exceptions cause correct rollback behavior. This helps guarantee that the Kafka idempotent repository behaves correctly in distributed and fault-tolerant Camel routes.

The class showcases practical usage of Kafka as an idempotent message store and demonstrates how to write robust tests for idempotent consumers in Camel.