KafkaConsumerCustomSubscribeAdapterIT.java

Overview

`KafkaConsumerCustomSubscribeAdapterIT.java` is an integration test class within the Apache Camel Kafka component module. The purpose of this file is to verify the behavior of a **custom Kafka consumer subscription adapter** when used with Camel routes consuming messages from a Kafka topic.

The test ensures that messages produced to a Kafka topic are correctly consumed by a Camel route configured with a custom `SubscribeAdapter` implementation. This adapter modifies the subscription behavior of the Kafka consumer, and the test confirms that the custom subscription logic is invoked as expected.

This file extends a base Kafka test support class and leverages the Camel testing framework (including [MockEndpoint](/projects/289/68546)) to validate message consumption within an isolated integration test environment.


Detailed Explanation

Package and Imports


Class: KafkaConsumerCustomSubscribeAdapterIT

This is the main test class responsible for setting up Kafka producer and consumer, defining Camel routes, and asserting correct message flow and subscription behavior.

Constants

Fields

Inner Class: TestSubscribeAdapter

This class extends [DefaultSubscribeAdapter](/projects/289/68602) and overrides the `subscribe` method to set a flag indicating the subscription call occurred.


Lifecycle Methods


Camel Route Configuration


Test Method


Important Implementation Details


Interaction with Other System Components


Usage Example

This file itself is a test and not a reusable library class. However, it demonstrates:

  1. How to create a custom subscription adapter by extending DefaultSubscribeAdapter.

  2. How to register the custom adapter with Camel's registry.

  3. How to configure a Camel route to consume from Kafka with the adapter.

  4. How to produce messages for testing and assert their consumption.


Mermaid Class Diagram

classDiagram
    class KafkaConsumerCustomSubscribeAdapterIT {
        -String TOPIC = "test-subscribe-adapter"
        -KafkaProducer<String, String> producer
        -TestSubscribeAdapter testSubscribeAdapter
        +void before()
        +void after()
        +RouteBuilder createRouteBuilder()
        +void kafkaMessagesIsConsumedByCamel()
    }

    class TestSubscribeAdapter {
        -volatile boolean subscribeCalled
        +void subscribe(Consumer<?, ?> consumer, ConsumerRebalanceListener listener, TopicInfo topicInfo)
        +boolean isSubscribeCalled()
    }

    KafkaConsumerCustomSubscribeAdapterIT o-- TestSubscribeAdapter : uses

Summary

`KafkaConsumerCustomSubscribeAdapterIT.java` is a focused integration test validating that a custom Kafka consumer subscription adapter is invoked correctly in an Apache Camel Kafka consumer route. It produces messages to a Kafka topic, consumes them through Camel routes configured with the custom adapter, and asserts both message receipt and adapter invocation. This test ensures extensibility and correctness of the Kafka consumer subscription mechanism within Camel's Kafka component.