Spring Kafka offers a Spring Bean structure for Producing Kafka Messages. For someone familiar with using Kafka API, Spring Kafka can seem a bit different.
Here is a code snippet from Spring Kafka documentation showing the Classes involved in the Spring Kafka Producer.
We can see that there are 2 important classes involved. DefaultKafkaProducerFactory and KafkaTemplate. Here is a UML class diagram showing how they are related.
The KafkaTemplate bean needs an implementation ProducerFactory interface that has information to produce messages. There is only one implementation available - the DefaultKafkaProducerFactory class.
DefaultKafkaProducerFactory takes a Hashmap with properties like Kafka Server URLs, Serializer classes, etc.
Reference - https://docs.spring.io/spring-kafka/reference/htmlsingle/#_overview
Here is a code snippet from Spring Kafka documentation showing the Classes involved in the Spring Kafka Producer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
public ProducerFactory<Integer, String> producerFactory() { | |
return new DefaultKafkaProducerFactory<>(producerConfigs()); | |
} | |
@Bean | |
public Map<String, Object> producerConfigs() { | |
Map<String, Object> props = new HashMap<>(); | |
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); | |
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | |
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | |
// See https://kafka.apache.org/documentation/#producerconfigs for more properties | |
return props; | |
} | |
@Bean | |
public KafkaTemplate<Integer, String> kafkaTemplate() { | |
return new KafkaTemplate<Integer, String>(producerFactory()); | |
} |
The KafkaTemplate bean needs an implementation ProducerFactory interface that has information to produce messages. There is only one implementation available - the DefaultKafkaProducerFactory class.
DefaultKafkaProducerFactory takes a Hashmap with properties like Kafka Server URLs, Serializer classes, etc.
Reference - https://docs.spring.io/spring-kafka/reference/htmlsingle/#_overview