Skip to main content
Version: 12.10.0

Kafka Utility Library for Spring Boot

The Kafka Utility Library for Spring Boot provides a convenient way to produce and consume Kafka topics in Spring Boot applications. This library encapsulates the complexities of interacting with Kafka, making it easy for other services to integrate Kafka messaging capabilities.

Features Getting Started Prerequisites Installation Usage Kafka Producer Kafka Consumer Example Sequencing

Features

Easy integration of Kafka messaging in Spring Boot applications. Simplified Kafka producer and consumer configuration. Abstracted error handling and message serialization. Customizable message serialization and deserialization strategies.

Getting Started

Prerequisites

Java 8 or higher

Installation

Add the Kafka Utility Library as a dependency in your Spring Boot project:

for maven ( edit the pom.xml )

<dependency>
<groupId>com.example</groupId>
<artifactId>oc-kafka-utility</artifactId>
<version>1.0.0</version>
</dependency>

for gradle ( edit the build.gradle )

implementation 'navida.pro:kafka-utility:1.0.0'

Configure Kafka properties in your application.properties or application.yml file. These properties include the Kafka broker servers and other Kafka-specific configurations. These properties should match the configuration used in the Kafka Utility Library.

properties

spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=my-group

# Security configurations
spring.kafka.properties.security.protocol=SASL_SSL
spring.kafka.properties.sasl.mechanism=PLAIN
spring.kafka.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="your-username" password="your-password";


Define Kafka topic names in your application.properties or application.yml file:

kafka.topics.oc-topic=my-topic-name

Usage

code sample

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class KafkaUtility {

private final KafkaTemplate<String, String> kafkaTemplate;

@Autowired
public KafkaUtility(KafkaTemplate<String, String> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}

public void sendMessage(String topic, String message) {
kafkaTemplate.send(topic, message);
}

// Add methods for receiving messages if needed
}

Use Kafka Producer:

In the other services' code, they can now use the Kafka producer from your utility library. They'll instantiate the KafkaProducerService (or similar) class and call the sendMessage() method to produce messages to a specific topic.

Example usage in other services:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import navida.pro.kafka-utility

@Service
public class MyProducerService {

private final KafkaUtility kafkaUtility;

@Autowired
public MyProducerService(KafkaUtility kafkaUtility) {
this.kafkaUtility = kafkaUtility;
}

public void sendMessage(String topic, String message) {
kafkaUtility.send(topic, message);
}
}

Use Kafka Consumer

Example usage in other services:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import navida.pro.kafka-utility


@Service
public class MyConsumerService {

private final KafkaUtility kafkaUtility;

@Autowired
public MyConsumerService(KafkaUtility kafkaUtility) {
this.kafkaUtility = kafkaUtility;
}

public String consumeMessage( ) {
return kafkaUtility.receive(topic, message);
}
}

Sequencing

The sequence of interactions between the Kafka Utility Library components can be visualized as follows: