Open In App

Spring Boot | How to publish String messages on Apache Kafka

Last Updated : 21 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Go to spring initializr and create a starter project with following dependencies:
    • Spring Web
    • Spring for Apache Kafka
  2. 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.
  3. 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")
    public class Controller {
      
        @Autowired
        KafkaTemplate<String, String>
            kafkaTemplate;
      
        static final String TOPIC = "gfg";
      
        // Implementing a GET method
        @GetMapping("publish/{message}")
        public String publish_message(
            @PathVariable("message") String message)
        {
            kafkaTemplate.send(TOPIC, message);
            return "Message Published on Kafka !";
        }
    }

    
    

  4. 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.
  5. 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

  6. 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

  7. 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:

  • Calling the API:

  • Checking the message in real time:



Previous Article
Next Article

Similar Reads

Spring Boot | How to publish JSON messages on Apache Kafka
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 JSON 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 full-form of JSON is JavaS
4 min read
Spring Boot | How to consume string messages using Apache Kafka
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. Approach: Step 1: Go to spring initializr and create a s
3 min read
Spring Boot | How to consume JSON messages using Apache Kafka
Apache Kafka is a stream processing system that lets you send messages between processes, applications, and servers. In this article, we will see how to publish JSON messages on the console of a Spring boot application using Apache Kafka. In order to learn how to create a Spring Boot project, refer to this article. Working Steps Step 1: Go to Sprin
3 min read
Difference Between Apache Kafka and Apache Flume
Apache Kafka: It is an open-source stream-processing software platform written in Java and Scala. It is made by LinkedIn which is given to the Apache Software Foundation. Apache Kafka aims to provide a high throughput, unified, low-latency platform for handling the real-time data feeds. Kafka generally used TCP based protocol which optimized for ef
2 min read
Spring Boot - Start/Stop a Kafka Listener Dynamically
When a Spring Boot application starts, Kafka Listener's default behavior is to begin listening for a certain topic. However, there are situations when we don't want to start it right away once our application launches. To start or stop a Kafka Listener dynamically we need major three approaches i.e. start/stop when Kafka messages need to be process
4 min read
Spring Boot - Spring JDBC vs Spring Data JDBC
Spring JDBC Spring can perform JDBC operations by having connectivity with any one of jars of RDBMS like MySQL, Oracle, or SQL Server, etc., For example, if we are connecting with MySQL, then we need to connect "mysql-connector-java". Let us see how a pom.xml file of a maven project looks like. C/C++ Code &lt;?xml version=&quot;1.0&quot; encoding=
4 min read
Why Apache Kafka is so Fast?
Apache Kafka is a well known open-source stream processing platform which aims to provide a high-throughput, low-latency &amp; fault-tolerant platform which is capable of handling real-time data input. So what is it that makes Apache Kafka the go-to platform of choice when it comes to real-time data processing? Apart from all the other perks that K
4 min read
How to Install and Run Apache Kafka on Windows?
Apache Kafka is an open-source application used for real-time streams for data in huge amount. Apache Kafka is a publish-subscribe messaging system. A messaging system lets you send messages between processes, applications, and servers. Broadly Speaking, Apache Kafka is software where topics can be defined and further processed. Downloading and Ins
2 min read
Spring Boot | How to access database using Spring Data JPA
Spring Data JPA is a method to implement JPA repositories to add the data access layer in applications easily. CRUD stands for create, retrieve, update, delete which are the possible operations which can be performed in a database. In this article, we will see an example of how to access data from a database(MySQL for this article) in a spring boot
4 min read
Difference between Spring MVC and Spring Boot
1. Spring MVC : Spring is widely used for creating scalable applications. For web applications Spring provides Spring MVC framework which is a widely used module of spring which is used to create scalable web applications. Spring MVC framework enables the separation of modules namely Model View, Controller, and seamlessly handles the application in
3 min read