PropagatedHeadersProvider.java

Overview

`PropagatedHeadersProvider` is a utility class designed to facilitate the propagation of Kafka message headers within the Apache Camel Kafka component. It manages how headers are extracted and provided for Kafka messages when producing records, especially in the context of batch processing with or without individual headers.

The class primarily supports two modes governed by the Kafka configuration flag `batchWithIndividualHeaders`:

This class abstracts the header propagation logic, ensuring correct and efficient handling of Kafka headers according to the configured behavior.


Detailed Class Description

Class: PropagatedHeadersProvider

This class encapsulates the logic for producing Kafka headers for messages based on the parent exchange and message or per child message, depending on batch configuration.

Fields

Field Name

Type

Description

`kafkaProducer`

`KafkaProducer`

Reference to the Kafka producer instance used to extract propagated headers.

`parentExchange`

`Exchange`

The Camel Exchange representing the parent context for the batch or message group.

`parentMessage`

`Message`

The parent Camel Message from which default headers may be derived.

`propagatedHeaders`

`List

`

Cached list of Kafka headers propagated from the parent exchange/message when batch headers are not individualized (can be `null`).

Constructor

public PropagatedHeadersProvider(KafkaProducer kafkaProducer,
                                 KafkaConfiguration configuration,
                                 Exchange parentExchange,
                                 Message parentMessage)

Methods

List<Header> getDefaultHeaders()
List<Header> defaultHeaders = propagatedHeadersProvider.getDefaultHeaders();

List<Header> getHeaders(Exchange childExchange, Message childMessage)
List<Header> headers = propagatedHeadersProvider.getHeaders(childExchange, childMessage);

Important Implementation Details


Interaction with Other Components

This class is typically used internally by the Kafka component’s producer logic during message batch processing to ensure headers are correctly propagated according to user configuration.


Mermaid Class Diagram

classDiagram
    class PropagatedHeadersProvider {
        -KafkaProducer kafkaProducer
        -Exchange parentExchange
        -Message parentMessage
        -List<Header> propagatedHeaders
        +PropagatedHeadersProvider(KafkaProducer, KafkaConfiguration, Exchange, Message)
        +List<Header> getDefaultHeaders()
        +List<Header> getHeaders(Exchange, Message)
    }
    PropagatedHeadersProvider --> KafkaProducer : uses
    PropagatedHeadersProvider --> KafkaConfiguration : reads config
    PropagatedHeadersProvider --> Exchange : parentExchange, childExchange
    PropagatedHeadersProvider --> Message : parentMessage, childMessage
    PropagatedHeadersProvider --> Header : produces List<Header>

Summary

`PropagatedHeadersProvider` is a focused helper class within the Apache Camel Kafka producer component that streamlines the propagation of Kafka headers during batch message production. It supports configurable behavior for header reuse or individual header extraction per message, integrating closely with `KafkaProducer` and Camel core types. This abstraction simplifies header management for Kafka messaging in Camel routes, improving maintainability and configurability of Kafka header propagation.