Spring Boot + Apache Kafka Tutorial - #11 - Create Kafka Producer to Produce JSON Message

Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will create Kafka Producer to produce JSON messages and send them to the Kafka topic.

Lecture  - #11 - Create Kafka Producer to Produce JSON Message


Hi, welcome back. In this lecture, will create a Kafka producer to produce JSON messages using the spring Kafka library. Let's head over to the IntelliJ idea, let's go and create a new Kafka producer to produce a JSON message. So we have a Kafka producer, but it will produce the string message, right? So we'll create a separate Kafka producer to produce a JSON message. So right-click on the Kafka package new and then choose Java class and let's see JSONKafkaProducer hit to enter and let's annotate this class with @Service annotation to make this class has a spring bean. All right, now let's configure the logger Okay, so private static final and then use a logger from slf4j library and then this should be a logger = logger factory and then call get logger API and then pass JSONKafkaProducer.class. Okay, perfect. Now let's go ahead and let's inject the Kafka template to send a JSON message to the Kafka topic. Well, here, let's see private and then Kafka template so Kafka template is from springframework.kafka.core. This is generic so we're going to pass key as a string type and value as a user. Well, we are going to pass the user object as a byte[] to the Kafka topic Right. That's why we are going to use user class as a value type here and then kafkaTemplate. So let's use, you know, constructor-based dependency injection in order to inject the KafkaTemplate. So let's generator constructor. And again, we don't have to use @Autowired annotation over here because Spring Framework will automatically inject this dependency whenever this bean will have only one parameterized constructor. So let's ignore @Autowired annotation over here. Okay, now let's create a method, and let's call it as sendMessage and in this case, we need to pass a user object as a parameter, let's say user, and then data All right. Well, within sendMessage method, we are going to create a message that we're going to write to the Kafka topic Right? So here let's say the message and make sure that you choose a Message class from org.springframework. messaging package. And then we need to pass the type to this message class that is user and this is a message and let's use a message builder so make sure that you choose a message builder from org.springframework.messaging. support package. And then let's use it's method that is with payload method and then pass user object as the argument and then call the set header. So basically we're going to set a topic name in a header, right? that's why let's call set header over here and then KafkaHeaders right, go ahead and then topic name. So let's call topic here and then by the second argument as a topic, the new topic name is javaguides isn't it. And next, let's call a build() method to build the message object. Now we have created a message, isn't it? Next, let's go ahead and send this message using the Kafka template. So let's say kafkaTemplate.send() method and here you can see we need to pass this message, right? So let's call this send() method and then pass message. Perfect. Now we have created a message object and we have sent using send() method of the Kafka template object. So we have created a logger, but we haven't logged this, you know, user object yet. Right? So let's go ahead and let's put the logger statement here in order to log the user object. So logger.info and then use string.format() and then let's say message sent and then the placeholder and then pass data to string method Okay. So we have created a toString() method in a user class, right. That is what we are calling here. Now we have created KafkaProducer to produce the JSON message and send that JSON message to the Kafka topic. Now let's run the Spring Boot application and here you can see the spring boot application is successfully running on tomcat server on port 8080. And also you can see myGroup is the Consumer group name and by default Kafka provides the partition name that is javaguides-01 All right. It means that the KafkaProducer that we have written for JSON serializer is working as expected Okay. In the next lecture, we'll create a simple REST endpoint to send a JSON message from the client. Alright, I will see you in the next lecture.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course