feat: Introduced a kafka-like `ShardingStrategy` for `inmemory`
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / persistence / inmemory / InMemoryServicesConfiguration.java
1 package de.juplo.kafka.chat.backend.persistence.inmemory;
2
3 import de.juplo.kafka.chat.backend.ChatBackendProperties;
4 import de.juplo.kafka.chat.backend.api.KafkaLikeShardingStrategy;
5 import de.juplo.kafka.chat.backend.api.ShardingStrategy;
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;
10
11 import java.time.Clock;
12
13
14 @ConditionalOnProperty(
15     prefix = "chat.backend",
16     name = "services",
17     havingValue = "inmemory",
18     matchIfMissing = true)
19 @Configuration
20 public class InMemoryServicesConfiguration
21 {
22   @Bean
23   InMemoryChatHomeService chatHomeService(
24       ChatBackendProperties properties,
25       StorageStrategy storageStrategy)
26   {
27     return new InMemoryChatHomeService(
28         properties.getInmemory().getNumShards(),
29         properties.getInmemory().getOwnedShards(),
30         storageStrategy.read());
31   }
32
33   @Bean
34   InMemoryChatHomeFactory chatHomeFactory(InMemoryChatHomeService service)
35   {
36     return new InMemoryChatHomeFactory(service);
37   }
38
39   @Bean
40   InMemoryChatRoomFactory chatRoomFactory(
41       ShardingStrategy strategy,
42       Clock clock,
43       ChatBackendProperties properties)
44   {
45     return new InMemoryChatRoomFactory(
46         strategy,
47         clock,
48         properties.getChatroomBufferSize());
49   }
50
51   @ConditionalOnProperty(
52       prefix = "chat.backend.inmemory",
53       name = "sharding-strategy",
54       havingValue = "none",
55       matchIfMissing = true)
56   @Bean
57   ShardingStrategy defaultShardingStrategy()
58   {
59     return chatRoomId -> 0;
60   }
61
62   @ConditionalOnProperty(
63       prefix = "chat.backend.inmemory",
64       name = "sharding-strategy",
65       havingValue = "kafkalike")
66   @Bean
67   ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties)
68   {
69     return new KafkaLikeShardingStrategy(
70         properties.getInmemory().getNumShards());
71   }
72 }