Spring KafkaTemplate

Explain Spring KafkaTemplate.

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…

I am Full Stack Java Developer @ Tata Strive | Get blogs and tutorials related to the (React | Kafka | DevOps) | Follow me on LinkedIn https://www.linkedin.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Chrome Extension For Web Developers

Convert Input Image to PNG Format in Python

Docker Tutorial: Containers, VMs, and Docker for Beginners

CRM Data Migration: Hassle-Free Shift to Your Company Prosperity — Data2CRM.Migration

Why We’re Streaming Media? Part II: The Secrets of PPIO’s Data Delivery Technology

2D Translation in Computer Graphics

Very clever Udacity and Google, very clever.

Contributing my way for SunPy1.0

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sagar Kudu

Sagar Kudu

I am Full Stack Java Developer @ Tata Strive | Get blogs and tutorials related to the (React | Kafka | DevOps) | Follow me on LinkedIn https://www.linkedin.com

More from Medium

Free Tier: Install Spring Boot on an Oracle Linux Instance (Oracle Cloud)

How to use Apache Camel JMS and connect to ActiveMQ?

Kafka, Apache Avro and Confluent Platform with Spring Boot

Using TLS on your Spring-Boot SOAP Service