feat: Prepared the application for sharding
[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.ShardingStrategy;
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;
9
10 import java.time.Clock;
11
12
13 @ConditionalOnProperty(
14     prefix = "chat.backend",
15     name = "services",
16     havingValue = "in-memory",
17     matchIfMissing = true)
18 @Configuration
19 public class InMemoryServicesConfiguration
20 {
21   @Bean
22   InMemoryChatHomeService chatHomeService(StorageStrategy storageStrategy)
23   {
24     return new InMemoryChatHomeService(1, storageStrategy.read());
25   }
26
27   @Bean
28   InMemoryChatRoomFactory chatRoomFactory(
29       ShardingStrategy strategy,
30       Clock clock,
31       ChatBackendProperties properties)
32   {
33     return new InMemoryChatRoomFactory(
34         strategy,
35         clock,
36         properties.getChatroomBufferSize());
37   }
38
39   @Bean
40   ShardingStrategy shardingStrategy()
41   {
42     return chatRoomId -> 0;
43   }
44 }