1 package de.juplo.kafka.chat.backend.persistence.inmemory;
3 import de.juplo.kafka.chat.backend.ChatBackendProperties;
4 import de.juplo.kafka.chat.backend.domain.ChatHome;
5 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
6 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.context.annotation.Configuration;
10 import java.time.Clock;
11 import java.util.stream.IntStream;
14 @ConditionalOnProperty(
15 prefix = "chat.backend",
17 havingValue = "inmemory",
18 matchIfMissing = true)
20 public class InMemoryServicesConfiguration
23 @ConditionalOnProperty(
24 prefix = "chat.backend.inmemory",
25 name = "sharding-strategy",
27 matchIfMissing = true)
28 ChatHome noneShardingChatHome(
29 ChatBackendProperties properties,
30 StorageStrategy storageStrategy,
33 return new SimpleChatHome(
36 properties.getChatroomBufferSize());
40 @ConditionalOnProperty(
41 prefix = "chat.backend.inmemory",
42 name = "sharding-strategy",
43 havingValue = "kafkalike")
44 ChatHome kafkalikeShardingChatHome(
45 ChatBackendProperties properties,
46 StorageStrategy storageStrategy,
49 int numShards = properties.getInmemory().getNumShards();
50 SimpleChatHome[] chatHomes = new SimpleChatHome[numShards];
52 .of(properties.getInmemory().getOwnedShards())
53 .forEach(shard -> chatHomes[shard] = new SimpleChatHome(
57 properties.getChatroomBufferSize()));
58 ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards);
59 return new ShardedChatHome(chatHomes, strategy);
62 @ConditionalOnProperty(
63 prefix = "chat.backend.inmemory",
64 name = "sharding-strategy",
66 matchIfMissing = true)
68 ShardingStrategy defaultShardingStrategy()
70 return chatRoomId -> 0;
73 @ConditionalOnProperty(
74 prefix = "chat.backend.inmemory",
75 name = "sharding-strategy",
76 havingValue = "kafkalike")
78 ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties)
80 return new KafkaLikeShardingStrategy(
81 properties.getInmemory().getNumShards());