From da026a0e3b259d2c2b76016bc0acee7cc8265db4 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 11 Jan 2023 19:09:53 +0100 Subject: [PATCH] feat: Introduced config switches to choose the used implementations - Switched the existing `@Configuration`-classes into an auto-configuration. - Added a `@ConditionalOnProperty`, that loades the default-configuration. - Added an Auto-Configuration for the `MongoDbStorageStrategy`, which is inactive by default and can be switched on by the property `chat.backend.storage` to `mongodb`. --- .../backend/ChatBackendConfiguration.java | 2 -- .../InMemoryServicesConfiguration.java | 6 ++++ .../files/FilesStorageConfiguration.java | 6 ++++ .../mongodb/MongoDbStorageConfiguration.java | 32 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java index ac77748e..10e9d37b 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java @@ -5,14 +5,12 @@ import de.juplo.kafka.chat.backend.domain.ChatHomeService; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; import java.time.Clock; @Configuration @EnableConfigurationProperties(ChatBackendProperties.class) -@EnableReactiveMongoRepositories public class ChatBackendConfiguration { @Bean diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java index 20a1b5f6..3a990191 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java @@ -2,12 +2,18 @@ package de.juplo.kafka.chat.backend.persistence.inmemory; import de.juplo.kafka.chat.backend.ChatBackendProperties; import de.juplo.kafka.chat.backend.persistence.StorageStrategy; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.time.Clock; +@ConditionalOnProperty( + prefix = "chat.backend", + name = "services", + havingValue = "in-memory", + matchIfMissing = true) @Configuration public class InMemoryServicesConfiguration { diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java index 32bedb8f..ae6af2f5 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import de.juplo.kafka.chat.backend.ChatBackendProperties; import de.juplo.kafka.chat.backend.persistence.StorageStrategy; import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -11,6 +12,11 @@ import java.nio.file.Paths; import java.time.Clock; +@ConditionalOnProperty( + prefix = "chat.backend", + name = "storage", + havingValue = "files", + matchIfMissing = true) @Configuration public class FilesStorageConfiguration { diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java new file mode 100644 index 00000000..0a3df755 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java @@ -0,0 +1,32 @@ +package de.juplo.kafka.chat.backend.persistence.storage.mongodb; + +import de.juplo.kafka.chat.backend.ChatBackendProperties; +import de.juplo.kafka.chat.backend.persistence.StorageStrategy; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.time.Clock; + + +@ConditionalOnProperty( + prefix = "chat.backend", + name = "storage", + havingValue = "mongodb") +@Configuration +public class MongoDbStorageConfiguration +{ + @Bean + public StorageStrategy storageStrategy( + ChatRoomRepository chatRoomRepository, + ChatBackendProperties properties, + Clock clock) + { + return new MongoDbStorageStrategy( + chatRoomRepository, + clock, + properties.getChatroomBufferSize(), + messageFlux -> new InMemoryChatRoomService(messageFlux)); + } +} -- 2.20.1