Default Serializer

Purpose

The Default Serializer addresses the need to convert various common Java object types into byte arrays suitable for Kafka message headers. Kafka headers require binary data, but Camel routes often work with rich Java objects. This serializer provides a standardized, out-of-the-box mechanism to translate typical header values—such as strings, numbers, booleans, and byte arrays—into the binary format Kafka expects. It ensures seamless header propagation from Camel messages into Kafka headers without requiring custom serialization logic for common data types.

Functionality

The Default Serializer implements a type-aware serialization strategy that handles several Java types explicitly:

If the value's type does not match any of these known types, the serializer attempts to convert the value to a byte array using Camel’s type conversion system, which can support a wide range of additional types through pluggable converters.

If no suitable conversion is found, the header is skipped, and a debug log entry notes the unsupported type.

This approach balances explicit handling for common types with extensibility via Camel’s type converters, ensuring flexibility and robustness.

**Key method snippet:**

@Override
public byte[] serialize(final String key, final Object value) {
    if (value instanceof String string) {
        return string.getBytes();
    } else if (value instanceof Long aLong) {
        ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
        buffer.putLong(aLong);
        return buffer.array();
    }
    // ... similar for Integer, Double, Boolean, byte[]
    
    if (camelContext != null) {
        byte[] converted = camelContext.getTypeConverter().tryConvertTo(byte[].class, value);
        if (converted != null) {
            return converted;
        }
    }
    LOG.debug("Cannot propagate header value of type[{}], skipping...", value != null ? value.getClass() : "null");
    return null;
}

Integration

The Default Serializer is a fundamental subcomponent within the Kafka Header Serialization parent topic. While the parent topic defines the overall contract and purpose of header serialization and deserialization, this serializer provides the default concrete implementation for the serialization direction.

It integrates tightly with Apache Camel’s type conversion infrastructure, leveraging CamelContext to enhance flexibility beyond hardcoded types. This makes it complementary to other serializers that might handle custom or complex types.

Within the broader Kafka component, the Default Serializer is invoked by Kafka producers when preparing Kafka headers before sending messages. It works alongside deserializers (for inbound headers) and potentially with custom serializers registered by users when special handling is required.

By standardizing header serialization, it facilitates consistent header propagation, enabling features like JMS interoperability and routing decisions based on header content.

Diagram

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 input type, applies the appropriate conversion, and either returns the serialized byte array or skips the header if unsupported.