DefaultKafkaHeaderSerializer.java


Overview

`DefaultKafkaHeaderSerializer.java` is a core implementation class within the Apache Camel Kafka component responsible for serializing Camel message header values into Kafka-compatible byte arrays. Kafka message headers require values as binary data (`byte[]`), but Camel message headers can contain Java objects of various types. This class provides a default strategy to convert common Java types into their byte representations for seamless propagation of headers through Kafka messages.

The serializer supports typical Java types such as `String`, `Long`, `Integer`, `Double`, `Boolean`, and raw `byte[]`. For other types, it leverages Apache Camel's flexible type conversion system to attempt a conversion to `byte[]`. If no conversion is possible, the header is skipped, and a debug message is logged.

By implementing the `KafkaHeaderSerializer` interface and being `CamelContextAware`, it integrates tightly with Camel's type converters, enabling extensible and robust header serialization within Camel-Kafka integrations.


Class: DefaultKafkaHeaderSerializer

public class DefaultKafkaHeaderSerializer implements KafkaHeaderSerializer, CamelContextAware

Description


Fields

Field

Type

Description

`LOG`

`Logger`

Logger instance for debug and error logs.

`camelContext`

`CamelContext`

Reference to the Camel context for type conversion.


Methods

byte[] serialize(String key, Object value)

DefaultKafkaHeaderSerializer serializer = new DefaultKafkaHeaderSerializer();
serializer.setCamelContext(camelContext); // Inject CamelContext before use

byte[] serializedString = serializer.serialize("myKey", "myValue");
// serializedString now contains UTF-8 bytes of "myValue"

byte[] serializedLong = serializer.serialize("count", 123L);
// serializedLong is 8 bytes representing the long value 123

CamelContext getCamelContext()

void setCamelContext(CamelContext camelContext)


Important Implementation Details


Interaction with Other Components


Class Diagram

classDiagram
    class DefaultKafkaHeaderSerializer {
        -static Logger LOG
        -CamelContext camelContext
        +byte[] serialize(String key, Object value)
        +CamelContext getCamelContext()
        +void setCamelContext(CamelContext camelContext)
    }
    DefaultKafkaHeaderSerializer ..|> KafkaHeaderSerializer
    DefaultKafkaHeaderSerializer ..|> CamelContextAware

Summary

`DefaultKafkaHeaderSerializer` provides a robust, extensible, and out-of-the-box mechanism for converting common Java types found in Camel message headers into Kafka-compatible byte arrays. Its integration with Camel's type conversion system and explicit handling of frequent types makes it a reliable default choice for header serialization in Camel-Kafka integrations.

By ensuring only supported headers are serialized and others are gracefully skipped, it contributes to stable and predictable header propagation, which is critical for metadata integrity across distributed messaging systems.


Additional Visual: Serialization Flow

flowchart TD
    A[Start Serialization] --> B{Value Type?}
    B -->|String| C[Convert to UTF-8 bytes]
    B -->|Long| D[Allocate 8 bytes, write long]
    B -->|Integer| E[Allocate 4 bytes, write int]
    B -->|Double| F[Allocate 8 bytes, write double]
    B -->|Boolean| G[Convert to string, then UTF-8 bytes]
    B -->|byte[]| H[Use as-is]
    B -->|Other| I[Try Camel TypeConverter]
    I -->|Success| J[Use converted byte[]]
    I -->|Fail| K[Skip header, log debug]
    C & D & E & F & G & H & J --> L[Return byte[]]
    K --> L

This flowchart illustrates how the serializer determines the type of the value and applies the appropriate serialization strategy, falling back on Camel's type converters, or skipping unsupported types.


End of Documentation for DefaultKafkaHeaderSerializer.java