Message.MessageKey key = Message.MessageKey.of(chatMessageTo.getUser(), chatMessageTo.getId());
Message message = new Message(key, offset, timestamp, chatMessageTo.getText());
- ChatRoomData chatRoomData = this
- .chatRoomData[partition]
- .computeIfAbsent(chatRoomId, this::computeChatRoomData);
+ ChatRoomData chatRoomData = computeChatRoomData(chatRoomId, partition);
KafkaChatMessageService kafkaChatRoomService =
(KafkaChatMessageService) chatRoomData.getChatRoomService();
.toArray();
}
+ void createChatRoomData(ChatRoomInfo chatRoomInfo)
+ {
+ computeChatRoomData(chatRoomInfo.getId(), chatRoomInfo.getShard());
+ }
+
Mono<ChatRoomData> getChatRoomData(int shard, UUID id)
{
if (loadInProgress)
return Mono.error(new ShardNotOwnedException(instanceId, shard));
}
- return channelMediator
- .getChatRoomInfo(id)
- .map(chatRoomInfo ->
- chatRoomData[shard].computeIfAbsent(id, this::computeChatRoomData));
+ return Mono.justOrEmpty(chatRoomData[shard].get(id));
}
- private ChatRoomData computeChatRoomData(UUID chatRoomId)
+ private ChatRoomData computeChatRoomData(UUID chatRoomId, int shard)
{
- log.info("Creating ChatRoom {} with buffer-size {}", chatRoomId, bufferSize);
- KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
- return new ChatRoomData(clock, service, bufferSize);
+ ChatRoomData chatRoomData = this.chatRoomData[shard].get(chatRoomId);
+
+ if (chatRoomData != null)
+ {
+ log.info(
+ "Ignoring request to create already existing ChatRoomData for {}",
+ chatRoomId);
+ }
+ else
+ {
+ log.info("Creating ChatRoomData {} with buffer-size {}", chatRoomId, bufferSize);
+ KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
+ chatRoomData = new ChatRoomData(clock, service, bufferSize);
+ this.chatRoomData[shard].put(chatRoomId, chatRoomData);
+ }
+
+ return chatRoomData;
}
ConsumerGroupMetadata getConsumerGroupMetadata()
private final long[] nextOffset;
private final Map<UUID, ChatRoomInfo> chatRoomInfo;
private final String instanceUri;
+ private final ChannelMediator channelMediator;
private boolean running;
@Getter
Consumer<String, AbstractMessageTo> infoChannelConsumer,
Duration pollingInterval,
int numShards,
- URI instanceUri)
+ URI instanceUri,
+ ChannelMediator channelMediator)
{
log.debug(
"Creating InfoChannel for topic {}",
.forEach(partition -> this.nextOffset[partition] = -1l);
this.instanceUri = instanceUri.toASCIIString();
+
+ this.channelMediator = channelMediator;
}
chatRoomId);
this.chatRoomInfo.put(chatRoomId, chatRoomInfo);
+ this.channelMediator.chatRoomCreated(chatRoomInfo);
}
}
infoChannelConsumer,
properties.getKafka().getPollingInterval(),
properties.getKafka().getNumPartitions(),
- properties.getKafka().getInstanceUri());
+ properties.getKafka().getInstanceUri(),
+ channelMediator);
channelMediator.setInfoChannel(infoChannel);
return infoChannel;
}
ChannelMediator channelMediator,
ShardingPublisherStrategy shardingPublisherStrategy)
{
- return new DataChannel(
+ DataChannel dataChannel = new DataChannel(
properties.getInstanceId(),
properties.getKafka().getDataChannelTopic(),
producer,
clock,
channelMediator,
shardingPublisherStrategy);
+ channelMediator.setDataChannel(dataChannel);
+ return dataChannel;
}
@Bean