Spring Boot + Apache Kafka Tutorial - #7 - Create Kafka Producer - Line-by-Line Coding

Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will create a Kafka Producer in our spring boot project.

Lecture - #7 - Create Kafka Producer - Line-by-Line Coding


Hi. Welcome back. In this lecture, we'll create a Kafka Producer. Well, in the previuos lecture, we have created a topic in the Kafka cluster. Right? In this lecture, we want to create a Kafka Producer so that we can able to write a message to the Kafka topic. All right, let's go to the IntelliJ Idea, and let's create a Kafka producer to write a message to the topic. Well, I'm going to create a new package for that. So let's right-click on the main package -> new and then choose a package and let's go give the package name as Kafka. Okay, within the Kafka package, we are going to create a class and let your class name as KafkaProducer all right hit enter and let's annotate this Kafka Producer service with @Service annotation. Okay, great. Well, in order to send a message to the topic, we're going to use spring provided Kafka template. Well, let's go, and let's inject the Kafka template into this Spring bean. Well, basically spring boot provide a autoconfiguration for Kafka template. We just have to inject and use it. For example, let's say private KafkaTemplate, so this should be private and then KafkaTemplate and make sure that you choose KafkaTemplate from Spring Framework Library. that is org.springframework.kafka.core and this is the generic so we have pass the key, value here. So the type of the key string and the type of the, you know, value is a string. Okay, now let's provide a variable name that is kafkaTemplate. So we are going to basically use constructor based injection so in order to do that, let's creator constructor Okay. Now we have used constructor dependency to inject this KafkaTemplate. Okay. So if you don't use Spring boot then we have to explicitly, you know, configure Kafka template means you need to write a code to configure Kafka template if you don't use spring boot, okay. If you use spring boot then you need to configure the Kafka template because Spring Boot will provide a auto-configuration for this Kafka template so that we can simply inject and use it right great. Now let's go ahead and let's create a method which will use Kafka template to send the message. So here I'm going to create the method, let's say public void and the method name sendMessage() and let's pass the message of type string as a method argument. Alright and here, let's use Kafka template.send() So look at your Kafka template basically provides a lot of overloaded send() methods right. We have to use appropriate send() method. So in our case we're going to use the first one. It has, you know, type string topic, string data so go ahead and choose this one and then pass the topic as a first parameter. In our case, we have created javaguides as a topic name, right? and then second parameter is a message. All right. So if you can go to your KafkaTopicConfig class, you can see we have provided a topic name as javaguides. Now let's go to the Kafka Producer class again and let's put a log statement in order to print this message. Well, let's use Spring Boot provided default logger to log the message. So here let's say private static final and then logger. So let's use logger from self4j for the library and then let's use the name as logger equal to logger factory. So make sure that you choose logger factory from org.self4j and then getLogger and then the name of the class that is KafkaProducer and then class. Okay, now we have created a logger instance, so let's use this logger instance to log this message. So here, just use info method. So within info() method, let's use the string dot format method and then pass a message as something like message sent and then let's use a placeholder and then message. Okay, so this will basically log the message sent followed by the message. Okay, now we have created a Kafka producer, which will use the Kafka template to send a message to the topic. The topic name is Javaguides All right, great. So let me run this application and let's see any errors or exceptions. So here you can see in the console, Spring Boot application is running you know default Tomcat server on port 8080. Well if we can just scroll up and you can see these are the all your default properties and values provided by Kafka Okay, we're not going to change anything, so we're going to rely on these default you know, properties provided by Kafka admin client. All right. So in this lecture, basically we have created a Capcom. which used the Kafka template to send a message to the Kafka topic. Okay, great. I will see you in the next lecture.