DefaultKafkaClientFactory.java

Overview

`DefaultKafkaClientFactory.java` is a core implementation class within the Apache Camel Kafka component. It provides a default mechanism for creating Apache Kafka client instances—specifically Kafka producers and consumers—based on given configuration properties. This factory class encapsulates the instantiation logic of Kafka clients, ensuring that clients are created consistently according to the configured properties.

The class also validates essential configuration parameters, such as the Kafka broker URLs, to guarantee that clients have the necessary connection information to interact with the Kafka cluster.

This factory implementation enables Apache Camel routes and components to easily obtain correctly configured Kafka clients without managing Kafka client lifecycle or configuration details directly.


Class: DefaultKafkaClientFactory

Implements the `KafkaClientFactory` interface to create Kafka `Producer` and `Consumer` instances.

Package

org.apache.camel.component.kafka

Dependencies


Methods

1. Producer getProducer(Properties kafkaProps)

Creates and returns a new Kafka `Producer` instance using the provided Kafka configuration properties.

Properties props = new Properties();
props.put("bootstrap.servers", "broker1:9092,broker2:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

DefaultKafkaClientFactory factory = new DefaultKafkaClientFactory();
Producer producer = factory.getProducer(props);

2. Consumer getConsumer(Properties kafkaProps)

Creates and returns a new Kafka `Consumer` instance using the provided Kafka configuration properties.

Properties props = new Properties();
props.put("bootstrap.servers", "broker1:9092,broker2:9092");
props.put("group.id", "my-group");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

DefaultKafkaClientFactory factory = new DefaultKafkaClientFactory();
Consumer consumer = factory.getConsumer(props);

3. String getBrokers(KafkaConfiguration configuration)

Retrieves the Kafka broker URLs from the given configuration and validates that the broker URLs are provided.

KafkaConfiguration config = new KafkaConfiguration();
config.setBrokers("broker1:9092,broker2:9092");

DefaultKafkaClientFactory factory = new DefaultKafkaClientFactory();
String brokers = factory.getBrokers(config); // returns "broker1:9092,broker2:9092"

Implementation Details


Interaction with Other Components


Class Diagram

classDiagram
    class DefaultKafkaClientFactory {
        +Producer getProducer(Properties kafkaProps)
        +Consumer getConsumer(Properties kafkaProps)
        +String getBrokers(KafkaConfiguration configuration)
    }

    DefaultKafkaClientFactory ..|> KafkaClientFactory

    KafkaClientFactory <|.. DefaultKafkaClientFactory

Summary

`DefaultKafkaClientFactory.java` is a straightforward, yet crucial class within the Apache Camel Kafka integration. It encapsulates Kafka client creation logic, ensuring producers and consumers are instantiated with valid configurations. By mandating the presence of broker URLs and leveraging native Kafka clients, it provides a clean and reliable way to integrate Kafka messaging into Camel routes and components.


If you require additional information on `KafkaConfiguration` or the `KafkaClientFactory` interface, those would be separate documentation targets to complete the understanding of this file’s context.