Spring Kafka SendTo Example, easy in 15 minutes

Spring Kafka SendTo Example introduces you to the way Spring Kafka forwards message using a @SendTo annotation . Starting with version 2.0, if you also annotate a @KafkaListener with a @SendTo annotation. Let’s dig deeer.

Spring Kafka SendTo Example

Project Setup

  • Spring Kafka: 2.3.4.RELEASE
  • Spring Boot: 2.2.2.RELEASE
  • Apache Kafka: kafka_2.13-2.4.0
  • Maven: 3.5

Project Structure

Spring Kafka SendTo Example

Maven Dependencies

At least, we must have these dependencies: spring-boot-starter, spring-kafka in pom.xml file. Note, you can change spring-kafka version for your own needs.

Configure Kafka Application with application.yml

Spring Boot takes care of most of the configuration. However, some properties that need to be explicitly set in the application.yml file:

  1. The kafka.consumer.auto-offset-reset:earliest by default, it will start reading from the beginning of the topic and stream all of the existing.
  2. The kafka.consumer.group-id: firstyou should always configure group.id unless you are using the simple assignment API and you don’t need to store offsets in Kafka.

Sending Kafka Messages with Spring Boot

Spring Boot automatically configures and initializes a KafkaTemplate based on the properties configured in the application.yml property file. We are sending a number to the topic first.t

Forwarding Listener Results using @SendTo

Starting with version 2.0, if you also annotate a @KafkaListener with a @SendTo annotation and the method invocation returns a result, the result will be forwared to the topic specified by the @SendTo.

The @SendTo value can have several forms:

  • @SendTo("someTopic") routes to the literal topic
  • @SendTo("#{someExpression}") routes to the topic determined by evaluating the expression once during application context initialization.
  • @SendTo("!{someExpression}") routes to the topic determined by evaluating the expression at runtime. The #root object for the evaluation has 3 properties:
  • request – the inbound ConsumerRecord (or ConsumerRecords object for a batch listener))
  • source – the org.springframework.messaging.Message<?> converted from the request.
  • result – the method return result.

The result of the expression evaluation must be a String representing the topic name. You will see the @SendTo example Receiver below.

Receiving Kafka Messages with Spring Boot

By annotating a method with @KafkaListener annotation Spring Kafka will automatically create a message listener container. Annotation that marks a method to be the target of a Kafka message listener on the specified topics first.t .

The calculate method consumes a number of the first.t topic then calculate base power of exponent from that number then send to the topic second.t. Finally, the resultmethod consumes the result from the second.t topic. Let have a look at Receiver below.

Spring Boot Application

Now, we create a Spring Boot application to demonstrate the application like below.

Running the Application

Before we make a demo for the Spring Kafka SendTo Example, Kafka server must start on localhost with port 9092 which is the default configuration of Kafka. Then we run Spring boot application normally and see the output.

That’s all about Spring Kafka SendTo Example. You should download the complete source code and run on your local machine to get more deeply the way Kafka works.

Download complete source code

spring-kafka-sendto-example.zip (224 downloads)


Apache Kafka Official Website
Forwarding Listener Results using @SendTo
Spring Kafka Documentation
Spring Boot Tutorial for Beginners

Please share it if you found this useful
Hide Buttons