X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fpersistence%2Finmemory%2FInMemoryServicesConfiguration.java;h=ee41c4f39d0e61f80d52e2b3876a4168e16b6cfa;hb=c5976140f6ad8c5a09753c528f0f01a69716dd6b;hp=20a1b5f650f83de3508a8cf234144754c2527666;hpb=6665467f76198e20f4852f3eb61d53ea9d07bfca;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 20a1b5f6..ee41c4f3 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,25 +1,84 @@ package de.juplo.kafka.chat.backend.persistence.inmemory; import de.juplo.kafka.chat.backend.ChatBackendProperties; +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 = "inmemory", + matchIfMissing = true) @Configuration public class InMemoryServicesConfiguration { @Bean - InMemoryChatHomeService chatHomeService( - StorageStrategy storageStrategy, + @ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "sharding-strategy", + havingValue = "none", + matchIfMissing = true) + ChatHome noneShardingChatHome(StorageStrategy storageStrategy) + { + return new SimpleChatHome(storageStrategy.read()); + } + + @Bean + @ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "sharding-strategy", + havingValue = "kafkalike") + ChatHome kafkalikeShardingChatHome( + ChatBackendProperties properties, + StorageStrategy storageStrategy) + { + int numShards = properties.getInmemory().getNumShards(); + SimpleChatHome[] chatHomes = new SimpleChatHome[numShards]; + IntStream + .of(properties.getInmemory().getOwnedShards()) + .forEach(shard -> chatHomes[shard] = new SimpleChatHome(shard, storageStrategy.read())); + ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards); + return new ShardedChatHome(chatHomes, strategy); + } + + @Bean + InMemoryChatRoomFactory chatRoomFactory( + ShardingStrategy strategy, Clock clock, ChatBackendProperties properties) { - return new InMemoryChatHomeService( - storageStrategy.read(), + return new InMemoryChatRoomFactory( + 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()); + } }