From: Kai Moritz Date: Sat, 14 Jan 2023 15:01:55 +0000 (+0100) Subject: refactor: Removed `ChatHomeFactory` X-Git-Tag: wip-sharding~28 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=3526be7c01fa286f4253c96fd7f6c35abf3694ef;p=demos%2Fkafka%2Fchat refactor: Removed `ChatHomeFactory` - `ChatHomeFactory` was only used during initialization. - By moving the instanciation of `chatHomes` into the implementation- specific configuration, the necessity of the factory vanished. --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java index 6e74bb01..f49e385b 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java @@ -1,9 +1,5 @@ package de.juplo.kafka.chat.backend; -import de.juplo.kafka.chat.backend.domain.ChatHome; -import de.juplo.kafka.chat.backend.domain.ChatHomeFactory; -import de.juplo.kafka.chat.backend.domain.ChatHomeService; -import de.juplo.kafka.chat.backend.persistence.StorageStrategy; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -15,24 +11,6 @@ import java.time.Clock; @EnableConfigurationProperties(ChatBackendProperties.class) public class ChatBackendConfiguration { - @Bean - ChatHome[] chatHomes( - ChatHomeFactory factory, - ChatBackendProperties properties, - StorageStrategy storageStrategy) - { - ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()]; - storageStrategy - .read() - .subscribe(chatRoom -> - { - int shard = chatRoom.getShard(); - if (chatHomes[shard] == null) - chatHomes[shard] = factory.createChatHome(shard); - }); - return chatHomes; - } - @Bean Clock clock() { diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java deleted file mode 100644 index fb20cc7b..00000000 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java +++ /dev/null @@ -1,6 +0,0 @@ -package de.juplo.kafka.chat.backend.domain; - -public interface ChatHomeFactory -{ - ChatHome createChatHome(int shard); -} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java deleted file mode 100644 index d2c6b635..00000000 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.juplo.kafka.chat.backend.persistence.inmemory; - -import de.juplo.kafka.chat.backend.domain.ChatHome; -import de.juplo.kafka.chat.backend.domain.ChatHomeFactory; -import lombok.RequiredArgsConstructor; - - -@RequiredArgsConstructor -public class InMemoryChatHomeFactory implements ChatHomeFactory -{ - private final InMemoryChatHomeService service; - - - @Override - public ChatHome createChatHome(int shard) - { - return new ChatHome(service, shard); - } -} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java index 94dd5d8a..e40d950f 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java @@ -3,6 +3,7 @@ package de.juplo.kafka.chat.backend.persistence.inmemory; import de.juplo.kafka.chat.backend.ChatBackendProperties; import de.juplo.kafka.chat.backend.api.KafkaLikeShardingStrategy; import de.juplo.kafka.chat.backend.api.ShardingStrategy; +import de.juplo.kafka.chat.backend.domain.ChatHome; import de.juplo.kafka.chat.backend.persistence.StorageStrategy; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -19,6 +20,24 @@ import java.time.Clock; @Configuration public class InMemoryServicesConfiguration { + @Bean + ChatHome[] chatHomes( + ChatBackendProperties properties, + InMemoryChatHomeService chatHomeService, + StorageStrategy storageStrategy) + { + ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()]; + storageStrategy + .read() + .subscribe(chatRoom -> + { + int shard = chatRoom.getShard(); + if (chatHomes[shard] == null) + chatHomes[shard] = new ChatHome(chatHomeService, shard); + }); + return chatHomes; + } + @Bean InMemoryChatHomeService chatHomeService( ChatBackendProperties properties, @@ -30,12 +49,6 @@ public class InMemoryServicesConfiguration storageStrategy.read()); } - @Bean - InMemoryChatHomeFactory chatHomeFactory(InMemoryChatHomeService service) - { - return new InMemoryChatHomeFactory(service); - } - @Bean InMemoryChatRoomFactory chatRoomFactory( ShardingStrategy strategy, diff --git a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java index 12d34be1..5ebd08d1 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java @@ -262,12 +262,12 @@ public class ChatBackendControllerTest @Bean ChatHome[] chatHomes( ChatBackendProperties properties, - ChatHomeFactory factory) + InMemoryChatHomeService service) { ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()]; Arrays .stream(properties.getInmemory().getOwnedShards()) - .forEach(i -> chatHomes[i] = factory.createChatHome(i)); + .forEach(i -> chatHomes[i] = new ChatHome(service, i)); return chatHomes; } }