From: Kai Moritz Date: Wed, 19 Apr 2023 15:30:55 +0000 (+0200) Subject: NEU X-Git-Tag: kafkadata~15 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=cbc0ed5c9bf21fc08ca4312627342738e331c634;p=demos%2Fkafka%2Fchat NEU --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatMessageChannel.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatMessageChannel.java index 230f8226..78665dee 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatMessageChannel.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatMessageChannel.java @@ -251,35 +251,19 @@ public class ChatMessageChannel implements Runnable, ConsumerRebalanceListener { 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 getChatRoom(int shard, UUID id) diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatRoomChannel.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatRoomChannel.java index b08a50d2..ac44f997 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatRoomChannel.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatRoomChannel.java @@ -27,49 +27,12 @@ public class ChatRoomChannel implements Runnable private final Producer 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 records = consumer.poll(Duration.ofMinutes(5)); - log.info("Fetched {} messages", records.count()); - - for (ConsumerRecord 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 sendCreateChatRoomRequest( UUID chatRoomId, String name) @@ -104,4 +67,42 @@ public class ChatRoomChannel implements Runnable })); }); } + + @Override + public void run() + { + consumer.assign(List.of(new TopicPartition(topic, 0))); + + running = true; + + while (running) + { + try + { + ConsumerRecords records = consumer.poll(Duration.ofMinutes(5)); + log.info("Fetched {} messages", records.count()); + + for (ConsumerRecord 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); + } }