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());
41 @ConditionalOnProperty(
42 prefix = "chat.backend.inmemory",
43 name = "sharding-strategy",
44 havingValue = "kafkalike")
45 ChatHomeService kafkalikeShardingChatHome(
46 ChatBackendProperties properties,
47 StorageStrategy storageStrategy,
50 int numShards = properties.getInmemory().getNumShards();
51 SimpleChatHomeService[] chatHomes = new SimpleChatHomeService[numShards];
53 .of(properties.getInmemory().getOwnedShards())
54 .forEach(shard -> chatHomes[shard] = new SimpleChatHomeService(
58 properties.getChatroomBufferSize()));
59 ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards);
60 return new ShardedChatHomeService(
61 properties.getInstanceId(),
63 properties.getInmemory().getShardOwners(),
67 @ConditionalOnProperty(
68 prefix = "chat.backend.inmemory",
69 name = "sharding-strategy",
71 matchIfMissing = true)
73 ShardingStrategy defaultShardingStrategy()
75 return chatRoomId -> 0;
78 @ConditionalOnProperty(
79 prefix = "chat.backend.inmemory",
80 name = "sharding-strategy",
81 havingValue = "kafkalike")
83 ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties)
85 return new KafkaLikeShardingStrategy(
86 properties.getInmemory().getNumShards());