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


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

  1. Compile the regex pattern using Pattern.compile(regex).

  2. Retrieve the current Kafka topic name from the header kafka.TOPIC.

  3. 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.

  4. Retrieve the CloudEvent type header "ce-type".

  5. If the ce-type header is present, apply the same matching and replacement logic.

    • Update the ce-type header with the replaced value if matched.

Returns

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


Interaction with Other Components


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


References