refactor: Migrate from monolithic to modular architecture
### **Service-Implementation** - [ ] **Tag 1**: Members-Service REST-API implementieren - [ ] **Tag 2**: Database-Migrations und Repository-Layer - [ ] **Tag 3**: Event-Publishing nach Kafka aktivieren - [ ] **Tag 4**: Horses-Service analog implementieren - [ ] **Tag 5**: Integration-Tests für beide Services - [ ] **Tag 6-7**: Events-Service und Masterdata-Service
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
package at.mocode.infrastructure.messaging.client
|
||||
|
||||
/**
|
||||
* Interface for publishing domain events to message broker.
|
||||
*/
|
||||
interface EventPublisher {
|
||||
|
||||
/**
|
||||
* Publishes an event to the specified topic.
|
||||
*
|
||||
* @param topic The topic to publish to
|
||||
* @param key The message key (optional)
|
||||
* @param event The event to publish
|
||||
*/
|
||||
suspend fun publishEvent(topic: String, key: String? = null, event: Any)
|
||||
|
||||
/**
|
||||
* Publishes multiple events to the specified topic.
|
||||
*
|
||||
* @param topic The topic to publish to
|
||||
* @param events The events to publish with their keys
|
||||
*/
|
||||
suspend fun publishEvents(topic: String, events: List<Pair<String?, Any>>)
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package at.mocode.infrastructure.messaging.client
|
||||
|
||||
import kotlinx.coroutines.future.await
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.kafka.core.KafkaTemplate
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
/**
|
||||
* Kafka implementation of EventPublisher.
|
||||
*/
|
||||
@Component
|
||||
class KafkaEventPublisher(
|
||||
private val kafkaTemplate: KafkaTemplate<String, Any>
|
||||
) : EventPublisher {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(KafkaEventPublisher::class.java)
|
||||
|
||||
override suspend fun publishEvent(topic: String, key: String?, event: Any) {
|
||||
try {
|
||||
logger.debug("Publishing event to topic '{}' with key '{}'", topic, key)
|
||||
|
||||
val sendResult = if (key != null) {
|
||||
kafkaTemplate.send(topic, key, event).get()
|
||||
} else {
|
||||
kafkaTemplate.send(topic, event).get()
|
||||
}
|
||||
|
||||
logger.info("Successfully published event to topic '{}' with key '{}'", topic, key)
|
||||
} catch (exception: Exception) {
|
||||
logger.error("Failed to publish event to topic '{}' with key '{}'", topic, key, exception)
|
||||
throw exception
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun publishEvents(topic: String, events: List<Pair<String?, Any>>) {
|
||||
try {
|
||||
logger.debug("Publishing {} events to topic '{}'", events.size, topic)
|
||||
|
||||
events.forEach { (key, event) ->
|
||||
publishEvent(topic, key, event)
|
||||
}
|
||||
|
||||
logger.info("Successfully published {} events to topic '{}'", events.size, topic)
|
||||
} catch (exception: Exception) {
|
||||
logger.error("Failed to publish events to topic '{}'", topic, exception)
|
||||
throw exception
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user