5

As I have seen the Kafka template internally used Kafka producer. I just want to know what is the exact difference. Also, I found many send() methods available in the Kafka template as compared to Kafka producer.
Please help me with it. If anyone knows more.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Aadi
  • 1,131
  • 2
  • 16
  • 32

2 Answers2

9

The producer is the pattern, while the KafkaTemplate wraps a Producer instance and provides convenience methods for sending messages to Kafka topics. (source)

The Kafka Producer is defined in Apache Kafka. The KafkaTemplate is Spring's implementation of it (although it does not implement Producer directly) and so it provides more methods for you to use. So you can use KafkaTemplate to get started or implement your own solution through implementing the Producer yourself.

Milgo
  • 2,617
  • 4
  • 22
  • 37
  • 1
    Anything else as I have already gone through that source page. – Aadi Sep 09 '20 at 09:17
  • I added some details. I guess, KafkaTemplate is your way to go. – Milgo Sep 09 '20 at 09:26
  • Spring for Apache Kafka adds familiar Spring Messaging abstractions to Kafka (`KafkaTemplate`, `@KafkaListener` - similar abstractions are available for RabbitMQ and JMS. It is a higher level abstraction. https://spring.io/projects/spring-kafka – Gary Russell Sep 09 '20 at 12:46
1

Kafka Producer is the term for producer in PubSub Pattern. If you want to implement Apache Kafka using Spring, Spring has provided a wrapper around this producer which we call as Kafka Template. Kafka template is a class which spring provides to produce messages into the Kafka Topic. How Kafka Template works:: There are different layers in which Kafka Template does its tasks -

  1. SERLIALIZER Any record that we intend to send to the Kafka topic, needs to be serialised to bytes. ( 2 techniques involved - a) key.serializer b) value.serializer )
  2. Partitioner It determines in which partition the message is going to go.
  3. Record Accumulator It buffers all the records sent by Kafka template and these records only sent to the Kafka Topics when this buffer gets full.(However, linger.ms file is there where we can set the time limit)
AnjuR
  • 91
  • 3