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.StorageStrategy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import java.time.Clock;
+import java.util.stream.IntStream;
@ConditionalOnProperty(
public class InMemoryServicesConfiguration
{
@Bean
- ChatHome[] chatHomes(
- ChatBackendProperties properties,
- InMemoryChatHomeService chatHomeService,
- StorageStrategy storageStrategy)
+ @ConditionalOnProperty(
+ prefix = "chat.backend.inmemory",
+ name = "sharding-strategy",
+ havingValue = "none",
+ matchIfMissing = true)
+ ChatHome noneShardingChatHome(StorageStrategy storageStrategy)
{
- ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()];
- storageStrategy
- .read()
- .subscribe(chatRoom ->
- {
- int shard = chatRoom.getShard();
- if (chatHomes[shard] == null)
- chatHomes[shard] = new ChatHome(chatHomeService, shard);
- });
- return chatHomes;
+ return new SimpleChatHome(storageStrategy.read());
}
@Bean
- InMemoryChatHomeService chatHomeService(
+ @ConditionalOnProperty(
+ prefix = "chat.backend.inmemory",
+ name = "sharding-strategy",
+ havingValue = "kafkalike")
+ ChatHome kafkalikeShardingChatHome(
ChatBackendProperties properties,
StorageStrategy storageStrategy)
{
- return new InMemoryChatHomeService(
- properties.getInmemory().getNumShards(),
- properties.getInmemory().getOwnedShards(),
- storageStrategy.read());
+ 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