KafkaProducerMetadataCallBack.java


Overview

`KafkaProducerMetadataCallBack.java` defines a simple but crucial helper class used within the Apache Camel Kafka producer module to handle Kafka producer callbacks. Its primary responsibility is to process the completion of Kafka message sends by capturing any exceptions and optionally storing the Kafka record metadata into the message body.

This callback implementation integrates tightly with Apache Camel's message exchange model, enabling the propagation of Kafka producer results (success or failure) back into Camel message bodies for subsequent processing or auditing.


Class: KafkaProducerMetadataCallBack

Purpose

This class implements Kafka's `Callback` interface and acts as a handler for asynchronous Kafka producer send completions. Upon completion, it:

This allows Camel routes to access Kafka metadata or errors related to message production without blocking or polling.


Package

package org.apache.camel.component.kafka.producer.support;

Imports


Declaration

public class KafkaProducerMetadataCallBack implements Callback

Implements Kafka's `Callback` interface.


Fields

Field

Type

Description

`body`

Object

The message body (usually Camel message body) to attach results.

`recordMetadata`

boolean

Flag indicating if Kafka record metadata should be set on body.


Constructor

public KafkaProducerMetadataCallBack(Object body, boolean recordMetadata)

Method: onCompletion

@Override
public void onCompletion(RecordMetadata recordMetadata, Exception e)

Implementation Details


Interaction with Other Components


Usage Context

In the Kafka asynchronous sending process, when the Kafka client finishes sending a message, it calls the registered `Callback.onCompletion()` method. This class is used when the producer configuration or logic requires Kafka record metadata to be captured and/or exceptions to be recorded on the message body, allowing downstream Camel processors to react accordingly.


Example Workflow

  1. Camel route sends message via KafkaProducer.

  2. KafkaProducer creates a KafkaProducerMetadataCallBack instance passing the message body and metadata recording flag.

  3. Kafka producer sends the message asynchronously with this callback.

  4. Kafka client invokes onCompletion() after sending completes.

  5. The callback sets exception or metadata on the message body.

  6. Camel route continues processing with updated message information.


Mermaid Class Diagram

classDiagram
    class KafkaProducerMetadataCallBack {
        -Object body
        -boolean recordMetadata
        +KafkaProducerMetadataCallBack(body: Object, recordMetadata: boolean)
        +onCompletion(recordMetadata: RecordMetadata, e: Exception) void
    }
    KafkaProducerMetadataCallBack ..> Callback : implements
    KafkaProducerMetadataCallBack ..> ProducerUtil : uses

Summary

`KafkaProducerMetadataCallBack` is a focused, utility callback class used in the Apache Camel Kafka component to:

It works as part of the asynchronous Kafka message sending infrastructure within Camel, enabling Kafka producer send results to be integrated into Camel's routing and error handling mechanisms smoothly, without introducing complex threading or routing logic.

This class is a small but essential bridge between Kafka's asynchronous callback model and Camel's message exchange processing model.