Spring Boot + Apache Kafka


You must install and run Apache Kafka. For help with Apache Kafka installation and to run please check the post :


Apache Kafka is an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications.

In this post lets see how to create Kafka producers, consumers and to built an API to send and receive events. Producers are those client applications that publish (write) events to Kafka, and consumers are those that subscribe to (read and process) these events.

To use Spring for Apache Kafka add the below dependency


As the next step configure kafka configs in the application properties file.



Defined MessagesController to create API to send and receive events

public class MessagesController {
	MessageService svc;

	public ProducerMetadata sendMessage(@RequestBody Order order) {
		try {
			return svc.sendMessage(order);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return null;
	public Order receiveOrder(@PathVariable String partition,@PathVariable String offset) {
		return svc.retrieveMessage(partition,offset);

Used Spring KafkaTemplate to send and receive events. The KafkaTemplate wraps a producer and provides convenience methods to send data to Kafka topics. From the response we can get the partition and offset to which events was sent.

	public ProducerMetadata sendMessage(Order order) throws Exception {
		// TODO Auto-generated method stub
		ListenableFuture<SendResult<String, Order>> result = kafkaTemplate.send(topicName,order);
		ProducerMetadata data = new ProducerMetadata();
		RecordMetadata metadata = null;
		try {
			SendResult<String, Order> sendResult = result.get();
			metadata = sendResult.getRecordMetadata();
		catch(Exception e) {
			throw e;
		return data;

Using the partition and offset details we can receive the event that was sent by calling receive method of the KafkaTemplate

ConsumerRecord<String, Order> orderRecord = kafkaTemplate.receive(topicName,Integer.parseInt(partition),Long.valueOf(offset));

Leave a Reply

Your email address will not be published. Required fields are marked *