1 package de.juplo.kafka.chat.backend.persistence.inmemory;
3 import de.juplo.kafka.chat.backend.ChatBackendProperties;
4 import de.juplo.kafka.chat.backend.ChatBackendProperties.ShardingStrategyType;
5 import de.juplo.kafka.chat.backend.persistence.KafkaLikeShardingStrategy;
6 import de.juplo.kafka.chat.backend.domain.ShardingStrategy;
7 import de.juplo.kafka.chat.backend.domain.ChatHome;
8 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
9 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
13 import java.time.Clock;
16 @ConditionalOnProperty(
17 prefix = "chat.backend",
19 havingValue = "inmemory",
20 matchIfMissing = true)
22 public class InMemoryServicesConfiguration
25 ChatHome chatHome(InMemoryChatHomeService chatHomeService)
27 return new ChatHome(chatHomeService);
31 InMemoryChatHomeService chatHomeService(
32 ChatBackendProperties properties,
33 StorageStrategy storageStrategy)
35 ShardingStrategyType sharding =
36 properties.getInmemory().getShardingStrategy();
40 ShardingStrategy shardingStrategy;
46 ownedShards = new int[] { 0 };
47 shardingStrategy = id -> 0;
50 numShards = properties.getInmemory().getNumShards();
51 ownedShards = properties.getInmemory().getOwnedShards();
52 shardingStrategy = new KafkaLikeShardingStrategy(numShards);
55 throw new IllegalArgumentException("Unknown sharding strategy: " + sharding);
58 return new InMemoryChatHomeService(
62 storageStrategy.read());
66 InMemoryChatRoomFactory chatRoomFactory(
67 InMemoryChatHomeService service,
68 ShardingStrategy strategy,
70 ChatBackendProperties properties)
72 return new InMemoryChatRoomFactory(
76 properties.getChatroomBufferSize());
79 @ConditionalOnProperty(
80 prefix = "chat.backend.inmemory",
81 name = "sharding-strategy",
83 matchIfMissing = true)
85 ShardingStrategy defaultShardingStrategy()
87 return chatRoomId -> 0;
90 @ConditionalOnProperty(
91 prefix = "chat.backend.inmemory",
92 name = "sharding-strategy",
93 havingValue = "kafkalike")
95 ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties)
97 return new KafkaLikeShardingStrategy(
98 properties.getInmemory().getNumShards());