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.domain.ChatHome;
6 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
7 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration;
11 import java.time.Clock;
12 import java.util.stream.IntStream;
15 @ConditionalOnProperty(
16 prefix = "chat.backend",
18 havingValue = "inmemory",
19 matchIfMissing = true)
21 public class InMemoryServicesConfiguration
24 @ConditionalOnProperty(
25 prefix = "chat.backend.inmemory",
26 name = "sharding-strategy",
28 matchIfMissing = true)
29 ChatHome noneShardingChatHome(InMemoryChatHomeService chatHomeService)
31 return new SimpleChatHome(chatHomeService);
35 @ConditionalOnProperty(
36 prefix = "chat.backend.inmemory",
37 name = "sharding-strategy",
38 havingValue = "kafkalike")
39 ChatHome kafkalikeShardingChatHome(
40 ChatBackendProperties properties,
41 InMemoryChatHomeService chatHomeService)
43 int numShards = properties.getInmemory().getNumShards();
44 SimpleChatHome[] chatHomes = new SimpleChatHome[numShards];
46 .of(properties.getInmemory().getOwnedShards())
47 .forEach(shard -> chatHomes[shard] = new SimpleChatHome(chatHomeService, shard));
48 ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards);
49 return new ShardedChatHome(chatHomes, strategy);
53 InMemoryChatHomeService chatHomeService(
54 ChatBackendProperties properties,
55 StorageStrategy storageStrategy)
57 ShardingStrategyType sharding =
58 properties.getInmemory().getShardingStrategy();
59 int numShards = sharding == ShardingStrategyType.none
61 : properties.getInmemory().getNumShards();
62 int[] ownedShards = sharding == ShardingStrategyType.none
64 : properties.getInmemory().getOwnedShards();
65 return new InMemoryChatHomeService(
68 storageStrategy.read());
72 InMemoryChatRoomFactory chatRoomFactory(
73 InMemoryChatHomeService service,
74 ShardingStrategy strategy,
76 ChatBackendProperties properties)
78 return new InMemoryChatRoomFactory(
82 properties.getChatroomBufferSize());
85 @ConditionalOnProperty(
86 prefix = "chat.backend.inmemory",
87 name = "sharding-strategy",
89 matchIfMissing = true)
91 ShardingStrategy defaultShardingStrategy()
93 return chatRoomId -> 0;
96 @ConditionalOnProperty(
97 prefix = "chat.backend.inmemory",
98 name = "sharding-strategy",
99 havingValue = "kafkalike")
101 ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties)
103 return new KafkaLikeShardingStrategy(
104 properties.getInmemory().getNumShards());