KafkaConsumerTopicIsPatternIT.java


Overview

`KafkaConsumerTopicIsPatternIT.java` is an integration test class within the Apache Camel Kafka component module. Its primary purpose is to verify the functionality of consuming Kafka messages from topics matched by a regular expression pattern, rather than specifying a single topic name explicitly.

The test validates that the Kafka consumer properly subscribes to topics whose names match the given pattern and receives messages accordingly. It also ensures that auto-commit behavior and consumer interceptors work as expected in this scenario.

This file extends a base test class (`BaseKafkaTestSupport`) that provides shared setup and utilities for Kafka integration testing.


Class Summary

KafkaConsumerTopicIsPatternIT

This class contains:


Detailed Explanation

Constants

Constant

Type

Description

`TOPIC`

String

Name of the Kafka topic used in the test.

`TOPIC_PATTERN`

String

Regex pattern to match topics for consumption.

Fields

Field

Type

Description

`producer`

`KafkaProducer`

Kafka producer instance to send messages to Kafka.

Lifecycle Methods

before()

@BeforeEach
public void before()

after()

@AfterEach
public void after()

Route Definition

createRouteBuilder()

@Override
protected RouteBuilder createRouteBuilder()
from("kafka:" + TOPIC_PATTERN + "?topicIsPattern=true&groupId=KafkaConsumerTopicIsPatternIT&autoOffsetReset=earliest" +
    "&autoCommitIntervalMs=1000&pollTimeoutMs=1000&autoCommitEnable=true" +
    "&interceptorClasses=org.apache.camel.component.kafka.MockConsumerInterceptor&metadataMaxAgeMs=1000")
    .to(KafkaTestUtil.MOCK_RESULT);

Test Methods

kafkaTopicIsPattern()

@Test
public void kafkaTopicIsPattern() throws Exception
// This is executed automatically by JUnit during test runs
kafkaTopicIsPattern();

Important Implementation Details


Interaction with Other System Components


Usage Scenario

This integration test is run as part of the continuous integration pipeline to ensure that the Kafka consumer component in Apache Camel correctly supports topic pattern subscriptions. It helps maintain the reliability of consuming from dynamically named topics in Kafka without specifying each topic explicitly.


Visual Diagram

classDiagram
    class KafkaConsumerTopicIsPatternIT {
        +String TOPIC
        +String TOPIC_PATTERN
        -KafkaProducer<String, String> producer
        +void before()
        +void after()
        +RouteBuilder createRouteBuilder()
        +void kafkaTopicIsPattern()
    }

    KafkaConsumerTopicIsPatternIT --|> BaseKafkaTestSupport : extends
    KafkaConsumerTopicIsPatternIT ..> KafkaProducer : uses
    KafkaConsumerTopicIsPatternIT ..> RouteBuilder : creates
    KafkaConsumerTopicIsPatternIT ..> MockEndpoint : verifies
    KafkaConsumerTopicIsPatternIT ..> MockConsumerInterceptor : verifies captured records

Summary

`KafkaConsumerTopicIsPatternIT.java` is a focused integration test class verifying Apache Camel Kafka component’s ability to consume messages from topics selected by regex pattern subscription. It tests key Kafka consumer configurations, interceptor usage, and message receipt correctness, ensuring robustness of the dynamic topic consumption feature within the Camel Kafka component.

The test is a vital part of the Kafka component's test suite, helping prevent regressions and guaranteeing reliable Kafka integration in real-world use cases involving pattern-based topic consumption.