Apache Kafka is a publish-subscribe messaging queue used for real-time streams of data. A messaging queue lets you send messages between processes, applications, and servers. In this article we will see how to send string messages from apache kafka to the console of a spring boot application.
Step 1: Go to spring initializr and create a starter project with following dependency:
- Spring for Apache Kafka
Note: We can also create a maven project and add the following code to pom.xml file.
Step 2: Open the project in an IDE and sync the dependencies. Now create a new class Config and add annotations @Configuration and @EnableKafka.
Step 3: Now create beans ConsumerFactory and ConcurrentKafkaListenerContainerFactory with String object.
Step 4: Create a class KafkaService with @Service annotation. This class will contain the listener method to publish the message on the console.
Step 5: Start zookeeper and then kafka server using the command below.
For mac and linux:
Step 6: Now we need to create a new topic with the name StringProducer. To do so, open a new command prompt window and change directory to the kafka directory. 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
Step 7: Now to run kafka producer console, use the command below:
// For Mac and Linux
bin/kafka-console-producer.sh –broker-list localhost:9092 –topic Kafka_Example
// For Windows
.\bin\windows\kafka-console-producer.bat –broker-list localhost:9092 –topic Kafka_Example
Step 7: Run the application and and type message on kafka producer and press enter.
Here type a message in string formate on kafka producer
>Hello >Welcome to GeeksForGeeks
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Spring Boot | How to consume JSON messages using Apache Kafka
- Spring Boot | How to publish String messages on Apache Kafka
- Spring Boot | How to publish JSON messages on Apache Kafka
- Spring Boot | How to access database using Spring Data JPA
- Difference Between Apache Kafka and Apache Flume
- How to encrypt passwords in a Spring Boot project using Jasypt
- Containerizing Java applications | Creating a Spring Boot App using Dockerfile
- How to create a REST API using Java Spring Boot
- Introduction to Spring Boot
- How to create a basic application in Java Spring Boot
- Why Apache Kafka is so Fast?
- How to Install and Run Apache Kafka on Windows?
- Spring MVC using Java based configuration
- Android Boot Process
- 3 Different ways to print Exception messages in Java
- Messages, aggregation and abstract classes in OOPS
- Introduction to Spring Framework
- Aspect Oriented Programming and AOP in Spring Framework
- Spring Dependency Injection with Example
- Singleton and Prototype Bean Scopes in Java Spring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.