Apache Kafka and Spring Boot Producer and Consumer Example
By: Mamed Yahyayev
Tags: Apache Kafka, Docker Compose, Spring, Docker

Second Step completed… ✔

In the third step , we create config classes for Producer and Consumer.

Produce Message

Open src/main/java/ <package_name> , and create new Java Class and give it a name , I give ProducerConfiguration name to this class.

In this class we create configuration for Producer to publish message. You can add following code snippets to this class.

@Bean

public ProducerFactory<String, String> producerFactory() {

Map<String, Object> configProps = new HashMap<>();

configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");

configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

return new DefaultKafkaProducerFactory<>(configProps);

} @Bean

public KafkaTemplate<String, String> kafkaTemplate() {

return new KafkaTemplate<>(producerFactory());

}

and you have to add @Configuration annotation to this class. That’s it.

To publish a message we have to define new Class which use KafkaTemplate. I give the name Publisher. and i will use this class for publish the message which send from Rest Api.

@Autowired

private KafkaTemplate<String, String> kafkaTemplate;



public void produceMessage(String message) {

kafkaTemplate.send("kafka-topic", message);

}

kafkaTemplate.send() methods expect 2 arguments in this case. First arguments is topic name. This topic name should have the same name as the topic name in the docker-compose file we are using. Second arguments is the our message sending from Rest Api.

Consume Message

After that you create a new class for implement Consumer configuration, I give the name ConsumerConfiguration to this class if you want to use other name of course you can.

You have to add @Configuration annotation to this class same in the ProducerConfiguration and additional you have to add @EnableKafka annotation . Add following code snippets.

@Bean

public ConsumerFactory<String, String> consumerFactory() {

Map<String, Object> props = new HashMap<>();

props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:2181");

props.put(ConsumerConfig.GROUP_ID_CONFIG, "group-id");

props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

return new DefaultKafkaConsumerFactory<>(props);

}



@Bean

public ConcurrentKafkaListenerContainerFactory<String, String> containerFactory() {

ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();

factory.setConsumerFactory(consumerFactory());

return factory;

}

To consume message You create a new Java class. This class will listen the message. Give it the name what you like , I give the name Consumer.

@KafkaListener(topics = "kafka-topic", groupId = "group-id")

public void listenMessage(String message) {

System.out.println("Message: " + message);

}

This method listening the topic which name is kafka-topic with group id. In our example group id is “group-id”. This method expect the message from producer and then receive the message and write the message in console.

Now we have to create a new Java class which is controller class. This class receive the message from Rest Api and send the message to Producer. Producer receive the message to send to Consumer.

Create the controller class and give the name. I give the name MessageController. You have to add @RestController annotation top of the class. After that we add Publisher class’s instance to MessageController class.Because we’ll use its method. For doing this write following code.

@Autowired

private Publisher publisher;

After that we create a new Http Method. And this method will receive the message. We have to create Post method because we’ll send the message. that’s why we use Post method.

@PostMapping("/message")

public ResponseEntity<String> sendMessage(@RequestBody String message) {

publisher.produceMessage(message);

return ResponseEntity.ok("Message send.");

}

in this method we use Publisher class’s method which is produceMessage() method and this method expect message argument. We complete the implementation , let’s see the message in the console.

Open the Postman App and navigate the url http://localhost:8080/message , switch the Http method to Post. Open the Body Section , click the raw in Body section and type message what you want to see on the console. And after that click the send button and you’ll get 200 Ok Response.