feat(infra-messaging): Implement fully reactive Kafka producer and consumer
This commit introduces a comprehensive refactoring of the messaging module to establish a fully reactive, non-blocking, and robust infrastructure for Kafka-based communication. Features & Refinements Reactive Publisher: The KafkaEventPublisher has been refactored from a blocking implementation (KafkaTemplate) to a fully non-blocking, reactive one using Spring's ReactiveKafkaProducerTemplate. The EventPublisher interface now returns reactive types (Mono, Flux) to reflect the asynchronous nature of the operations. Reactive Consumer: A new KafkaEventConsumer has been implemented, providing a standardized, reusable, and reactive way for services to consume events. It encapsulates the complexity of reactor-kafka and exposes a simple receiveEvents<T>(topic) method that returns a Flux<T>. Architectural Cleanup: The Spring configuration has been split. The basic ProducerFactory and consumer properties reside in messaging-config, while the reactive-specific ReactiveKafkaProducerTemplate bean is now correctly located in messaging-client. Testing Added Kafka Integration Test: A new KafkaIntegrationTest has been created to ensure the reliability of the messaging infrastructure. The test uses Testcontainers to spin up a real Apache Kafka broker for end-to-end validation. Project Reactor's StepVerifier is used to test the reactive streams deterministically, avoiding flaky tests. The test correctly manages the lifecycle of Kafka producers to ensure clean shutdown without hanging threads. Bug Fixes Resolved UninitializedPropertyAccessException in tests by making the KafkaConfig test-friendly. Fixed IllegalStateException related to Testcontainers lifecycle by making the container a static resource. Corrected compilation errors in tests related to resource cleanup by using the concrete DefaultKafkaProducerFactory type.
This commit is contained in:
+23
-6
@@ -1,6 +1,8 @@
|
||||
package at.mocode.infrastructure.messaging.config
|
||||
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig
|
||||
import org.apache.kafka.clients.producer.ProducerConfig
|
||||
import org.apache.kafka.common.serialization.StringDeserializer
|
||||
import org.apache.kafka.common.serialization.StringSerializer
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.context.annotation.Bean
|
||||
@@ -8,16 +10,18 @@ import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.kafka.core.DefaultKafkaProducerFactory
|
||||
import org.springframework.kafka.core.KafkaTemplate
|
||||
import org.springframework.kafka.core.ProducerFactory
|
||||
import org.springframework.kafka.support.serializer.JsonDeserializer
|
||||
import org.springframework.kafka.support.serializer.JsonSerializer
|
||||
|
||||
/**
|
||||
* Kafka configuration for event publishing.
|
||||
*/
|
||||
@Configuration
|
||||
class KafkaConfig {
|
||||
|
||||
// KORREKTUR: Von lateinit zu einer public var mit Standardwert, um Tests zu ermöglichen
|
||||
@Value($$"${spring.kafka.bootstrap-servers:localhost:9092}")
|
||||
private lateinit var bootstrapServers: String
|
||||
var bootstrapServers: String = "localhost:9092"
|
||||
|
||||
@Value("\${spring.kafka.consumer.group-id:meldestelle-group}")
|
||||
private lateinit var consumerGroupId: String
|
||||
|
||||
@Bean
|
||||
fun producerFactory(): ProducerFactory<String, Any> {
|
||||
@@ -34,7 +38,20 @@ class KafkaConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
fun kafkaTemplate(): KafkaTemplate<String, Any> {
|
||||
return KafkaTemplate(producerFactory())
|
||||
fun kafkaTemplate(producerFactory: ProducerFactory<String, Any>): KafkaTemplate<String, Any> {
|
||||
return KafkaTemplate(producerFactory)
|
||||
}
|
||||
|
||||
// NEU: Stellt eine zentrale Map mit den Basis-Konfigurationen für alle Consumer bereit.
|
||||
@Bean
|
||||
fun kafkaConsumerConfiguration(): Map<String, Any> {
|
||||
return mapOf(
|
||||
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServers,
|
||||
ConsumerConfig.GROUP_ID_CONFIG to consumerGroupId,
|
||||
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java,
|
||||
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to JsonDeserializer::class.java,
|
||||
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to "earliest", // Beginne davon am Anfang, wenn kein Offset existiert
|
||||
JsonDeserializer.TRUSTED_PACKAGES to "*" // Erlaube Deserialisierung aller unserer Klassen
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user