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

Install TensorFlow-gpu with Miniconda3 on Ubuntu 18.04

Send Slack Notifications with Jenkins

API GOVERNANCE: A VITAL BUILDING BLOCK FOR API SECURITY

DevOps: The Last 6 Months

K3D — A development Launchpad

Frontend-Design Knowledge Sharing #2

WordPress Takes Steps to Achieve Performance Lead

How I Became a Full Stack Developer in Less Than 2 Years.

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

Cross Repository Maven-based Test Execution using GitHub Action Workflow

Guide to Maven build cycle using Jenkins

Geospatial indexing app with different backends using Spring Boot and Testcontainers

How to Verify API Interfaces Using Automated Contract Test?