X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fpersistence%2Finmemory%2FInMemoryServicesConfiguration.java;h=00e0c6fde07d96f9c721cfc2894eb4c2fc9774bd;hb=1a6e2af4b700d92efe20ce5099affc01413c6eaa;hp=3a9901917c36849b9eddb11f9bb5ba5e2819c13d;hpb=eac88250213f88a51c88ed2049f484ff183332a2;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 3a990191..00e0c6fd 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,31 +1,106 @@ package de.juplo.kafka.chat.backend.persistence.inmemory; import de.juplo.kafka.chat.backend.ChatBackendProperties; +import de.juplo.kafka.chat.backend.ChatBackendProperties.ShardingStrategyType; +import de.juplo.kafka.chat.backend.domain.ChatHome; 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( prefix = "chat.backend", name = "services", - havingValue = "in-memory", + havingValue = "inmemory", matchIfMissing = true) @Configuration public class InMemoryServicesConfiguration { + @Bean + @ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "sharding-strategy", + havingValue = "none", + matchIfMissing = true) + ChatHome noneShardingChatHome() + { + return new SimpleChatHome(); + } + + @Bean + @ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "sharding-strategy", + havingValue = "kafkalike") + ChatHome kafkalikeShardingChatHome( + ChatBackendProperties properties, + InMemoryChatHomeService chatHomeService) + { + int numShards = properties.getInmemory().getNumShards(); + SimpleChatHome[] chatHomes = new SimpleChatHome[numShards]; + IntStream + .of(properties.getInmemory().getOwnedShards()) + .forEach(shard -> chatHomes[shard] = new SimpleChatHome(chatHomeService, shard)); + ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards); + return new ShardedChatHome(chatHomes, strategy); + } + @Bean InMemoryChatHomeService chatHomeService( - StorageStrategy storageStrategy, + ChatBackendProperties properties, + StorageStrategy storageStrategy) + { + ShardingStrategyType sharding = + properties.getInmemory().getShardingStrategy(); + int numShards = sharding == ShardingStrategyType.none + ? 1 + : properties.getInmemory().getNumShards(); + int[] ownedShards = sharding == ShardingStrategyType.none + ? new int[] { 0 } + : properties.getInmemory().getOwnedShards(); + return new InMemoryChatHomeService( + numShards, + ownedShards, + storageStrategy.read()); + } + + @Bean + InMemoryChatRoomFactory chatRoomFactory( + InMemoryChatHomeService service, + ShardingStrategy strategy, Clock clock, ChatBackendProperties properties) { - return new InMemoryChatHomeService( - storageStrategy.read(), + return new InMemoryChatRoomFactory( + service, + strategy, clock, properties.getChatroomBufferSize()); } + + @ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "sharding-strategy", + havingValue = "none", + matchIfMissing = true) + @Bean + ShardingStrategy defaultShardingStrategy() + { + return chatRoomId -> 0; + } + + @ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "sharding-strategy", + havingValue = "kafkalike") + @Bean + ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties) + { + return new KafkaLikeShardingStrategy( + properties.getInmemory().getNumShards()); + } }