DelegatingCallback.java


Overview

`DelegatingCallback.java` defines a utility class **DelegatingCallback** that implements Kafka's `Callback` interface and delegates callback invocations to two separate `Callback` instances. This class is primarily used in the Kafka producer component of Apache Camel to combine multiple callback behaviors into a single callback handler. It ensures that both callbacks are executed when Kafka producer operations complete, and it gracefully handles exceptions thrown by either callback to prevent disruption of the other.


Class: DelegatingCallback

public final class DelegatingCallback implements Callback

Purpose

The `DelegatingCallback` class acts as a composite callback handler for Kafka producer callbacks. When Kafka sends a message asynchronously, it can invoke a callback to signal completion or failure. This class allows two callbacks to be registered and invoked sequentially for a single Kafka send operation, facilitating modular handling of Kafka send completions such as metadata recording, error handling, or routing continuation.

Key Features


Constructor

DelegatingCallback(Callback callback1, Callback callback2)

Creates a new `DelegatingCallback` that delegates calls to two callbacks.

Callback firstCallback = new KafkaProducerMetadataCallBack(...);
Callback secondCallback = new KafkaProducerCallBack(...);

Callback delegatingCallback = new DelegatingCallback(firstCallback, secondCallback);

// Pass this delegatingCallback to kafkaProducer.send(record, delegatingCallback);

Method

void onCompletion(RecordMetadata metadata, Exception exception)

Invoked by Kafka producer when a send operation completes, either successfully or with an exception.


Implementation Details


Interaction with Kafka Producer Component


Example Usage in Kafka Producer Asynchronous Send

// Create individual callbacks
Callback metadataCallback = new KafkaProducerMetadataCallBack(exchange, recordMetadata);
Callback producerCallback = new KafkaProducerCallBack(exchange, asyncCallback, workerPool, recordMetadata);

// Combine callbacks into one
Callback delegatingCallback = new DelegatingCallback(metadataCallback, producerCallback);

// Send Kafka record asynchronously with combined callback
kafkaProducer.send(producerRecord, delegatingCallback);

Mermaid Class Diagram

classDiagram
    class DelegatingCallback {
        -Callback callback1
        -Callback callback2
        +DelegatingCallback(Callback callback1, Callback callback2)
        +void onCompletion(RecordMetadata metadata, Exception exception)
    }
    DelegatingCallback ..|> Callback

Summary

The **DelegatingCallback** class is a small but crucial utility in Apache Camel's Kafka producer support that allows two Kafka producer callbacks to be combined and invoked as one. It ensures robust and modular callback handling by isolating exceptions in each delegate and logging warnings without interrupting the Kafka send lifecycle. This class enables clean composition of callback functionality such as metadata recording and routing continuation, fitting neatly into the asynchronous Kafka message production workflow.


If you are working with the Apache Camel Kafka component's producer code, understanding and using `DelegatingCallback` helps you extend or customize Kafka producer callbacks safely without disrupting the existing callback chain or error handling logic.