From: Kai Moritz Date: Wed, 11 Jan 2023 18:09:53 +0000 (+0100) Subject: feat: Introduced config switches to choose the used implementations X-Git-Tag: wip-sharding~39 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=eac88250213f88a51c88ed2049f484ff183332a2;p=demos%2Fkafka%2Fchat 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`. --- 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)); + } +}