Spring KafkaTemplate

Explain Spring KafkaTemplate.

Kafka template is a class that is part of the Spring to produce messages into the Kafka Topics.

Overview

→ Think KafkaTemplate as a JDBCTemplate for database interactions.

The KafkaTemplate wraps a producer and provides convenient methods to send data to Kafka topics. The following listing shows the relevant methods from KafkaTemplate:

— If you look below it has many different overloaded versions of KafkaTemplate with send method.

ListenableFuture<SendResult<K, V>> sendDefault(V data);

ListenableFuture<SendResult<K, V>> sendDefault(K key, V data);

ListenableFuture<SendResult<K, V>> sendDefault(Integer partition, K key, V data);

ListenableFuture<SendResult<K, V>> sendDefault(Integer partition, Long timestamp, K key, V data);

ListenableFuture<SendResult<K, V>> send(String topic, V data);

ListenableFuture<SendResult<K, V>> send(String topic, K key, V data);

ListenableFuture<SendResult<K, V>> send(String topic, Integer partition, K key, V data);

ListenableFuture<SendResult<K, V>> send(String topic, Integer partition, Long timestamp, K key, V data);

ListenableFuture<SendResult<K, V>> send(ProducerRecord<K, V> record);

ListenableFuture<SendResult<K, V>> send(Message<?> message);

Map<MetricName, ? extends Metric> metrics();

List<PartitionInfo> partitionsFor(String topic);

<T> T execute(ProducerCallback<K, V, T> callback);

// Flush the producer.

void flush();

interface ProducerCallback<K, V, T> {

T doInKafka(Producer<K, V> producer);

}

How to Configure KafkaTemplate?

In order to configure KafkaTemplate we require to provide mandatory values:

1. bootstrap-server — it represents broker server.

bootstrap-server: localhost:9092,localhost:9093,localhost:9093

2. key-serializer

key-serializer: org.apache.kafka.common.serialization.IntegerSerializer

Note: By default the key-serializer is String Serializer.

3. value-serializer

value-serializer: org.apache.kafka.common.serialization.StringSerializer

To configure KafkaTemplate we can use the Spring Boot AutoConfiguration technique.

→ In this example, we have used the application.yml file.

Next Kafka Tutorial to follow…

Software Engineer at HCL | Technical Content Writing | Follow me on LinkedIn https://www.linkedin.com/in/sagarkudu/