KafkaSendDynamicAware.java

Overview

The `KafkaSendDynamicAware` class is part of the Apache Camel Kafka component and implements the `SendDynamicAware` interface. It provides an optimization for sending messages dynamically to Kafka topics using the Camel **toD** ("dynamic to") DSL. Instead of creating a new Kafka endpoint URI for every dynamic topic, this class allows using a static Kafka endpoint and overrides the topic dynamically via the message header `KafkaConstants.OVERRIDE_TOPIC`.

This approach reduces endpoint creation overhead and improves performance when sending messages to multiple Kafka topics dynamically within the same route.


Class: KafkaSendDynamicAware

Package

`org.apache.camel.component.kafka`

Purpose

Implements dynamic sending awareness for Kafka endpoints in Apache Camel, enabling topic override via message headers rather than URI parameters.

Annotations

Inheritance


Properties

Property

Type

Description

`camelContext`

`CamelContext`

The Camel context instance in which this runs.

`scheme`

`String`

The URI scheme this class handles, i.e., `"kafka"`.


Methods

String getScheme()


void setScheme(String scheme)


CamelContext getCamelContext()


void setCamelContext(CamelContext camelContext)


boolean isLenientProperties()


DynamicAwareEntry prepare(Exchange exchange, String uri, String originalUri) throws Exception


String resolveStaticUri(Exchange exchange, DynamicAwareEntry entry) throws Exception


Processor createPreProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception


Processor createPostProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception


private String parseTopicName(String uri)


Important Implementation Details


Interaction with Other Components


Usage Example

from("direct:start")
    .toD("kafka:defaultTopic");

// Internally:
// For each message, KafkaSendDynamicAware sets the header KafkaConstants.OVERRIDE_TOPIC
// to the topic parsed from the endpoint URI or overridden by the user, allowing dynamic topic routing.

Mermaid Class Diagram

classDiagram
    class KafkaSendDynamicAware {
        -CamelContext camelContext
        -String scheme
        +String getScheme()
        +void setScheme(String scheme)
        +CamelContext getCamelContext()
        +void setCamelContext(CamelContext camelContext)
        +boolean isLenientProperties()
        +DynamicAwareEntry prepare(Exchange exchange, String uri, String originalUri) throws Exception
        +String resolveStaticUri(Exchange exchange, DynamicAwareEntry entry) throws Exception
        +Processor createPreProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception
        +Processor createPostProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception
        -String parseTopicName(String uri)
    }
    KafkaSendDynamicAware --|> ServiceSupport
    KafkaSendDynamicAware ..|> SendDynamicAware

Summary

`KafkaSendDynamicAware` is a specialized helper class used by Camel's Kafka component to enable efficient dynamic sending of messages to Kafka topics. It achieves this by reusing a static endpoint and dynamically setting the target topic via message headers, thus optimizing the usage of Kafka producers and reducing endpoint URI churn in Camel routes. This class carefully parses URIs, manages headers, and integrates seamlessly with the Camel routing engine’s dynamic send mechanism (`toD`).