- `ChatHomeFactory` was only used during initialization.
- By moving the instanciation of `chatHomes` into the implementation-
specific configuration, the necessity of the factory vanished.
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;
@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()
{
+++ /dev/null
-package de.juplo.kafka.chat.backend.domain;
-
-public interface ChatHomeFactory
-{
- ChatHome createChatHome(int shard);
-}
+++ /dev/null
-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);
- }
-}
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;
@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,
storageStrategy.read());
}
- @Bean
- InMemoryChatHomeFactory chatHomeFactory(InMemoryChatHomeService service)
- {
- return new InMemoryChatHomeFactory(service);
- }
-
@Bean
InMemoryChatRoomFactory chatRoomFactory(
ShardingStrategy strategy,
@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;
}
}