feat: The size buffer for listeners to a chatroom is configurable
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / ChatBackendConfiguration.java
1 package de.juplo.kafka.chat.backend;
2
3 import de.juplo.kafka.chat.backend.domain.ChatHome;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import de.juplo.kafka.chat.backend.domain.ChatroomFactory;
6 import de.juplo.kafka.chat.backend.persistence.InMemoryChatroomFactory;
7 import de.juplo.kafka.chat.backend.persistence.LocalJsonFilesStorageStrategy;
8 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
9 import org.springframework.boot.context.properties.EnableConfigurationProperties;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12
13 import java.nio.file.Paths;
14 import java.time.Clock;
15
16
17 @Configuration
18 @EnableConfigurationProperties(ChatBackendProperties.class)
19 public class ChatBackendConfiguration
20 {
21   @Bean
22   public ChatHome chatHome(
23       ChatroomFactory chatroomFactory,
24       StorageStrategy storageStrategy)
25   {
26     return new ChatHome(
27         storageStrategy.readChatrooms().collectMap(chatroom -> chatroom.getId()).block(),
28         chatroomFactory);
29   }
30
31   @Bean
32   public StorageStrategy storageStrategy(
33       ChatBackendProperties properties,
34       ObjectMapper mapper,
35       ChatroomFactory chatroomFactory)
36   {
37     return new LocalJsonFilesStorageStrategy(
38         Paths.get(properties.getDatadir()),
39         mapper,
40         chatroomFactory);
41   }
42
43   @Bean
44   ChatroomFactory chatroomFactory(ChatBackendProperties properties)
45   {
46     return new InMemoryChatroomFactory(properties.getChatroomBufferSize());
47   }
48
49   @Bean
50   public Clock clock()
51   {
52     return Clock.systemDefaultZone();
53   }
54 }