X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fpersistence%2Finmemory%2FInMemoryServicesConfiguration.java;h=d2fd731eb14f97291b6bf080e967069122900840;hb=e8d8cb2aba9988608ee98b0a7dfc1053b6429040;hp=94dd5d8a0aa5b39ee437d1f9c190f6b34528914d;hpb=ff98b068a91fc9e60e51bd4a95065633bb8ed2db;p=demos%2Fkafka%2Fchat 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 94dd5d8a..d2fd731e 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 @@ -1,14 +1,15 @@ package de.juplo.kafka.chat.backend.persistence.inmemory; import de.juplo.kafka.chat.backend.ChatBackendProperties; -import de.juplo.kafka.chat.backend.api.KafkaLikeShardingStrategy; -import de.juplo.kafka.chat.backend.api.ShardingStrategy; +import de.juplo.kafka.chat.backend.domain.ChatHome; +import de.juplo.kafka.chat.backend.persistence.ShardingStrategy; 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; +import java.util.stream.IntStream; @ConditionalOnProperty( @@ -20,32 +21,43 @@ import java.time.Clock; public class InMemoryServicesConfiguration { @Bean - InMemoryChatHomeService chatHomeService( + @ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "sharding-strategy", + havingValue = "none", + matchIfMissing = true) + ChatHome noneShardingChatHome( ChatBackendProperties properties, - StorageStrategy storageStrategy) - { - return new InMemoryChatHomeService( - properties.getInmemory().getNumShards(), - properties.getInmemory().getOwnedShards(), - storageStrategy.read()); - } - - @Bean - InMemoryChatHomeFactory chatHomeFactory(InMemoryChatHomeService service) + StorageStrategy storageStrategy, + Clock clock) { - return new InMemoryChatHomeFactory(service); + return new SimpleChatHome( + storageStrategy, + clock, + properties.getChatroomBufferSize()); } @Bean - InMemoryChatRoomFactory chatRoomFactory( - ShardingStrategy strategy, - Clock clock, - ChatBackendProperties properties) + @ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "sharding-strategy", + havingValue = "kafkalike") + ChatHome kafkalikeShardingChatHome( + ChatBackendProperties properties, + StorageStrategy storageStrategy, + Clock clock) { - return new InMemoryChatRoomFactory( - strategy, - clock, - properties.getChatroomBufferSize()); + int numShards = properties.getInmemory().getNumShards(); + SimpleChatHome[] chatHomes = new SimpleChatHome[numShards]; + IntStream + .of(properties.getInmemory().getOwnedShards()) + .forEach(shard -> chatHomes[shard] = new SimpleChatHome( + shard, + storageStrategy, + clock, + properties.getChatroomBufferSize())); + ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards); + return new ShardedChatHome(chatHomes, strategy); } @ConditionalOnProperty(