Wednesday, April 10, 2019

Kafka and Spring

Kafka has become a very popular platform and is being used as a stream, journal and even eventing system. In this post, we explore how to integrate Kafka with spring framework application. First, we add the Kafka bootstrap server details in the application.properties file.

Let's also add dependencies in pom.xml.

Now, for each Kafka topic, we create a listener class. The listener class provides a callback method that is called when any message is retrieved on that topic.

Now we create an endpoint through which we inject a message in the queue. The message is sent to the queue and is retrieved by the listener.

We autowire a KafkaTemplate instance that is used to send the message to the queue.

$ curl -X POST \
>   'http://localhost:8081/send?token=3193fa24-a0ba-451b-83ff-eb563c3fd43b-cdf12811-7e41-474b-8fa6-e8fefd4a738c' \
>   -H 'Content-Type: application/json' \
>   -H 'Postman-Token: e281e3c5-0dae-4bb7-ac8d-6555f66a18c6' \
>   -H 'cache-control: no-cache' \
>   -H 'token: 3193fa24-a0ba-451b-83ff-eb563c3fd43b-cdf12811-7e41-474b-8fa6-e8fefd4a738c' \
>   -d '{
> "message" : "This is my message!"
> }'
Message sent successfully!.
The receipt of message is indicated in the spring server log.

2019-04-10 14:28:03.969  INFO 31091 --- [ntainer#0-0-C-1] i.s.b.t.listeners.MyTopicKafkaListener   : Received Promise message This is my message!

3 comments:

  1. People ought to by no means make compromises or lose small bets outcome of|as a outcome 1xbet of} they will obtain large things occasion that they} work collectively. As a outcome, people ought to begin small and progressively enhance their stakes. It’s a mistake to strive for larger prizes whereas ignoring smaller ones. In Punto Banco the participant can bet on their very own hand , the banker’s hand , or a tie.

    ReplyDelete
  2. The article provides a practical introduction to integrating Apache Kafka with Spring applications, covering the essential configuration steps, Maven dependencies, topic listeners, and an endpoint for publishing messages. The listener-based approach clearly demonstrates how a Spring application can react to messages retrieved from a Kafka topic rather than relying on direct synchronous communication between components.

    This event-driven architecture is particularly relevant to applications that need to connect backend services with responsive web interfaces. A Spring Boot Angular Full Stack Developer Course provides a useful context for understanding how Spring Boot services can handle asynchronous Kafka events while Angular applications consume and present the resulting data.

    ReplyDelete
  3. The example also shows how Kafka can become part of a broader Java application architecture, with REST endpoints publishing messages and Kafka listeners processing them. A Java Full Stack Developer Course is relevant for building the wider foundation needed to connect Java backend services, messaging systems, APIs, and frontend applications into a complete solution.

    ReplyDelete