{
Integer partition = chatRoom.getShard();
UUID chatRoomId = chatRoom.getId();
- ChatRoom existingChatRoom = chatrooms[partition].get(chatRoomId);
- if (existingChatRoom == null)
+ if (chatrooms[partition].containsKey(chatRoomId))
+ {
+ log.warn("Ignoring existing chat-room: " + chatRoom);
+ }
+ else
{
log.info(
- "Creating new chat-room in partition {}: {}",
+ "Adding new chat-room to partition {}: {}",
partition,
chatRoom);
+
chatrooms[partition].put(chatRoomId, chatRoom);
}
- else
- {
- if (chatRoom.getShard() != existingChatRoom.getShard())
- {
- throw new IllegalArgumentException(
- "Could not change the shard of existing chat-room " +
- chatRoomId + " from " +
- existingChatRoom.getShard() + " to " +
- chatRoom.getShard());
- }
- else
- {
- log.info(
- "Updating chat-room in partition {}: {} -> {}",
- partition,
- existingChatRoom,
- chatRoom);
- existingChatRoom.s
- }
- }
}
Mono<ChatRoom> getChatRoom(int shard, UUID id)
private final Producer<Integer, ChatRoomTo> producer;
private final ShardingStrategy shardingStrategy;
private final ChatMessageChannel chatMessageChannel;
+ private final Clock clock;
+ private final int bufferSize;
private boolean running;
- @Override
- public void run()
- {
- consumer.assign(List.of(new TopicPartition(topic, 0)));
-
- running = true;
-
- while (running)
- {
- try
- {
- ConsumerRecords<Integer, ChatRoomTo> records = consumer.poll(Duration.ofMinutes(5));
- log.info("Fetched {} messages", records.count());
-
- for (ConsumerRecord<Integer, ChatRoomTo> record : records)
- {
- UUID id = record.value().getId();
- String name = record.value().getName();
- chatRoomFactory.createChatRoom(id, name);
- }
- }
- catch (WakeupException e)
- {
- }
- catch (RecordDeserializationException e)
- {
- }
- }
- }
-
- void createChatRoom()
- {
- log.info("Creating ChatRoom with buffer-size {}", bufferSize);
- KafkaChatRoomService service = new KafkaChatRoomService(chatMessageChannel, id);
- int shard = shardingStrategy.selectShard(id);
- ChatRoom chatRoom = new ChatRoom(id, name, shard, clock, service, bufferSize);
- chatMessageChannel.putChatRoom(chatRoom);
- }
-
Mono<ChatRoomInfo> sendCreateChatRoomRequest(
UUID chatRoomId,
String name)
}));
});
}
+
+ @Override
+ public void run()
+ {
+ consumer.assign(List.of(new TopicPartition(topic, 0)));
+
+ running = true;
+
+ while (running)
+ {
+ try
+ {
+ ConsumerRecords<Integer, ChatRoomTo> records = consumer.poll(Duration.ofMinutes(5));
+ log.info("Fetched {} messages", records.count());
+
+ for (ConsumerRecord<Integer, ChatRoomTo> record : records)
+ {
+ createChatRoom(record.value().toChatRoomInfo());
+ }
+ }
+ catch (WakeupException e)
+ {
+ log.info("Received WakeupException, exiting!");
+ running = false;
+ }
+ }
+ }
+
+ void createChatRoom(ChatRoomInfo chatRoomInfo)
+ {
+ UUID id = chatRoomInfo.getId();
+ String name = chatRoomInfo.getName();
+ int shard = chatRoomInfo.getShard();
+ log.info("Creating ChatRoom {} with buffer-size {}", id, bufferSize);
+ KafkaChatRoomService service = new KafkaChatRoomService(chatMessageChannel, id);
+ ChatRoom chatRoom = new ChatRoom(id, name, shard, clock, service, bufferSize);
+ chatMessageChannel.putChatRoom(chatRoom);
+ }
}