+ @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();
+ ShardingStrategy shardingStrategy = new KafkaLikeShardingStrategy(numShards);
+ SimpleChatHome[] chatHomes = new SimpleChatHome[numShards];
+ storageStrategy
+ .read()
+ .subscribe(chatRoom ->
+ {
+ int shard = shardingStrategy.selectShard(chatRoom.getId());
+ if (chatHomes[shard] == null)
+ chatHomes[shard] = new SimpleChatHome(chatHomeService, shard);
+ });
+ return new ShardedChatHome(chatHomes, shardingStrategy);
+ }
+