ProducerUtil.java

Overview

`ProducerUtil` is a utility class within the Apache Camel Kafka component, specifically under the package `org.apache.camel.component.kafka.producer.support`. It provides essential helper methods that assist Kafka producers in:

This class is designed as a final utility class with static methods only, preventing instantiation. It encapsulates common, reusable logic related to Kafka message production, serialization, and metadata management to keep the core producer code clean and focused.


Class: ProducerUtil

Description

A utility helper class that provides static methods for:

This class is package-private and intended for internal use by Kafka producer components.


Constructor

private ProducerUtil()

Methods


tryConvertToSerializedType

public static Object tryConvertToSerializedType(Exchange exchange, Object object, String valueSerializer)
Exchange exchange = ...;
Object originalValue = somePojo;
String serializerClass = "org.apache.kafka.common.serialization.ByteArraySerializer";

Object serializedValue = ProducerUtil.tryConvertToSerializedType(exchange, originalValue, serializerClass);
// serializedValue will be a byte[] if conversion was successful

setException

static void setException(Object object, Exception e)
try {
    // Kafka sending logic
} catch (Exception e) {
    ProducerUtil.setException(exchange, e);
}

setRecordMetadata (single metadata)

static void setRecordMetadata(Object body, RecordMetadata recordMetadata)

setRecordMetadata (list of metadata)

public static void setRecordMetadata(Object body, List<RecordMetadata> recordMetadataList)
List<RecordMetadata> metadataList = ...;
ProducerUtil.setRecordMetadata(exchange, metadataList);

Important Implementation Details


Interaction with Other Components


Visual Diagram: Function Flow and Relationships

flowchart TD
    A[Input Object] --> B{Is Exchange null?}
    B -- Yes --> C[Return original object]
    B -- No --> D[Check valueSerializer]
    D -->|Default Serializer| E[Convert to String]
    D -->|ByteArraySerializer| F[Convert to byte[]]
    D -->|ByteBufferSerializer| G[Convert to ByteBuffer]
    D -->|BytesSerializer| H[Convert to byte[] then wrap as Bytes]
    E --> I[Return converted object or original]
    F --> I
    G --> I
    H --> I

    subgraph Exception Handling
        J[Input object (Exchange or Message)]
        K[Exception e]
        J --> L{Is Exchange?}
        L -- Yes --> M[Set exception on Exchange]
        L -- No --> N{Is Message with Exchange?}
        N -- Yes --> O[Set exception on Message's Exchange]
        N -- No --> P[Do nothing]
    end

    subgraph Metadata Setting
        Q[Input object (Exchange or Message)]
        R[RecordMetadata or List<RecordMetadata>]
        Q --> S{Exchange?}
        S -- Yes --> T[Set header on Exchange's Message]
        S -- No --> U{Message?}
        U -- Yes --> V[Set header on Message]
        U -- No --> W[No action]
    end

Summary

`ProducerUtil.java` is a focused utility class that supports Kafka message production in Apache Camel by:

It plays a critical role in enabling seamless integration between Camel's routing and Kafka's producer APIs by bridging type conversions and metadata handling, ensuring reliable and transparent Kafka message production.


Usage Snippets

Convert message body to Kafka serialized type

Object serializedValue = ProducerUtil.tryConvertToSerializedType(exchange, message.getBody(), valueSerializerClassName);

Set exception on exchange or message

try {
    // Kafka send logic that may throw exception
} catch (Exception e) {
    ProducerUtil.setException(exchange, e);
}

Attach Kafka record metadata after sending

RecordMetadata metadata = kafkaSendResult.getRecordMetadata();
ProducerUtil.setRecordMetadata(exchange, metadata);

This documentation provides a detailed understanding of the purpose, functions, and usage of `ProducerUtil.java` within the Apache Camel Kafka component.