RegexRouter.java
Overview
The `RegexRouter` class is a specialized processor within the Apache Camel Kafka integration ecosystem designed to dynamically route Kafka messages by modifying topic names or CloudEvent type headers using regular expressions. It reads regex pattern and replacement strings from Camel exchange properties, applies these patterns to Kafka topic headers or CloudEvent `ce-type` headers, and updates the message headers accordingly to override the target Kafka topic or event type.
This enables flexible, runtime-driven topic routing based on pattern matching and substitution without changing route configurations or producer logic. The class is typically used in Camel routes where dynamic Kafka topic determination is required, such as environment-based topic prefixes, multi-tenant routing, or event-type normalization.
Class: RegexRouter
Package
org.apache.camel.component.kafka.transform
Purpose
Dynamically rewrite Kafka topic names using regex matching and replacement.
Optionally rewrite the CloudEvents
ce-typeheader similarly.Sets the overridden topic name in the
kafka.OVERRIDE_TOPICheader to influence Kafka producer routing.Supports configuration through exchange properties
regexandreplacement.
Public Methods
void process(String regex, String replacement, Exchange ex)
Processes the incoming `Exchange` message by applying a regular expression replacement on the Kafka topic header and CloudEvent type header.
Parameters
Name | Type | Description |
|---|---|---|
`regex` | String | The regular expression pattern to match against the topic or `ce-type` header. Provided as an exchange property `"regex"`. |
`replacement` | String | The replacement string used for substitution. Provided as an exchange property `"replacement"`. |
`ex` | Exchange | The Camel Exchange object containing message and headers to be processed. |
Behavior
Compile the regex pattern using
Pattern.compile(regex).Retrieve the current Kafka topic name from the header
kafka.TOPIC.If the topic name is not empty, attempt to match it against the regex.
If it matches, perform a replacement using
Matcher.replaceFirst(replacement).Set the transformed topic name in the header
kafka.OVERRIDE_TOPIC.
Retrieve the CloudEvent type header
"ce-type".If the
ce-typeheader is present, apply the same matching and replacement logic.Update the
ce-typeheader with the replaced value if matched.
Returns
void— This method modifies the exchange message headers in-place.
Usage Example
// Assume exchange properties are set:
exchange.setProperty("regex", "prod-(.*)");
exchange.setProperty("replacement", "dev-$1");
// The incoming message has kafka.TOPIC = "prod-orders"
// Instantiate and invoke the processor
RegexRouter router = new RegexRouter();
router.process(
exchange.getProperty("regex", String.class),
exchange.getProperty("replacement", String.class),
exchange
);
// After processing:
// exchange.getMessage().getHeader("kafka.OVERRIDE_TOPIC") == "dev-orders"
Important Implementation Details
Uses Java's
java.util.regex.PatternandMatcherfor regex operations.The method only applies the replacement if the entire string matches the regex (via
matcher.matches()).The replacement uses
replaceFirst, meaning only the first match is replaced.Operates on two headers:
kafka.TOPIC: The original Kafka topic header.ce-type: A CloudEvents header that can also be rewritten.
The overridden topic is set in
kafka.OVERRIDE_TOPIC, a Kafka-specific header recognized by the Kafka producer to route messages dynamically.Relies on Apache Camel's
ExchangeandExchangePropertyannotations for property injection.Uses
ObjectHelper.isNotEmptyfrom Camel utilities to guard against null or empty strings.
Interaction with Other Components
Camel Exchange: Operates directly on the Camel
Exchangeobject, reading message headers and writing override headers.Kafka Producer Component: Downstream Kafka producer uses the
kafka.OVERRIDE_TOPICheader to send the message to the dynamically determined topic instead of the route's configured topic.Camel Routes: Intended to be used as a processor in Camel routes where dynamic topic routing is needed.
Other Transformations: Can be combined with other transformation processors like
TimestampRouteror JSON field manipulators to build complex routing logic.Configuration: The regex and replacement strings can be dynamically configured via exchange properties, enabling flexible route definitions.
Summary
`RegexRouter` enables regex-based dynamic routing for Kafka messages by transforming topic names and event type headers at runtime. It facilitates sophisticated routing scenarios in Apache Camel Kafka integrations without hardcoding topic names, enhancing flexibility and maintainability.
Mermaid Class Diagram
classDiagram
class RegexRouter {
+void process(String regex, String replacement, Exchange ex)
}
class Exchange {
+Message getMessage()
+Object getProperty(String name)
}
class Message {
+<T> T getHeader(String name, Class<T> type)
+void setHeader(String name, Object value)
}
class Pattern {
<<static>>
+Pattern compile(String regex)
+Matcher matcher(CharSequence input)
}
class Matcher {
+boolean matches()
+String replaceFirst(String replacement)
}
class ObjectHelper {
<<static>>
+boolean isNotEmpty(Object obj)
}
RegexRouter --> Exchange : uses
Exchange --> Message : has
RegexRouter --> Pattern : compiles regex
Pattern --> Matcher : creates matcher
Matcher --> RegexRouter : used for matching & replacement
RegexRouter --> ObjectHelper : uses isNotEmpty() to check headers
Additional Notes
The class is lightweight and stateless; no fields or constructor parameters.
It depends on Camel's framework to inject exchange properties.
Ensures that only messages with matching topics or event types are altered, preventing unnecessary header overrides.
Can be easily extended or reused for similar header transformation needs.