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-config-package~5 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=b8312110fdb82d756aa953ecb02b6f220f2b6717;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/InMemoryServicesAutoConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesAutoConfiguration.java new file mode 100644 index 00000000..7e3667df --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesAutoConfiguration.java @@ -0,0 +1,31 @@ +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 InMemoryServicesAutoConfiguration +{ + @Bean + InMemoryChatHomeService chatHomeService( + StorageStrategy storageStrategy, + Clock clock, + ChatBackendProperties properties) + { + return new InMemoryChatHomeService( + storageStrategy.read(), + clock, + properties.getChatroomBufferSize()); + } +} 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 deleted file mode 100644 index 20a1b5f6..00000000 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java +++ /dev/null @@ -1,25 +0,0 @@ -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.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.time.Clock; - - -@Configuration -public class InMemoryServicesConfiguration -{ - @Bean - InMemoryChatHomeService chatHomeService( - StorageStrategy storageStrategy, - Clock clock, - ChatBackendProperties properties) - { - return new InMemoryChatHomeService( - storageStrategy.read(), - clock, - properties.getChatroomBufferSize()); - } -} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageAutoConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageAutoConfiguration.java new file mode 100644 index 00000000..2a3936cd --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageAutoConfiguration.java @@ -0,0 +1,36 @@ +package de.juplo.kafka.chat.backend.persistence.storage.files; + +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; + +import java.nio.file.Paths; +import java.time.Clock; + + +@ConditionalOnProperty( + prefix = "chat.backend", + name = "storage", + havingValue = "files", + matchIfMissing = true) +@Configuration +public class FilesStorageAutoConfiguration +{ + @Bean + public StorageStrategy storageStrategy( + ChatBackendProperties properties, + Clock clock, + ObjectMapper mapper) + { + return new FilesStorageStrategy( + Paths.get(properties.getStorageDirectory()), + clock, + properties.getChatroomBufferSize(), + messageFlux -> new InMemoryChatRoomService(messageFlux), + mapper); + } +} 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 deleted file mode 100644 index 32bedb8f..00000000 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.juplo.kafka.chat.backend.persistence.storage.files; - -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.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.nio.file.Paths; -import java.time.Clock; - - -@Configuration -public class FilesStorageConfiguration -{ - @Bean - public StorageStrategy storageStrategy( - ChatBackendProperties properties, - Clock clock, - ObjectMapper mapper) - { - return new FilesStorageStrategy( - Paths.get(properties.getStorageDirectory()), - clock, - properties.getChatroomBufferSize(), - messageFlux -> new InMemoryChatRoomService(messageFlux), - mapper); - } -} 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..120fab34 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java @@ -0,0 +1,38 @@ +package de.juplo.kafka.chat.backend.persistence.storage.mongodb; + +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 de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageStrategy; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; + +import java.nio.file.Paths; +import java.time.Clock; + + +@ConditionalOnProperty( + prefix = "chat.backend", + name = "mongodb", + havingValue = "files") +@Configuration +@EnableReactiveMongoRepositories +public class MongoDbStorageConfiguration +{ + @Bean + public StorageStrategy storageStrategy( + ChatBackendProperties properties, + Clock clock, + ObjectMapper mapper) + { + return new FilesStorageStrategy( + Paths.get(properties.getStorageDirectory()), + clock, + properties.getChatroomBufferSize(), + messageFlux -> new InMemoryChatRoomService(messageFlux), + mapper); + } +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..6688cd53 --- /dev/null +++ b/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryServicesAutoConfiguration,\ +de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageAutoConfiguration