A reusable Kafka messaging library for the DevSocket eCommerce microservices ecosystem. This module abstracts Kafka producer and consumer logic, enabling services to send and receive messages without duplicating boilerplate code.
- Generic Kafka producer and consumer support
- Delegated message handling via
KafkaEventHandler - Manual acknowledgment for reliable processing
- Plug-and-play integration with any microservice
- JSON serialization/deserialization out of the box
- Centralized configuration via Spring Boot
This module is designed to decouple Kafka messaging logic from business services, allowing microservices to focus solely on handling domain events.
┌────────────────────────────┐
│ Microservice A │
│ ┌────────────────────────┐ │
│ │ KafkaEventHandler<T> │◄─── Handles domain-specific logic
│ └────────────────────────┘ │
│ ▲ │
│ │ Delegates │
└────────┼────────────────────┘
│ ▼
┌────────────────────────────────────────────┐
│ ecommerce-kafka module │
│ ┌────────────────────────────────────────┐ │
│ │ KafkaListener │ │
│ │ └── Receives Kafka messages │ │
│ │ └── Delegates to KafkaEventHandler │ │
│ │ └── Acknowledges after success │ │
│ └────────────────────────────────────────┘ │
│ ┌────────────────────────────────────────┐ │
│ │ KafkaProducerService<T> │ │
│ │ └── Publishes events to Kafka topics │ │
│ └────────────────────────────────────────┘ │
│ ┌────────────────────────────────────────┐ │
│ │ KafkaConfig │ │
│ │ └── Sets up bootstrap servers │ │
│ │ └── Configures serializers/deserial. │ │
│ └────────────────────────────────────────┘ │
└────────────────────────────────────────────┘
Add the following to your microservice's application.yml:
kafka:
bootstrap-servers: localhost:9092Implement the KafkaEventHandler interface in your microservice:
@Component
public class OrderEventHandler implements KafkaEventHandler<Object> {
@Override
public void handleEvent(Object event) {
// Cast and process your event
System.out.println("Received: " + event);
}
}The KafkaListener class in this module will automatically delegate incoming messages to your handler.
Use the KafkaProducerService<T> to send messages:
@Autowired
private KafkaProducerService<OrderEvent> producer;
public void publishOrder(OrderEvent event) {
producer.send("order-events", event);
}Use spring-kafka-test for integration testing with embedded Kafka:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>Class Purpose KafkaListener Receives messages and delegates to handler KafkaEventHandler Interface for microservices to implement KafkaProducerService Sends messages to Kafka topics KafkaConfig Configures Kafka producer/consumer factories
This module ensures reliable message processing with manual acknowledgment and robust error reporting.
- ✅ Messages are acknowledged only after successful handling
- 🛑 If an exception occurs:
- The error is logged with full context (event, partition, offset, key)
- The exception is rethrown to trigger Kafka retries or dead-letter queue (DLQ) behavior
- 📊 You can extend the listener to:
- Forward failed events to a DLQ topic
- Report errors to monitoring systems like Sentry, Datadog, or Prometheus
Add this module as a dependency in your microservice:
<dependency>
<groupId>com.devsocket.ecommerce</groupId>
<artifactId>common-kafka</artifactId>
<version>1.0.0</version>
</dependency>Feel free to open issues or submit PRs to improve flexibility, add support for Avro/Protobuf, or extend topic routing.