TopicInfo.java
Overview
`TopicInfo.java` is a utility class within the Apache Camel Kafka component that encapsulates information about Kafka topics used for consumer subscriptions. It manages both static topic names and dynamic topic subscriptions based on regular expression patterns. The class provides a simple abstraction to represent either a fixed list of topic names or a pattern that matches multiple topics dynamically.
This file is essential in managing Kafka consumer subscription semantics, particularly when subscribing to topics by name or by pattern. It is part of the subscription support infrastructure in the Kafka consumer integration of Apache Camel.
Class: TopicInfo
Description
`TopicInfo` acts as a carrier for topic subscription information. It can represent:
A list of explicit topic names (e.g.,
"topic1,topic2")A regular expression pattern to subscribe to topics matching the pattern
This dual representation supports Kafka's native consumer subscription modes: either subscribing to specific topics or subscribing via a regex pattern.
Package
org.apache.camel.component.kafka.consumer.support.subcription
Fields
Field Name | Type | Description |
|---|---|---|
`pattern` | `Pattern` | A regex pattern for dynamic subscription. Null if using explicit topic names. |
`topicName` | `List` | A list of topic names parsed from a comma-separated string. |
Constructors
TopicInfo(Pattern pattern, String topicName)
Creates a new `TopicInfo` instance.
Parameters:
pattern(java.util.regex.Pattern): The regex pattern for topic subscription. Passnullif subscribing by explicit topics.topicName(String): Comma-separated topic names (e.g.,"topicA,topicB"). Ignored ifpatternis non-null.
Behavior:
If
patternis non-null, the instance represents a pattern subscription.The
topicNamestring is split by commas and stored as a list of topic names internally.
Example usage:
// Subscription by explicit topic names TopicInfo topicInfoByNames = new TopicInfo(null, "topic1,topic2,topic3"); // Subscription by pattern Pattern topicPattern = Pattern.compile("topic.*"); TopicInfo topicInfoByPattern = new TopicInfo(topicPattern, "");
Methods
boolean isPattern()
Description:
Returns whether thisTopicInfoinstance represents a pattern-based subscription.Returns:
trueifpatternis not null (i.e., subscription is by regex pattern), otherwisefalse.Example:
if (topicInfo.isPattern()) { System.out.println("Subscribed with pattern: " + topicInfo.getPattern()); } else { System.out.println("Subscribed with explicit topics: " + topicInfo.getTopics()); }
Pattern getPattern()
Description:
Retrieves the regexPatternused for topic subscription.Returns:
ThePatterninstance if subscription is pattern-based; otherwisenull.
Collection<String> getTopics()
Description:
Retrieves the list of explicit topic names.Returns:
An unmodifiable collection of topic names parsed from the comma-separated string.Note:
If the subscription is pattern-based, this list may be empty or irrelevant.
Implementation Details
The class is declared
finalto prevent subclassing, ensuring immutability of topic subscription info.The constructor splits the
topicNamestring by commas into aList<String>, which simplifies handling multiple topics.The
patternfield is nullable, making it straightforward to distinguish between pattern vs explicit topic subscriptions.The class does not expose setters or mutators, maintaining thread safety and immutability.
No external dependencies except standard Java library (
java.util.regex.Pattern,java.util.List, etc.)
Interaction with Other System Components
This class is part of the
org.apache.camel.component.kafka.consumer.support.subcriptionpackage, which supports Kafka consumer subscription functionalities.It is likely used by Kafka consumer subscription managers or handlers within Apache Camel Kafka component to determine the subscription mode.
The encapsulated topic information is used when configuring Kafka consumers, either passing a list of topics or a regex pattern to Kafka consumer APIs.
Helps abstract away subscription logic from other parts of the Kafka consumer implementation, improving modularity.
Usage Summary
Use
TopicInfoto represent and manage subscription info cleanly.Check
isPattern()to determine the subscription type.Use
getTopics()orgetPattern()accordingly to retrieve subscription criteria.Pass this object downstream when creating or reconfiguring Kafka consumers.
Class Diagram
classDiagram
class TopicInfo {
-Pattern pattern
-List<String> topicName
+TopicInfo(Pattern pattern, String topicName)
+boolean isPattern()
+Pattern getPattern()
+Collection<String> getTopics()
}
Summary
`TopicInfo.java` provides a minimal but essential abstraction in the Apache Camel Kafka component to represent either a static list of Kafka topics or a dynamic subscription via regex patterns. It supports Kafka's flexible subscription model and promotes clean separation of concerns in the consumer subscription handling code. The class's immutability and simple API make it a reliable building block for managing topic subscriptions within the Kafka consumer integration layer.