From: Kai Moritz Date: Sat, 3 Feb 2024 14:20:25 +0000 (+0100) Subject: refactor: separated message- and record- (aka offset-) handling X-Git-Tag: rebase--2024-02-18--17-42~13 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=016799a742995f203ec29ab77210763a8a9c377b;p=demos%2Fkafka%2Fchat refactor: separated message- and record- (aka offset-) handling --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java index 3429d405..3fe15c44 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java @@ -196,7 +196,11 @@ public class InfoChannel implements Runnable { ConsumerRecords records = consumer.poll(Duration.ofMinutes(1)); log.debug("Fetched {} messages", records.count()); - handleMessages(records); + for (ConsumerRecord record : records) + { + handleMessage(record); + updateNextOffset(record.partition(), record.offset() + 1); + } } catch (WakeupException e) { @@ -208,47 +212,47 @@ public class InfoChannel implements Runnable log.info("Exiting normally"); } - private void handleMessages(ConsumerRecords records) + private void updateNextOffset(int partition, long nextOffset) { - for (ConsumerRecord record : records) - { - switch (record.value().getType()) - { - case EVENT_CHATROOM_CREATED: - EventChatRoomCreated eventChatRoomCreated = - (EventChatRoomCreated) record.value(); - createChatRoom(eventChatRoomCreated.toChatRoomInfo()); - break; - - case EVENT_SHARD_ASSIGNED: - EventShardAssigned eventShardAssigned = - (EventShardAssigned) record.value(); - log.info( - "Shard {} was assigned to {}", - eventShardAssigned.getShard(), - eventShardAssigned.getUri()); - shardOwners[eventShardAssigned.getShard()] = eventShardAssigned.getUri(); - break; - - case EVENT_SHARD_REVOKED: - EventShardRevoked eventShardRevoked = - (EventShardRevoked) record.value(); - log.info( - "Shard {} was revoked from {}", - eventShardRevoked.getShard(), - eventShardRevoked.getUri()); - shardOwners[eventShardRevoked.getShard()] = null; - break; - - default: - log.debug( - "Ignoring message for key={} with offset={}: {}", - record.key(), - record.offset(), - record.value()); - } + this.nextOffset[partition] = nextOffset; + } - nextOffset[record.partition()] = record.offset() + 1; + private void handleMessage(ConsumerRecord record) + { + switch (record.value().getType()) + { + case EVENT_CHATROOM_CREATED: + EventChatRoomCreated eventChatRoomCreated = + (EventChatRoomCreated) record.value(); + createChatRoom(eventChatRoomCreated.toChatRoomInfo()); + break; + + case EVENT_SHARD_ASSIGNED: + EventShardAssigned eventShardAssigned = + (EventShardAssigned) record.value(); + log.info( + "Shard {} was assigned to {}", + eventShardAssigned.getShard(), + eventShardAssigned.getUri()); + shardOwners[eventShardAssigned.getShard()] = eventShardAssigned.getUri(); + break; + + case EVENT_SHARD_REVOKED: + EventShardRevoked eventShardRevoked = + (EventShardRevoked) record.value(); + log.info( + "Shard {} was revoked from {}", + eventShardRevoked.getShard(), + eventShardRevoked.getUri()); + shardOwners[eventShardRevoked.getShard()] = null; + break; + + default: + log.debug( + "Ignoring message for key={} with offset={}: {}", + record.key(), + record.offset(), + record.value()); } }