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.ShardedChatHome;
6 import de.juplo.kafka.chat.backend.persistence.KafkaLikeShardingStrategy;
7 import de.juplo.kafka.chat.backend.domain.ShardingStrategy;
8 import de.juplo.kafka.chat.backend.domain.ChatHome;
9 import de.juplo.kafka.chat.backend.domain.SimpleChatHome;
10 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
11 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.Configuration;
15 import java.time.Clock;
18 @ConditionalOnProperty(
19 prefix = "chat.backend",
21 havingValue = "inmemory",
22 matchIfMissing = true)
24 public class InMemoryServicesConfiguration
27 @ConditionalOnProperty(
28 prefix = "chat.backend.inmemory",
29 name = "sharding-strategy",
31 matchIfMissing = true)
32 ChatHome noneShardingChatHome(InMemoryChatHomeService chatHomeService)
34 return new SimpleChatHome(chatHomeService);
38 @ConditionalOnProperty(
39 prefix = "chat.backend.inmemory",
40 name = "sharding-strategy",
41 havingValue = "kafkalike")
42 ChatHome kafkalikeShardingChatHome(
43 ChatBackendProperties properties,
44 InMemoryChatHomeService chatHomeService,
45 StorageStrategy storageStrategy)
47 int numShards = properties.getInmemory().getNumShards();
48 SimpleChatHome[] chatHomes = new SimpleChatHome[numShards];
51 .subscribe(chatRoom ->
53 int shard = chatRoom.getShard();
54 if (chatHomes[shard] == null)
55 chatHomes[shard] = new SimpleChatHome(chatHomeService, shard);
57 ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards);
58 return new ShardedChatHome(chatHomes, strategy);
62 InMemoryChatHomeService chatHomeService(
63 ChatBackendProperties properties,
64 StorageStrategy storageStrategy)
66 ShardingStrategyType sharding =
67 properties.getInmemory().getShardingStrategy();
68 int numShards = sharding == ShardingStrategyType.none
70 : properties.getInmemory().getNumShards();
71 int[] ownedShards = sharding == ShardingStrategyType.none
73 : properties.getInmemory().getOwnedShards();
74 return new InMemoryChatHomeService(
77 storageStrategy.read());
81 InMemoryChatRoomFactory chatRoomFactory(
82 InMemoryChatHomeService service,
83 ShardingStrategy strategy,
85 ChatBackendProperties properties)
87 return new InMemoryChatRoomFactory(
91 properties.getChatroomBufferSize());
94 @ConditionalOnProperty(
95 prefix = "chat.backend.inmemory",
96 name = "sharding-strategy",
98 matchIfMissing = true)
100 ShardingStrategy defaultShardingStrategy()
102 return chatRoomId -> 0;
105 @ConditionalOnProperty(
106 prefix = "chat.backend.inmemory",
107 name = "sharding-strategy",
108 havingValue = "kafkalike")
110 ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties)
112 return new KafkaLikeShardingStrategy(
113 properties.getInmemory().getNumShards());