+ @Bean
+ @ConditionalOnProperty(
+ prefix = "chat.backend.inmemory",
+ name = "sharding-strategy",
+ havingValue = "none",
+ matchIfMissing = true)
+ ChatHome noneShardingChatHome(InMemoryChatHomeService chatHomeService)
+ {
+ return new SimpleChatHome(chatHomeService);
+ }
+
+ @Bean
+ @ConditionalOnProperty(
+ prefix = "chat.backend.inmemory",
+ name = "sharding-strategy",
+ havingValue = "kafkalike")
+ ChatHome kafkalikeShardingChatHome(
+ ChatBackendProperties properties,
+ InMemoryChatHomeService chatHomeService,
+ StorageStrategy storageStrategy)
+ {
+ int numShards = properties.getInmemory().getNumShards();
+ SimpleChatHome[] chatHomes = new SimpleChatHome[numShards];
+ storageStrategy
+ .read()
+ .subscribe(chatRoom ->
+ {
+ int shard = chatRoom.getShard();
+ if (chatHomes[shard] == null)
+ chatHomes[shard] = new SimpleChatHome(chatHomeService, shard);
+ });
+ ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards);
+ return new ShardedChatHome(chatHomes, strategy);
+ }
+