Apache Kafka is a publish-subscribe messaging system. A messaging queue lets you send messages between processes, applications, and servers. In this article, we will see how to send string messages to Apache Kafka in a spring boot application.
In order to learn how to create a spring boot project, refer to this article.
The string is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created. And also, java allows explicit typecasting where one type of variable can be forcibly converted into other. For example, the same string message can further be converted into an int or a float if the string consists of only numbers and it needs to be used for some computation. The following steps can be followed in order to publish a string message to Apache Kafka:
- Go to spring initializr and create a starter project with following dependencies:
- Spring Web
- Spring for Apache Kafka
- Open the project in an IDE and sync the dependencies. Now create a new class Controller with the annotation @RestController. This class handles all the RESTful routes.
- In this class, create a GET API and initialize KafkaTemplate with parameter as string. The following is the implementation of the class:
// Java program to implement the// controller for the spring// application@RestController@RequestMapping("/kafka")publicclassController {@AutowiredKafkaTemplate<String, String>kafkaTemplate;staticfinalString TOPIC ="gfg";// Implementing a GET method@GetMapping("publish/{message}")publicString publish_message(@PathVariable("message") String message){kafkaTemplate.send(TOPIC, message);return"Message Published on Kafka !";}}chevron_rightfilter_none - Start zookeeper and Kafka server. Now we need to create a new topic with the name gfg. To do so, open a new command prompt window and change directory to the Kafka directory.
- Now create a new topic using the command given below:
For Mac and Linux: bin/kafka-topics.sh –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic topic_name
For Windows: .\bin\windows\kafka-topics.bat –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic topic_name
- Now to see the messages on the Kafka server in the real-time, use the command below:
For MAC and Linux: bin/kafka-console-consumer.sh –bootstrap-server localhost:9092 –topic topic_name –from-beginning
For Windows: .\bin\windows\kafka-console-consumer.bat –bootstrap-server localhost:9092 –topic topic_name –from-beginning
- Run the application and call the API as:
localhost:8080/kafka/publish/{your message}
Note: If a different port has been used, then replace the port with 8080.
Output:
Recommended Posts:
- Spring Boot | How to publish JSON messages on Apache Kafka
- Spring Boot | How to consume string messages using Apache Kafka
- Spring Boot | How to consume JSON messages using Apache Kafka
- Spring Boot | How to access database using Spring Data JPA
- Difference Between Apache Kafka and Apache Flume
- Introduction to Spring Boot
- Why Apache Kafka is so Fast?
- Apache Kafka | Introduction
- How to create a REST API using Java Spring Boot
- How to Install and Run Apache Kafka on Windows?
- How to encrypt passwords in a Spring Boot project using Jasypt
- How to create a basic application in Java Spring Boot
- Containerizing Java applications | Creating a Spring Boot App using Dockerfile
- Difference between Apache Tomcat server and Apache web server
- Custom Messages in ElectronJS
- Send unlimited Whatsapp messages using JavaScript
- Spring MVC with JSP View
- How to print debug messages in the Google Chrome JavaScript Console?
- Spring Dependency Injection with Example
- Introduction to Spring Framework
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



