1 package de.juplo.kafka.chat.backend.implementation.inmemory;
3 import de.juplo.kafka.chat.backend.ChatBackendProperties;
4 import de.juplo.kafka.chat.backend.domain.ChatHomeService;
5 import de.juplo.kafka.chat.backend.implementation.ShardingStrategy;
6 import de.juplo.kafka.chat.backend.implementation.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 ChatHomeService noneShardingChatHome(
30 ChatBackendProperties properties,
31 StorageStrategy storageStrategy,
34 return new SimpleChatHomeService(
37 properties.getChatroomBufferSize(),
38 properties.getInstanceUri());
42 @ConditionalOnProperty(
43 prefix = "chat.backend.inmemory",
44 name = "sharding-strategy",
45 havingValue = "kafkalike")
46 ChatHomeService kafkalikeShardingChatHome(
47 ChatBackendProperties properties,
48 StorageStrategy storageStrategy,
51 int numShards = properties.getInmemory().getNumShards();
52 SimpleChatHomeService[] chatHomes = new SimpleChatHomeService[numShards];
54 .of(properties.getInmemory().getOwnedShards())
55 .forEach(shard -> chatHomes[shard] = new SimpleChatHomeService(
59 properties.getChatroomBufferSize()));
60 ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards);
61 return new ShardedChatHomeService(chatHomes, strategy);
64 @ConditionalOnProperty(
65 prefix = "chat.backend.inmemory",
66 name = "sharding-strategy",
68 matchIfMissing = true)
70 ShardingStrategy defaultShardingStrategy()
72 return chatRoomId -> 0;
75 @ConditionalOnProperty(
76 prefix = "chat.backend.inmemory",
77 name = "sharding-strategy",
78 havingValue = "kafkalike")
80 ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties)
82 return new KafkaLikeShardingStrategy(
83 properties.getInmemory().getNumShards());